Sunday, May 4, 2014
This post will be less of a typical cheat sheet and more of an ever growing list of basic and advanced git commands I use but not frequently enough to remember them.
git config --global user.name "Firstname Lastname" git config --global user.email "firstname.lastname@example.org"
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' " HEAD
git commit --amend
git reset HEAD~1
git reset --hard HEAD~1
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 ''
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>
git clean -f -d
git clean -f -x -d
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>
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"
git push origin --delete <BRANCH>
git log --oneline
git log --all --graph --decorate --oneline --abbrev-commit
git shortlog -sn
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>