Git Cheat Sheet

Updated: June 19, 2019  |  2 minute read

This is not a complete Git cheat sheet for everyone, this is just a personal cheat sheet for some often forgotten git commands.

Undo

Discard changes in working directory

# discard changes to a file in working directory
git checkout <filename or wildcard>

# discard changes to all files in working directory
git checkout .
# or
git checkout *

Untracked files cannot be discarded by checkout.

Unstage from staging area

StackOverflow: How do I undo git add before commit?

# unstage a file from staging area
git reset <filename or wildcard>

# unstage all files from staging area
git reset

No more need to add HEAD like git reset HEAD <file> and git reset HEAD since git v1.8.2.

Dont use git rm --cached <filename> to unstage, it works only for newly created file to remove them from the staging area. But if you specify a existing file, it will delete it from cache, even if it is not staged.

Undo commit to working directory

StackOverflow: How do I undo the most recent local commits in Git?

You should readd the files if you want to commit them, as they’re in the working directory now, they’re unstaged too.

# Undo last commit to working directory
git reset head~
# same as to
git reset head~1

# Undo last 2 commits to working directory
git reset head~2

# Undo till a special commit to working directory,
# the special commit and every commits before are still committed.
git reset <commit number>

git reset head will do nothing, as the head is already at the last commit.

Undo commit to staging area

StackOverflow: How do I undo the most recent local commits in Git?

Often used to just change a commit message, because the uncommitted files have already been staged.

# Undo last commit to staging area
git reset --soft head~
# same as to
git reset --soft head~1

# Undo the last 2nd commit to staging area,
# and all the after (the last commit in this case) to working directory
git reset --soft head~2

# Undo till a special commit to staging area,
# the special commit and every commits before are still committed.
git reset --soft <commit number>

git reset head will do nothing, as the head is already at the last commit.

git reset --hard head~ will undo the last commit (head~) and also delete the changes from the working directory. This is like doing git reset head~ ; git checkout . . If you want to rollback the reset --hard, and you have the discarded commit number, you can rollback by git cherry-pick <commit number>

GUI

GitForWindows ships with a GUI tool, very cool.

# start git gui tool
git gui

git-gui

Tags:

Updated:

Leave a comment