Git Cheat Sheet
Free Git commands cheat sheet for developers and job seekers. Quick reference for daily work and interview prep, with a downloadable PDF from VeerLogix.
By netrapal Singh · · 3 min read
Git is a popular version control system that allows developers to keep track of changes made to their codebase, collaborate with others, and manage multiple versions of their code. It can be a powerful tool, but it can also have a steep learning curve for those new to it. That's where a Git cheat sheet can come in handy.
A Git cheat sheet is a quick reference guide that summarizes some of the most commonly used Git commands and their syntax. Here are some of the basic Git commands that you might find on a cheat sheet:
Git Basic commands:
Git init: Creates / Initializes a new Git repository within the current directory to start tracking changes.
git initGit clone: Clone an existing Git repository (on Github, Bitbucket, etc) to a new folder/directory
git clone <repository>Git add: Adds a file to the staging area (index) for the next commit
git add . git add <filename>Git add -p: Interactively adds changes to the staging area (useful for selectively staging changes within a file)
git add -pGit commit: To commit changes that have been staged
git commit -m "commit message"Git status: Displays the current state of both the working directory and the staging area
git statusGit log: Displays the commit history
git logGit branch: Lists all local branches in the repository
git branchGit checkout <branch>: You can switch to a designated branch within the repository
git checkout <branch>Git checkout -b <new-branch>: Creates a new branch and switches to the created branch
git checkout -b <new-branch>Git merge: Merges the specified branch into the current branch
git merge <branch>Git pull: Fetch and merge changes from the remote repository to the current branch
git pullGit push: Used to send modifications made in the current branch to a remote repository
git pushGit stash: Allows users to temporarily store changes that are not yet prepared for commit.
git stash
Git Advanced commands:
Git rebase: Reapplies commits from the current branch on top of the specified branch (useful for integrating changes from another branch into your own)
git rebase <branch>Git reset <commit>: Resets the repository to a previous commit (use with caution as it can permanently discard commits)
git reset <commit>Git reset --hard: Resets the repository to the last commit and discards all changes in the working directory
git reset --hardGit cherry-pick: Applies a single commit from one branch to another
git cherry-pick <commit>Git tag: Tags a specific commit with a meaningful name
git tag <tag-name> <commit>Git remote add <name> <url>: Adds a new remote repository with a given name and URL
git remote add <name> <url>Git remote set-url: The commend change the URL of remote repository associated with 'origin'.
git remote set-url origin <NEW_GIT_URL_HERE>Git remote -v: Using the command displays a list of all remote repositories along with their corresponding URLs.
git remote -vGit push --tags: Pushes all tags to a remote repository
git push --tagsGit submodule add: Adds a Git submodule to the repository at the specified path
git submodule add <repository> <path>Git submodule update: Initializes and updates Git submodules in the repository
git submodule updateGit bisect: Binary search for a specific commit that introduced a bug (useful for debugging)
git bisect
Git Tips:
Always start by checking the status of your repository before making any changes using git status It's advisable to make frequent commits with clear descriptions by using the command 'git commit -m "commit message" Use branches to keep your work organized and to collaborate with others Always pull before you push to avoid conflicts using git pull and git push Use .gitignore to ignore files and directories that don't belong in the repository
When in doubt, consult the Git documentation or seek help from experienced Git users.