Pagine

Monday 29 May 2017

How to use the git reset command

The git reset command will allow you to go back to a previous state (for example, commit). The git reset command has three options (soft, hard, or mixed, by default). In general, the git reset command's aim is to take the current branch, reset it to point somewhere else, and possibly bring the index and work tree along. 
This means that
 git reset <paths>
is the opposite of
 git add <paths>
More concretely, if the master branch (currently checked out) looks like the first row (in the following figure) and you want it to point to B and not C, you will use this command:
 git reset B
The more options that you can provide on the reset command can be easily explained:
  • --hard: This option is the simplest. It will restore the content to the given commit. All the local changes will be erased. The git reset --hard command means git reset --hard HEAD, which will reset your files to the previous version and erase your local changes.
  • --mixed: This option resets the index, but not the work tree. It will reset your local files, but the differences found during the process will be marked as local modifications if you analyze them using git status. It's very helpful if you make some bugs on previous commits and want to keep your local changes.
  • --soft: This option will keep all your files, such as mixed, intact. If you use git status, it will appear as changes to commit. You can use this option when you have not committed files as expected, but your work is correct.
  • --merge: this option resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted. In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries.
  • --keep: this options resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is aborted.
The git reset command doesn't remove untracked files; use git clean instead.

Examples

1) Undo add:
 $ git add myfile.txt
 $ git reset
2) Undo a commit and redo:
 $ git commit -m "a comment"
 $ git reset --soft HEAD^
 $ git commit -a -c ORIG_HEAD
3) Undo a commit, making it a topic branch:
 $ git branch atopicbranch    
 $ git reset --hard HEAD~3  
 $ git checkout atopicbranch
4) Undo commits permanently:
 $ git commit ...
 $ git reset --hard HEAD~3
5) Undo a merge or pull:
 $ git pull
 Auto-merging ...
 CONFLICT (content): Merge conflict in ...
 Automatic merge failed; fix conflicts and then commit the result.
 $ git reset --hard
 $ git pull . atopicbranch
 Updating from 81683... to 43984...
 Fast-forward
 $ git reset --hard ORIG_HEAD
6) Undo a merge or pull inside a dirty working tree:
 $ git pull
 Auto-merging ....
 Merge made by recursive.
  ...                |   20 +++++----
  ...
 $ git reset --merge ORIG_HEAD
7) Interrupted workflow:
 $ git checkout feature ;
...developement...
 $ git commit -a -m "snapshot"
 $ git checkout master
...bug fixing...
 $ git commit 
 $ git checkout feature
 $ git reset --soft HEAD^ ;# go back to before bug fixing 
 $ git reset
8) Keep changes in working tree while discarding some previous commits:
 $ git tag 1.0
 $ git checkout -b branch1
... edit one o more files...
 $ git commit -m "a comment"
... edit one o more files...
 $ git checkout -b branch2
 $ git reset --keep 1.0
9) Split a commit apart into a sequence of commits:
 $ git reset -N HEAD^
 $ git add -p
 $ git diff --cached
 $ git commit -c HEAD@{1}
...
 $ git add file.txt
 $ git diff --cached
 $ git commit -m "a comment"

No comments:

Post a Comment