Git Commands Cheat Sheet

This is a list of git commands for quick reference.
Kindly refer to other git tutorials if you are not familiar with git commands.
To check the installed git version
git --version
To see the list of git commands
git
Git configuration commands
Sets up git with your name
git config --global user.name “<username>”
Sets up git with your email
git config --global user.email “<email_address>”
Windows-style checkout
git config --global core.autocrlf true
Atom editor setup
git config --global core.editor “atom --wait”
Sublime Text setup
git config --global core.editor “‘C:/Program Files/Sublime Text 2/sublime_text.exe’ -n -w”
VS code setup
git config --global core.editor “code --wait”
Makes sure that git output is colored
git config --global color.ui auto
Displays the original state in a conflict
git config --global merge.conflictstyle diff3
Windows git credentials manager
git config --global credential.helper wincred
To view the current configuration
git config -l
git config --list
To create a repository
Empty repository
git init
With the provided project name
git init <project_name>
To clone repositories
To clone a repo
git clone <repository_link>
To clone a repo and rename the directory
git clone <repository_link> <new_directory_name>
To check status
git status
To check history
To display all of the commits of a repository
git log
Displays commit message with just the short SHA and commit message
git log --oneline
To get commit statistics: display the files that have been changed in the commit, as well as the number of lines that have been added or deleted
git log --stat
To display the actual changes made to a file
git log -p
git log --patch
Shows the patch information, but will not highlight lines where only whitespace changes have occurred.
git log -p -w
To display details of a specific commit
git log <flag_params> <commit_SHA>
To display commit details in a graph (bulleted)
git log --oneline --graph
To display commit details from all branches in a graph (bulleted)
git log --oneline --graph --all
Note: Use ‘q’ to return from git log
To check details about the last commit
git show
To check details about a given commit
git show <commit_SHA>
Note: The output of the git show command is exactly the same as the git log -p command unless — stat or other flags are provided.
Staging
To move files from working directory to staging index
git add <filename.extension>
To stage multiple files
git add <file1> <file2> … <fileN>
Note: If one file has errors in staging, no file is staged.
To stage all files in the current directory and all nested directories
git add .
To unstage files
git rm --cached <filename-extension>
To unstage all files in the current directory and all nested directories
git rm --cached -r .
Committing
To commit staged files
git commit
To add commit message in command without opening thecode editor
git commit -m <commit_message>
To view uncommitted changes
To see changes that have been made but haven’t been committed
git diff
.gitignore :
Add files to ignore in the “.gitignore” file
- blank lines can be used for spacing
- # marks the line as a comment
- * matches 0 or more characters
- ? matches 1 character
- [abc] matches a, b, _or_ c
- ** matches nested directories: a/**/z matches a/z, a/b/z ,a/b/c/z etc.
Git tags
To display all tags in the repo
git tag
To add an annotated tag to a recent commit
git tag -a <tag>
To add an annotated tag to a past commit
git tag -a <tag> <commit_SHA>
To add tag without annotated info
git tag <tag>
To view tags in commit logs
git log --decorate
(or)
git log
To delete a tag
git tag -d <tag>
git tag --delete <tag>
Branching
To list all branches in the repository
git branch
Note: The one with ‘*’ is the active branch
To create a new branch
git branch <branch_name>
To checkout to a branch
git checkout <branch_name>
To delete a branch
git branch -d <branch_name>
To force delete a branch (To delete branch unique commits)
git branch -D <branch_name>
Checkout
To checkout to a branch
git checkout <branch_name>
To create a new branch from the current branch and checkout to it
git checkout -b <branch_name>
To create a new branch from a given checkout branch and checkout to it
git checkout -b <branch_name> <checkout_branch_name>
Merging
To merge a branch
git merge <branch_name>
Note: Checkout correct branch before merging
Fast-forward merging: Branch to merge in is directly ahead of checked out branch
Changing the last commit
To change the last commit message
It also commits any staged files
git commit --amend
Revert commit
To revert changes made by a commit
git revert <SHA-of-commit-to-revert>
Note: This creates a new commit with commit message ‘Revert “commit message of commit to revert” ‘.
Reset commit
Note :
- ^ indicates the parent commit
- ~ indicates the first parent commit
- The parent commit :
HEAD^, HEAD~ or HEAD~1
- The grandparent commit
HEAD^^ or HEAD~2
- The great-grandparent commit
HEAD^^^ or HEAD~3
- On Windows in cmd.exe, ^ is a special character and needs to be treated differently. You can either double it or put the commit reference in quotes.
To undo a merge
git reset HEAD~1
or
git reset --mixed HEAD~1
Moves last commit changes to the working directory
To the working directory
--mixed
To staging area
--soft
To trash
--hard
To undo a merge
git reset — hard HEAD^
Please comment on the commands you would like to add to the list.
Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.
To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇