Git Commands Cheat Sheet

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.

Set Global Name and Email

git config --global user.name "Firstname Lastname"
git config --global user.email "you@example.com"

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'
  " HEAD

Amend Last Commit Message

git commit --amend

Undo the Last Commit and Keep All Changes

git reset HEAD~1

Undo the Last Commit and Lose All Changes

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 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>

References

GitHub Cheat Sheet

How do I change the author of a commit in git?

Git tips from the trenches

How to list branches that contain a given commit?

Command line one-liners

How much of a git sha is generally considered necessary to uniquely identify a change in a given codebase?

restoring lost commits



comments powered by Disqus