Git Cheatsheet
Basic knowledge of Git is assumed in this tutorial. If you are new to Git, visit http://rogerdudler.github.io/git-guide/ to learn the basics first.
Configurations
Set the global username
git config --global user.name "John Doe"
Set the global user email
git config --global user.mail "johndoe@domain.com"
Set the local username
git config --local user.name "John Doe"
Set the local user email
git config --local user.mail "johndoe@domain.com"
To unset a given configuration
git config --global --unset-all user.name
To list global configurations
git config --global --list
To replace configurations
git config --global --replace-all user.name "New User Name"
To ignore file mode changes (i.e file permissions)
git config core.fileMode false
To ignore new line character differences between systems (windows-linux)
git config --global core.autocrlf true
Working with remotes
List all the configured remote repositories
git remote
Remove a remote address for the repository
git remote rm <remote>
Change url of specific remote
git remote set-url origin https://github.com/username/repo.git
git remote set-url upstream https://github.com/username/repo.git
To push all your branches
git push <remote> --all
to push all your tags
git push <remote> --tags
prunes tracking branches not on the remote
git remote prune origin
Working with Branches
List all the branches
git branch
List all the branches (local and repote branches)
git branch --all
Make a switch to a specified branch
git checkout <branch>
Deleting specified branch
git branch -d <branch>
Tracking a remote branch
git branch -u <remote>/<branch>
To remove all branches locally that have been already removed from origin
git remote prune origin
Sorting branches by date of last commit
git branch --sort=-committerdate # DESC
git branch --sort=committerdate # ASC
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
Stashing
Stash/save local changes
git stash save "updated the offline file"
Apply stashed changes (at the top of stack) to working dir
git stash pop
List stashed changes on the stack
git stash list
Reverting to previous states
Reverting Files
*Showing file snapshot at specific commit
git show <commit-sha>:relative/path/to/file/inside/repo
Saving file snapshot at specific commit
git show <commit-sha>:relative/path/to/file/inside/repo > /new/path/to/file/content/at/selected/commit
Graph history visualization
Showing last commit
git log -1
Showing the current local history (-5 last five commits)
git log --decorate --graph -5
Showing commits after a specific date
git log --date=local --after="2014-02-12T16:36:00-07:00"
Miscelaneous
Revert changes (i.e. restore to a pre-edited stage) to files named AssemblyInfo.cs
on any folder of the repo
$ git restore '**/AssemblyInfo.cs'
Custom git command to list the last branches I've been working on
$ last-branches = !git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | hea
d -n 20 | awk -F' ~ HEAD@{' '{printf(\" \\033[33m%s: \\033[37m %s\\033[0m\\n\", substr($2, 1, length($2)-1), $1)}'
Git cherry-pick
$ git cherry-pick 67c3ed592b3f1dcf50367550bf1d5537a2421b41
Get specific file version content from a given branch (replaces file in working dir)
$ git checkout develop -- OrdersServer/FormDevExpress/OrderTemplate/Controls/OrderControl.cs > /tmp/OrderControl.dev.cs
**This prints the file content to the console, which we can redirect to a file**
$ git show release-100.13:OrdersServer/FormDevExpress/OrderTemplate/Controls/OrderControl.cs > /tmp/OrderControl.13.cs
Stash specific file
$ git stash push -- OrdersServer/NewLaceupUI/MainForm.Designer.cs
Stash specific file with custom message
$ git stash push -m "temp store changes on MainForm Designer" OrdersServer/NewLaceupUI/MainForm.Designer.cs
Add a message describing the changes made to the stash
$ git stash save "message"
Give the stash a name (stash label), you can use the -m
option
$ git stash save -m "My stash name"
Remove configured commit.template
$ git config --unset --local commit.template
Revert the last N commits
$ git checkout -f HEAD~16 -- .
This makes your code go back 16 commits in your local storage, without rewriting any history with those 16 commits. The "changes" (reverting the past 16 commits) should be at the staging area at this moment.
https://www.reddit.com/r/git/comments/ypnys6/i_want_to_revert_the_code_to_a_previous_commit/