git is an extremely powerful version control system created by Linus Torvalds.
This post will be an ever growing list of
git commands I frequently use.
Set Global Name and Email
git config --global user.name "Firstname Lastname"
git config --global user.email "[email protected]"
Change Author and Committer Name and Email
This is not a command you should run on a git repo being shared. Only run this command if you have a git repo that only you work in, and want to change the author and committer name and email on every commit. Every commit’s hash will be recalculated.
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='New Author Name'; GIT_AUTHOR_EMAIL='New Author Email'; GIT_COMMITTER_NAME='New Committer Name'; GIT_COMMITTER_EMAIL='New Committer Email';" HEAD
Or, with line breaks to read easier:
git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='New Author Name'
GIT_AUTHOR_EMAIL='New Author Email'
GIT_COMMITTER_NAME='New Committer Name'
GIT_COMMITTER_EMAIL='New Committer Email'
Amend Last Commit Message
git commit --amend
Undo the Last Commit
git reset HEAD~1
Lose All Changes
Use extreme caution running this command.
git reset --hard HEAD~1
Git Commit with Empty Message
Normally you wouldn’t git commit with an empty message. But if you’re editing Gists from GitHub on your workstation, this is very useful:
git commit -a --allow-empty-message -m ''
Recover Hard Deleted Commit
If you deleted a commit using
git reset --hard HEAD~1 and need to recover it, you can recover it with the following commands:
git fsck --lost-found
One to many dangling commits should be returned.
At the very least, you will need to know the first 7 digits of your deleted commit’s SHA hash. You should have this short SHA hash somewhere in your terminal scroll back. If you don’t have it, and you have many dangling commits, you can run
git show <SHA HASH> on each dangling commit to figure out the right one to recover.
Once you have the 7 digit short SHA or the entire SHA hash, merge it into your current branch with the following command:
git merge <SHA HASH>
Remove Untracked Files
git clean -f -d
Remove Untracked and Ignored Files
git clean -f -x -d
Find Particular Commit in Branch or Tag
If the commit is on a local tracking branch:
git branch --contains <COMMIT>
If the commit is on a remote tracking branch:
git branch -a --contains <COMMIT>
If the commit is in a tag:
git tag --contains <COMMIT>
Merge Branch with One Commit
If you have been developing in a branch that contains a lot of commits you would rather not merge into the master branch, you can merge and squash all those commits into one commit by using the following commands:
git checkout master
git merge --squash dev
git commit -a -m "Commit message"
Delete Remote Branch
git push origin --delete <BRANCH>
Different Git Log Outputs
One Line Git Log
git log --oneline
Styled Git Log
git log --all --graph --decorate --oneline --abbrev-commit
Count Number of Commits
git shortlog -sn
Get Commit Short SHA
Run the following command to obtain the 7 character short SHA, perhaps to use for a Docker image tag, for a particular commit:
git rev-parse --short <SHA>