GIT reverting things

http://stackoverflow.com/questions/1146973/how-to-revert-all-local-changes-in-a-git-managed-project-to-previous-state
# 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:
git reset
# If you want to revert a change that you have committed, do this:
git revert ...
# If you've messed up the working tree, but haven't yet committed your mistake, you can return the entire working tree to the last committed state with
git reset --hard HEAD
# to reset a local branch so that it matches the remote server (example assumes that you want to revert the master branch):
git fetch origin
git checkout master
git reset --hard origin/master
 
# If you want to delete all untracked files, you need the git clean command.
# -d will also delete directories.
# -n will do a trial run, only showing what would happen but changing nothing
# -f force. If the git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f or -n.
 
# show what would happen
git clean -d -n
# delete all untracked files and directories
git clean -d -f
# or
git clean -df
And, after doing git reset --hard, you can undo it with git reflog:
$ git init > /dev/null
$ touch a
$ git add .
$ git commit -m"Add file a" > /dev/null
$ echo 'foo' >> a
$ git commit -a -m"Append foo to a" > /dev/null
$ for i in b c d e; do echo $i >>a; git commit -a -m"Append $i to a" ;done > /dev/null
$ git reset --hard HEAD^^ > /dev/null
$ cat a
foo
b
c
$ git reflog
145c322 HEAD@{0}: HEAD^^: updating HEAD
ae7c2b3 HEAD@{1}: commit: Append e to a
fdf2c5e HEAD@{2}: commit: Append d to a
145c322 HEAD@{3}: commit: Append c to a
363e22a HEAD@{4}: commit: Append b to a
fa26c43 HEAD@{5}: commit: Append foo to a
0a392a5 HEAD@{6}: commit (initial): Add file a
$ git reset --hard HEAD@{2}
HEAD is now at fdf2c5e Append d to a
$ cat a
foo
b
c
d
Here's a great post in the Git Book about reverting merges: http://git-scm.com/2010/03/02/undoing-merges.html

Internal References

External References

Article Type

General