<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-reset(1)">
<articleinfo>
    <title>git-reset(1)</title>
        <indexterm>
                <primary>git-reset(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-reset - Reset current HEAD to the specified state</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git reset</emphasis> [--mixed | --soft | --hard | --merge] [-q] [&lt;commit&gt;]
<emphasis>git reset</emphasis> [-q] [&lt;commit&gt;] [--] &lt;paths&gt;&#8230;</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Sets the current head to the specified commit and optionally resets the
index and working tree to match.</simpara>
<simpara>This command is useful if you notice some small error in a recent
commit (or set of commits) and want to redo that part without showing
the undo in the history.</simpara>
<simpara>If you want to undo a commit other than the latest on a branch,
<xref linkend="git-revert(1)"/> is your friend.</simpara>
<simpara>The second form with <emphasis>paths</emphasis> is used to revert selected paths in
the index from a given commit, without moving HEAD.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
--mixed
</term>
<listitem>
<simpara>
        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.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--soft
</term>
<listitem>
<simpara>
        Does not touch the index file nor the working tree at all, but
        requires them to be in a good order. This leaves all your changed
        files "Changes to be committed", as <emphasis>git-status</emphasis> would
        put it.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--hard
</term>
<listitem>
<simpara>
        Matches the working tree and index to that of the tree being
        switched to. Any changes to tracked files in the working tree
        since &lt;commit&gt; are lost.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--merge
</term>
<listitem>
<simpara>
        Resets the index to match the tree recorded by the named commit,
        and updates the files that are different between the named commit
        and the current commit in the working tree.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-q
</term>
<listitem>
<simpara>
        Be quiet, only report errors.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;commit&gt;
</term>
<listitem>
<simpara>
        Commit to make the current HEAD. If not given defaults to HEAD.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_examples">
<title>Examples</title>
<variablelist>
<varlistentry>
<term>
Undo a commit and redo
</term>
<listitem>
<literallayout>$ git commit ...
$ git reset --soft HEAD^      <co id="CO1-1"/>
$ edit                        <co id="CO1-2"/>
$ git commit -a -c ORIG_HEAD  <co id="CO1-3"/></literallayout>
<calloutlist>
<callout arearefs="CO1-1">
<simpara>
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".
</simpara>
</callout>
<callout arearefs="CO1-2">
<simpara>
Make corrections to working tree files.
</simpara>
</callout>
<callout arearefs="CO1-3">
<simpara>
"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.
</simpara>
<simpara>See also the --amend option to <xref linkend="git-commit(1)"/>.</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Undo commits permanently
</term>
<listitem>
<literallayout>$ git commit ...
$ git reset --hard HEAD~3   <co id="CO2-1"/></literallayout>
<calloutlist>
<callout arearefs="CO2-1">
<simpara>
The last three commits (HEAD, HEAD^, and HEAD~2) were bad
and you do not want to ever see them again.  Do <emphasis role="strong">not</emphasis> do this if
you have already given these commits to somebody else.  (See the
"RECOVERING FROM UPSTREAM REBASE" section in <xref linkend="git-rebase(1)"/> for
the implications of doing so.)
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Undo a commit, making it a topic branch
</term>
<listitem>
<literallayout>$ git branch topic/wip     <co id="CO3-1"/>
$ git reset --hard HEAD~3  <co id="CO3-2"/>
$ git checkout topic/wip   <co id="CO3-3"/></literallayout>
<calloutlist>
<callout arearefs="CO3-1">
<simpara>
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.
</simpara>
</callout>
<callout arearefs="CO3-2">
<simpara>
Rewind the master branch to get rid of those three commits.
</simpara>
</callout>
<callout arearefs="CO3-3">
<simpara>
Switch to "topic/wip" branch and keep working.
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Undo add
</term>
<listitem>
<literallayout>$ edit                                     <co id="CO4-1"/>
$ git add frotz.c filfre.c
$ mailx                                    <co id="CO4-2"/>
$ git reset                                <co id="CO4-3"/>
$ git pull git://info.example.com/ nitfol  <co id="CO4-4"/></literallayout>
<calloutlist>
<callout arearefs="CO4-1">
<simpara>
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.
</simpara>
</callout>
<callout arearefs="CO4-2">
<simpara>
Somebody asks you to pull, and the changes sounds worthy of merging.
</simpara>
</callout>
<callout arearefs="CO4-3">
<simpara>
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.
</simpara>
</callout>
<callout arearefs="CO4-4">
<simpara>
Then you can pull and merge, leaving frotz.c and filfre.c
changes still in the working tree.
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Undo a merge or pull
</term>
<listitem>
<literallayout>$ git pull                         <co id="CO5-1"/>
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard                 <co id="CO5-2"/>
$ git pull . topic/branch          <co id="CO5-3"/>
Updating from 41223... to 13134...
Fast forward
$ git reset --hard ORIG_HEAD       <co id="CO5-4"/></literallayout>
<calloutlist>
<callout arearefs="CO5-1">
<simpara>
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.
</simpara>
</callout>
<callout arearefs="CO5-2">
<simpara>
"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.
</simpara>
</callout>
<callout arearefs="CO5-3">
<simpara>
Merge a topic branch into the current branch, which resulted
in a fast forward.
</simpara>
</callout>
<callout arearefs="CO5-4">
<simpara>
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.
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Undo a merge or pull inside a dirty work tree
</term>
<listitem>
<literallayout>$ git pull                         <co id="CO6-1"/>
Auto-merging nitfol
Merge made by recursive.
 nitfol                |   20 +++++----
 ...
$ git reset --merge ORIG_HEAD      <co id="CO6-2"/></literallayout>
<calloutlist>
<callout arearefs="CO6-1">
<simpara>
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.
</simpara>
</callout>
<callout arearefs="CO6-2">
<simpara>
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.
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Interrupted workflow
</term>
<listitem>
<simpara>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.</simpara>
<literallayout>$ git checkout feature ;# you were working in "feature" branch and
$ work work work       ;# got interrupted
$ git commit -a -m "snapshot WIP"                 <co id="CO7-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  <co id="CO7-2"/>
$ git reset                                       <co id="CO7-3"/></literallayout>
<calloutlist>
<callout arearefs="CO7-1">
<simpara>
This commit will get blown away so a throw-away log message is OK.
</simpara>
</callout>
<callout arearefs="CO7-2">
<simpara>
This removes the <emphasis>WIP</emphasis> commit from the commit history, and sets
    your working tree to the state just before you made that snapshot.
</simpara>
</callout>
<callout arearefs="CO7-3">
<simpara>
At this point the index file still has all the WIP changes you
    committed as <emphasis>snapshot WIP</emphasis>.  This updates the index to show your
    WIP files as uncommitted.
</simpara>
<simpara>See also <xref linkend="git-stash(1)"/>.</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
Reset a single file in the index
</term>
<listitem>
<simpara>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.</simpara>
<literallayout>$ git reset -- frotz.c                      <co id="CO8-1"/>
$ git commit -m "Commit files in index"     <co id="CO8-2"/>
$ git add frotz.c                           <co id="CO8-3"/></literallayout>
<calloutlist>
<callout arearefs="CO8-1">
<simpara>
This removes the file from the index while keeping it in the working
    directory.
</simpara>
</callout>
<callout arearefs="CO8-2">
<simpara>
This commits all other changes in the index.
</simpara>
</callout>
<callout arearefs="CO8-3">
<simpara>
Adds the file to the index again.
</simpara>
</callout>
</calloutlist>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Junio C Hamano &lt;<ulink url="mailto:gitster@pobox.com">gitster@pobox.com</ulink>&gt; and Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
