git reset 浅析
来源:互联网 发布:网络投资平台 编辑:程序博客网 时间:2024/06/09 19:32
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="com" style="color: rgb(136, 0, 0);"># Changes to be committed: </span></div><div><span class="com" style="color: rgb(136, 0, 0);"># (use "git reset HEAD <file>..." to unstage) </span></div><div><span class="com" style="color: rgb(136, 0, 0);"># </span></div><div><span class="com" style="color: rgb(136, 0, 0);">#</span><span style="white-space: pre;"><span class="com" style="color: rgb(136, 0, 0);"> </span></span><span class="com" style="color: rgb(136, 0, 0);">new file: Test.scala </span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(0, 0, 128);"></p><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">commit3</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> add test3</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">commit2</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> add test2</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">commit1</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> add test1</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">c</span></span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(0, 0, 128);"></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit2</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit1</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(0, 0, 128);"></p><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit3</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test3</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit2</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit1</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(0, 0, 128);"></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit2</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><div><span style="color: rgb(51, 102, 255);"><span class="pln" style="color: rgb(0, 0, 0);">commit1</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> add test1</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c</span></span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
DESCRIPTION
In the first and second form, copy entries from <commit> to the index. In the third form, set the current branch head (HEAD) to <commit>, optionally modifying index and working tree to match. The <commit> defaults to HEAD in all forms.
- git reset [-q] [<commit>] [--] <paths>…
This form resets the index entries for all <paths> to their state at <commit>. (It does not affect the working tree, nor the current branch.)
This means that git reset <paths> is the opposite of git add <paths>.
After running git reset <paths> to update the index entry, you can use git-checkout(1) to check the contents out of the index to the working tree. Alternatively, using git-checkout(1) and specifying a commit, you can copy the contents of a path out of a commit to the index and to the working tree in one go.
- git reset (--patch | -p) [<commit>] [--] [<paths>…]
Interactively select hunks in the difference between the index and <commit> (defaults to HEAD). The chosen hunks are applied in reverse to the index.
This means that git reset -p is the opposite of git add -p, i.e. you can use it to selectively reset hunks. See the “Interactive Mode” section of git-add(1) to learn how to operate the --patch mode.
- git reset --<mode> [<commit>]
This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>, which must be one of the following:
- --soft
Does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.
- --mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
- --hard
Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
- --merge
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
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.
If you want to undo a commit other than the latest on a branch, git-revert(1) is your friend.
OPTIONS
- -q
- --quiet
Be quiet, only report errors.
EXAMPLES
- Undo add
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ edit </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git add frotz</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c filfre</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c $ mailx </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git pull git</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="com" style="color: rgb(136, 0, 0);">//info.example.com/ nitfol </span><span class="com" style="color: rgb(136, 0, 0);"><4></span></p>
You are happily working on something, and find the changes in these files are in good order. You do not want to see them when you run "git diff", because you plan to work on other files and changes with these files are distracting.
Somebody asks you to pull, and the changes sounds worthy of merging.
However, you already dirtied the index (i.e. your index does not match the HEAD commit). But you know the pull you are going to make does not affect frotz.c nor filfre.c, so you revert the index changes for these two files. Your changes in working tree remain there.
Then you can pull and merge, leaving frotz.c and filfre.c changes still in the working tree.
- Undo a commit and redo
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git commit </span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">soft HEAD</span><span class="pun" style="color: rgb(102, 102, 0);">^</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ edit </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git commit </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">a </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">c ORIG_HEAD </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
Make corrections to working tree files.
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
See also the --amend option to git-commit(1).
- Undo a commit, making it a topic branch
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git branch topic</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);">wip </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard HEAD</span><span class="pun" style="color: rgb(102, 102, 0);">~</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git checkout topic</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);">wip </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
You have made some commits, but realize they were premature to be in the "master" branch. You want to continue polishing them in a topic branch, so create "topic/wip" branch off of the current HEAD.
Rewind the master branch to get rid of those three commits.
Switch to "topic/wip" branch and keep working.
- Undo commits permanently
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git commit </span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard HEAD</span><span class="pun" style="color: rgb(102, 102, 0);">~</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
The last three commits (HEAD, HEAD^, and HEAD~2) were bad and you do not want to ever see them again. Do not do this if you have already given these commits to somebody else. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for the implications of doing so.)
- Undo a merge or pull
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git pull </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Auto</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">merging nitfol CONFLICT </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">content</span><span class="pun" style="color: rgb(102, 102, 0);">):</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Merge</span><span class="pln" style="color: rgb(0, 0, 0);"> conflict </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln" style="color: rgb(0, 0, 0);"> nitfol </span><span class="typ" style="color: rgb(102, 0, 102);">Automatic</span><span class="pln" style="color: rgb(0, 0, 0);"> merge failed</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> fix conflicts </span><span class="kwd" style="color: rgb(0, 0, 136);">and</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">then</span><span class="pln" style="color: rgb(0, 0, 0);"> commit the result</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git pull </span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"> topic</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);">branch </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Updating</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">from</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">41223.</span><span class="pun" style="color: rgb(102, 102, 0);">..</span><span class="pln" style="color: rgb(0, 0, 0);"> to </span><span class="lit" style="color: rgb(0, 102, 102);">13134.</span><span class="pun" style="color: rgb(102, 102, 0);">..</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Fast</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">forward $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard ORIG_HEAD </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">4</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
Try to update from the upstream resulted in a lot of conflicts; you were not ready to spend a lot of time merging right now, so you decide to do that later.
"pull" has not made merge commit, so "git reset --hard" which is a synonym for "git reset --hard HEAD" clears the mess from the index file and the working tree.
Merge a topic branch into the current branch, which resulted in a fast-forward.
But you decided that the topic branch is not ready for public consumption yet. "pull" or "merge" always leaves the original tip of the current branch in ORIG_HEAD, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit.
- Undo a merge or pull inside a dirty working tree
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git pull </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Auto</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">merging nitfol </span><span class="typ" style="color: rgb(102, 0, 102);">Merge</span><span class="pln" style="color: rgb(0, 0, 0);"> made </span><span class="kwd" style="color: rgb(0, 0, 136);">by</span><span class="pln" style="color: rgb(0, 0, 0);"> recursive</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"> nitfol </span><span class="pun" style="color: rgb(102, 102, 0);">|</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">20</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">+++++----</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">merge ORIG_HEAD </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
Even if you may have local modifications in your working tree, you can safely say "git pull" when you know that the change in the other branch does not overlap with them.
After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory. Running "git reset --hard ORIG_HEAD" will let you go back to where you were, but it will discard your local changes, which you do not want. "git reset --merge" keeps your local changes.
- Interrupted workflow
Suppose you are interrupted by an urgent fix request while you are in the middle of a large change. The files in your working tree are not in any shape to be committed yet, but you need to get to the other branch for a quick bugfix.
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git checkout feature </span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="com" style="color: rgb(136, 0, 0);"># you were working in "feature" branch and $ work work work ;# got interrupted $ git commit -a -m "snapshot WIP" </span><span class="com" style="color: rgb(136, 0, 0);"><1></span><span class="com" style="color: rgb(136, 0, 0);"> $ git checkout master $ fix fix fix $ git commit ;# commit with real log $ git checkout feature $ git reset --soft HEAD^ ;# go back to WIP state </span><span class="com" style="color: rgb(136, 0, 0);"><2></span><span class="com" style="color: rgb(136, 0, 0);"> $ git reset </span><span class="com" style="color: rgb(136, 0, 0);"><3></span></p>
This commit will get blown away so a throw-away log message is OK.
This removes the WIP commit from the commit history, and sets your working tree to the state just before you made that snapshot.
At this point the index file still has all the WIP changes you committed as snapshot WIP. This updates the index to show your WIP files as uncommitted.
See also git-stash(1).
- Reset a single file in the index
Suppose you have added a file to your index, but later decide you do not want to add it to your commit. You can remove the file from the index while keeping your changes with git reset.
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);"> frotz</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git commit </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">m </span><span class="str" style="color: rgb(0, 136, 0);">"Commit files in index"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git add frotz</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">c </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
This removes the file from the index while keeping it in the working directory.
This commits all other changes in the index.
Adds the file to the index again.
- Keep changes in working tree while discarding some previous commits
Suppose you are working on something and you commit it, and then you continue working a bit more, but now you think that what you have in your working tree should be in another branch that has nothing to do with what you committed previously. You can start a new branch and reset it while keeping the changes in your working tree.
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">$ git tag start $ git checkout </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">b branch1 $ edit $ git commit </span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ edit $ git checkout </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln" style="color: rgb(0, 0, 0);">b branch2 </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);"> $ git reset </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">keep start </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="lit" style="color: rgb(0, 102, 102);">3</span><span class="pun" style="color: rgb(102, 102, 0);">></span></p>
This commits your first edits in branch1.
In the ideal world, you could have realized that the earlier commit did not belong to the new topic when you created and switched to branch2 (i.e. "git checkout -b branch2 start"), but nobody is perfect.
But you can use "reset --keep" to remove the unwanted commit after you switched to "branch2".
DISCUSSION
The tables below show what happens when running:
git reset --option target
to reset the HEAD to another commit (target) with the different reset options depending on the state of the files.
In these tables, A, B, C and D are some different states of a file. For example, the first line of the first table means that if a file is in state A in the working tree, in state B in the index, in state C in HEAD and in state D in the target, then "git reset --soft target" will leave the file in the working tree in state A and in the index in state B. It resets (i.e. moves) the HEAD (i.e. the tip of the current branch, if you are on one) to "target" (which has the file in state D).
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="margin-top: 1em; margin-bottom: 1.5em;"><div style="padding: 0px;"><pre style="white-space: pre-wrap; word-wrap: break-word; padding: 0px; margin-top: 0px; margin-bottom: 0px;"><tt style="color: navy;"><span class="pln" style="color: rgb(0, 0, 0);">working index HEAD target working index HEAD </span><span class="pun" style="color: rgb(102, 102, 0);">----------------------------------------------------</span><span class="pln" style="color: rgb(0, 0, 0);"> A B C D </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">soft A B D </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">mixed A D D </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard D D D </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">merge </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">disallowed</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">keep </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">disallowed</span><span class="pun" style="color: rgb(102, 102, 0);">)</span></tt>
working index HEAD target working index HEAD ---------------------------------------------------- A B C C --soft A B C --mixed A C C --hard C C C --merge (disallowed) --keep A C C
working index HEAD target working index HEAD ---------------------------------------------------- B B C D --soft B B D --mixed B D D --hard D D D --merge D D D --keep (disallowed)
working index HEAD target working index HEAD ---------------------------------------------------- B B C C --soft B B C --mixed B C C --hard C C C --merge C C C --keep B C C
working index HEAD target working index HEAD ---------------------------------------------------- B C C D --soft B C D --mixed B D D --hard D D D --merge (disallowed) --keep (disallowed)
working index HEAD target working index HEAD ---------------------------------------------------- B C C C --soft B C C --mixed B C C --hard C C C --merge B C C --keep B C C
"reset --merge" is meant to be used when resetting out of a conflicted merge. Any mergy operation guarantees that the working tree file that is involved in the merge does not have local change wrt the index before it starts, and that it writes the result out to the working tree. So if we see some difference between the index and the target and also between the index and the working tree, then it means that we are not resetting out from a state that a mergy operation left after failing with a conflict. That is why we disallow --merge option in this case.
"reset --keep" is meant to be used when removing some of the last commits in the current branch while keeping changes in the working tree. If there could be conflicts between the changes in the commit we want to remove and the changes in the working tree we want to keep, the reset is disallowed. That’s why it is disallowed if there are both changes between the working tree and HEAD, and between HEAD and the target. To be safe, it is also disallowed when there are unmerged entries.
The following tables show what happens when there are unmerged entries:
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="margin-top: 1em; margin-bottom: 1.5em;"><div style="padding: 0px;"><pre style="white-space: pre-wrap; word-wrap: break-word; padding: 0px; margin-top: 0px; margin-bottom: 0px;"><tt style="color: navy;"><span class="pln" style="color: rgb(0, 0, 0);">working index HEAD target working index HEAD </span><span class="pun" style="color: rgb(102, 102, 0);">----------------------------------------------------</span><span class="pln" style="color: rgb(0, 0, 0);"> X U A B </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">soft </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">disallowed</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">mixed X B B </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">hard B B B </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">merge B B B </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">keep </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">disallowed</span><span class="pun" style="color: rgb(102, 102, 0);">)</span></tt>
working index HEAD target working index HEAD ---------------------------------------------------- X U A A --soft (disallowed) --mixed X A A --hard A A A --merge A A A --keep (disallowed)
X means any state and U means an unmerged index.
- git reset 浅析
- 辛星浅析git reset和git revert
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git reset
- git# reset
- git reset
- git reset
- git reset
- git reset
- StartServiceCtrlDispatcher
- windows下tomcat和java环境变量的配置
- Cocos2d-X中的ZORDER和Tag
- GNU C语言的 扩展(二)零长度数组&可变参数宏
- 关于tomcat 部署web应用,serverxml的设置。
- git reset 浅析
- JS设置cookie、读取cookie、删除cookie
- 【Unity3D】学习笔记(第5记) 2D游戏开发基本技巧之拾取道具
- ios的消息传递—— performSelector
- Cocos studio sample讲解 DemoShop商店界面
- C++友元函数与友元类的用法
- 关键帧动画
- Android笔记 定义意图过滤器intent- filter demo
- 用js判断页面刷新或关闭的方法