Studyon Minte9.com
Git

Study

Git reset




$ git reset --hard HEAD~1 // reset last commit $ git reflog $ git reset --hard f6e5064 // reset to reference
1) Make 2 commits
$ git init $ echo "line 1" > example.txt $ git add . $ git commit -m "first commit" $ echo "line 2" > example.txt $ git commit -a -m "second commit" $ git status # On branch master nothing to commit, working directory clean $ git log commit a6c3d003911ad4dc3990b7f855129018e70424cf second commit commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit
2) Reset (soft) - Modification remains in staging
$ git reset --soft HEAD~1 $ git log commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: example.txt // commit again $ git commit -a -m "second commit 2" $ git log commit 7af6d11a50b1c2885e0f7bc1f422b348489ab3c1 second commit 2 commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit $ git status # On branch master nothing to commit, working directory clean
3) Reset (normal) - Modification is removed from staging
$ git reset HEAD~1 Unstaged changes after reset: M example.txt $ git log commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit $ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: example.txt # no changes added to commit (use "git add" and/or "git commit -a") // commit again $ git commit -a -m "second commit 3" $ git log commit 4e882c7994e80e116b8d0e086340df35c7435a1f second commit 3 commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit $ git status # On branch master nothing to commit, working directory clean
4) Reset (hard) - Modifications are removed (but remains in garbage for few days)
$ git reset --hard HEAD~1 HEAD is now at c2385c4 first commit $ git log commit c2385c44d5d48f7511576656b95c454ebe4e0036 first commit $ git status # On branch master nothing to commit, working directory clean $ less example.txt line 1 // with reset --hard // the commit is gone entirely
5) Undo reset --hard You can get the commit back as long as it was within a few days.
$ git reflog fa3c483 HEAD@{5}: reset: moving to HEAD~1 95f3ae9 HEAD@{6}: commit: m $ git reset --hard 95f3ae9
 


http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit
http://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1