Git Reset

 

Overview

* A tree can be visualized as a directory
* git reset: moves HEAD pointer in the current branch
* Counting commits backward:
HEAD~: one commit back
HEAD~2: two commits back
HEAD~n: n commits back
* Counting commits forward (do not use):
HEAD@{1}: one commit forward
* You can also use commit hash value to reset HEAD pointer
git reset cdd1a5

Three Kinds Trees

* HEAD: the directory containing last commit (git commit copies files from index to HEAD directory)
* Index: the directory containing next commit (git add copies files from working directory to index directory)
* Working directory

Three Flavors of Resets

* soft: move HEAD pointer back one commit (equivalent to undo last commit):

git reset --soft HEAD~

* mixed: move HEAD pointer back one commit AND update index files with HEAD (Equivalent to undo last commit AND last staging. This is default behavior):

git reset --mixed HEAD~

* hard: move HEAD pointer back one commit AND update BOTH index and working directory files with HEAD (Equivalent to undo everything! NOT working directory safe!):

git reset --hard HEAD~

Reset with Path

* Reset with path can only be applied with either –mixed (default) or –hard since HEAD pointer can only be moved for all files

# Undo last add for foo.txt file:
git reset foo.txt 
# equivalent to: 
git reset --mixed HEAD~ foo.txt

Squashing Commits

* Redo multiple previous commits into one single new commit

git reset --soft HEAD~2
git commit -m "Squashed commits"

reset vs checkout

* Use checkout on branches

git checkout dev_branch
# is equivalent to:
git reset --hard dev_branch
# but 
# - is working directory safe: changes will be merged
# - also moves HEAD to dev_branch

* Do not use checkout with path. It’s not working directory safe!

Remove Untracked Files

* Use with caution!

git clean -f # remove untracked files
git clean -df # remove untracked files and directories
git clean -xdf # also removes ignored files!

Ignore Committed Files

* Ignore files that have already been Committed

# remove all cached files from index
git rm -r --cached .
# add all files back except files in .ignore file
git add .
# commit without all ignored files
git commit -m 'Removing ignored files'

Examples

If you want to revert changes made to your working copy, do this:
git checkout .
 
If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:
git reset
 
If you want to revert a change that you have committed, do this:
git revert <commit 1> <commit 2>
 
If you want to remove untracked files (e.g., new files, generated files):
git clean -f
 
Or untracked directories (e.g., new or automatically generated directories):
git clean -fd

References

* 7.7 Git Tools – Reset Demystified
https://stackoverflow.com/questions/1146973/how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state

This entry was posted in git and tagged . Bookmark the permalink.