table of contents
other versions
- jessie 1:2.1.4-2.1+deb8u6
- jessie-backports 1:2.11.0-3~bpo8+1
- stretch 1:2.11.0-3+deb9u4
- testing 1:2.20.1-2
- stretch-backports 1:2.20.1-1~bpo9+1
- unstable 1:2.20.1-2
- experimental 1:2.21.0+next.20190320-1
GIT-RESET(1) | Git Manual | GIT-RESET(1) |
NAME¶
git-reset - Reset current HEAD to the specified stateSYNOPSIS¶
git reset [-q] [<tree-ish>] [--] <paths>... git reset (--patch | -p) [<tree-ish>] [--] [<paths>...] git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
DESCRIPTION¶
In the first and second form, copy entries from <tree-ish> to the index. In the third form, set the current branch head (HEAD) to <commit>, optionally modifying index and working tree to match. The <tree-ish>/<commit> defaults to HEAD in all forms. git reset [-q] [<tree-ish>] [--] <paths>...This form resets the index entries for all <paths>
to their state at <tree-ish>. (It does not affect the working tree or
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) [<tree-ish>] [--] [<paths>...]
Interactively select hunks in the difference between the
index and <tree-ish> (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>. If
<mode> is omitted, defaults to "--mixed". The <mode>
must be one of the following:
--soft
If you want to undo a commit other than the latest on a branch,
git-revert(1) is your friend.
Does not touch the index file or 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.
If -N is specified, removed paths are marked as intent-to-add (see
git-add(1)).
--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.
OPTIONS¶
-q, --quietBe quiet, only report errors.
EXAMPLES¶
Undo add$ edit (1) $ git add frotz.c filfre.c $ mailx (2) $ git reset (3) $ git pull git://info.example.com/ nitfol (4)
$ git commit ... $ git reset --soft HEAD^ (1) $ edit (2) $ git commit -a -c ORIG_HEAD (3)
$ git branch topic/wip (1) $ git reset --hard HEAD~3 (2) $ git checkout topic/wip (3)
$ git commit ... $ git reset --hard HEAD~3 (1)
$ git pull (1) Auto-merging nitfol CONFLICT (content): Merge conflict in nitfol Automatic merge failed; fix conflicts and then commit the result. $ git reset --hard (2) $ git pull . topic/branch (3) Updating from 41223... to 13134... Fast-forward $ git reset --hard ORIG_HEAD (4)
$ git pull (1) Auto-merging nitfol Merge made by recursive. nitfol | 20 +++++---- ... $ git reset --merge ORIG_HEAD (2)
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.
1. This commit will get blown away so a throw-away log message is OK.
2. This removes the WIP commit from the commit history, and sets
your working tree to the state just before you made that snapshot.
3. 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
$ git checkout feature ;# you were working in "feature" branch and $ work work work ;# got interrupted $ git commit -a -m "snapshot WIP" (1) $ git checkout master $ fix fix fix $ git commit ;# commit with real log $ git checkout feature $ git reset --soft HEAD^ ;# go back to WIP state (2) $ git reset (3)
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.
1. This removes the file from the index while keeping it in the working
directory.
2. This commits all other changes in the index.
3. Adds the file to the index again.
Keep changes in working tree while discarding some previous commits
$ git reset -- frotz.c (1) $ git commit -m "Commit files in index" (2) $ git add frotz.c (3)
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.
1. This commits your first edits in branch1.
2. 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.
3. But you can use "reset --keep" to remove the unwanted commit
after you switched to "branch2".
$ git tag start $ git checkout -b branch1 $ edit $ git commit ... (1) $ edit $ git checkout -b branch2 (2) $ git reset --keep start (3)
DISCUSSION¶
The tables below show what happens when running:git reset --option target
working index HEAD target working index HEAD ---------------------------------------------------- A B C D --soft A B D --mixed A D D --hard D D D --merge (disallowed) --keep (disallowed)
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
working index HEAD target working index HEAD ---------------------------------------------------- X U A B --soft (disallowed) --mixed X B B --hard B B B --merge B B B --keep (disallowed)
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)
GIT¶
Part of the git(1) suite05/15/2017 | Git 2.11.0 |