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>
git add <paths>
git reset B
- --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.
Examples
1) Undo add: $ git add myfile.txt
$ git reset
$ git commit -m "a comment"
$ git reset --soft HEAD^
$ git commit -a -c ORIG_HEAD
$ git branch atopicbranch
$ git reset --hard HEAD~3
$ git checkout atopicbranch
$ git commit ...
$ git reset --hard HEAD~3
$ 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
$ git pull
Auto-merging ....
Merge made by recursive.
... | 20 +++++----
...
$ git reset --merge ORIG_HEAD
$ 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
$ 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
$ 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