Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git "reset" workdir to HEAD without touching staging area

Tags:

git

Using the same table representation as in git-reset manual page, I have these states in my git repository:

working index HEAD
------------------
 B       B     A

What command will change the states to those?

working index HEAD
------------------
 A       B     A

I other word, I want to "reset" the working directory state to the HEAD state, but without touching the staging area state.

like image 753
David Froger Avatar asked Sep 09 '16 07:09

David Froger


2 Answers

As noted in this answer, there is now a native git command which does this:

git restore --source=HEAD --worktree -- .

See the manpage.

like image 139
Anton Tykhyy Avatar answered Oct 31 '22 13:10

Anton Tykhyy


I assume this should work (ordering matters).

You will first need to commit what is in the index (to make the HEAD look like this index and the working directory: B -- using your annotations):

git commit

So, the HEAD will be B (using your annotations).

Now, print the reflog as we will need the hash of B:

git reflog

Now, run a couple of reset commands with different options:

git reset --hard HEAD~ # makes the working directory, the index, and the HEAD looks like this: A, A, A (respectively)
git reset --mixed <hashOfB> # makes the working directory, the index, and the HEAD looks like this: A, B, B (respectively)
git reset --soft HEAD~ # makes the working directory, the index, and the HEAD looks like this: A, B, A (respectively)

I hope this helps.

like image 29
joker Avatar answered Oct 31 '22 12:10

joker