Git Cheatsheet
A cheatsheet is a crisp set of notes, or commands used as a quick reference. Git is the most popular modern version control system (open-source and free). The below table contains the list of most frequently used Git commands. We present the Git commands with the syntax and the purpose of use. Also, the commands are grouped by their categories. To get a downloadable and printable Git Cheatsheet in PDF format, jump to the next section.
# | Category | Command | Purpose |
---|---|---|---|
1 | Setup | git config <key> <value> | Configure various Git options such as username, user email, etc. in the current repository |
2 | git config –global <key> <value> | Globally (all local repos) configure various Git options such as username, user email, etc. | |
3 | git init | Initialize a new local Git repository. Turn a directory into a Git repository. | |
4 | git clone <url> | Create a local working copy of an existing remote repository | |
5 | Status | git status | Display the state of the local repository (what is added, modified, and deleted) |
6 | Fetch | git fetch | Fetch the branch and commit details from the remote repositories without copying files |
7 | git pull origin <branch> | Fetch and merge the latest changes from the remote repository to the local repository | |
8 | git checkout <branch> | Checkout an existing branch and switch to it | |
9 | git checkout <commit> | Checkout a specific commit | |
10 | Branch | git branch | List all local branches and determine which branch the local repository is on |
11 | git branch -a | List all branches, including the remote ones | |
12 | git branch <name> | Create a new branch, but remain in the current branch | |
13 | git branch -d <branch> | Delete a branch | |
14 | git switch <branch> | Switch to a branch | |
15 | git checkout -b <branch> | Create a new branch and immediately switch to the new branch | |
16 | Develop | git add <file1> <file2> <file3> | Add specified files to the staging area in order to commit subsequently |
17 | git add –all | Add all files that are not staged to the staging area in order to commit subsequently | |
18 | git clean | Remove untracked files from the local repository | |
19 | git rm <file> | Remove tracked files or directories from the index or from the working directory | |
20 | git mv <file1> <file2> | Rename a file and update the index | |
21 | git checkout <file> | Discard changes made to the file in the local repository | |
22 | git commit -m <message> | Save the changes to the local repository | |
23 | git commit –amend | Amend (update) the most recent commit | |
24 | Submit | git push origin <branch> | Push (submit) the content of commits from the local repository to the remote repository |
25 | Sync | git merge <branch> | Integrate changes from one branch to another branch. Creates a merge commit. |
26 | git rebase <branch> | Integrate changes from one branch to another branch. Rewrites commit history. | |
27 | Analyze | git log | Show the commit history |
28 | git show <commit> | Show details of a specific commit | |
29 | git blame <file> | Show what revision and author last modified each line of a file | |
30 | git diff | View changes between the working directory and the staging area | |
31 | git diff <file> | View changes in a file | |
32 | git diff <branch1> <branch2> <file> | View changes of a file between two branches | |
33 | git diff <commit1> <commit2> <file> | View changes of a file between two commits | |
34 | Revert | git reset –hard <commit> | Undo changes to files in the working directory. Other options are --soft and --mixed . |
35 | git reset –hard HEAD~2 | Move the current branch backward by 2 commits | |
36 | git revert HEAD~2 | Revert (Undo) the 2nd to last commit and create a new commit | |
37 | Stash | git stash | Save changes made when they are not in a state to commit them |
38 | git stash list | Lists all stashes | |
39 | git stash pop | Applies the top stashed element and removes it | |
40 | git stash apply | Applies the top stashed element and preserves it | |
41 | git remote <cmd> <name> <url> | Add a remote repository | |
42 | git remote -v | List named remote repositories | |
43 | Tag | git tag | List all tags |
44 | git tag <name> | Create a new tag | |
45 | Others | .gitignore | A file placed in the repository to specify exclusion file patterns separated by new line |
.git/config or ~/.gitconfig | The git configuration file for a specific repository or global configuration |
Download Git Cheatsheet
Download the printable single-page Git cheatsheet in PDF format by clicking the below button.
Git Cheatsheet – Frequently Used Git Commands
Be The First
Join our list to get instant access to new articles and weekly newsletter.