0

I updated files and committed them, e.g.

git add *lots of files*
git add b.txt
git commit -m 'Update files'

Now I realize I actually want to make the changes to b.txt in a different commit. I want to remove the changes to b.txt from my commit, but I want to keep the changes in the filesystem. If b.txt were a new file, I could do this with

git rm --cached b.txt

But this doesn't work for reverting b.txt to its previous state in the staging area.

Basically, I want to check out directly into the staging area without modifying my local files. Is there a way to accomplish this with a single command in Git?

This question is not a duplicate of Reset or revert a specific file to a specific revision using Git? because that asker wants to change the working tree, but here I don't want to change the working tree.

AAudibert
  • 1,223
  • 11
  • 23
  • 1
    Hint: `git reset --mixed`. – Dai Oct 29 '18 at 22:31
  • Possible duplicate of [Reset or revert a specific file to a specific revision using Git?](https://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git) – codemonkeh Oct 29 '18 at 22:47
  • codemonkeh: That question says how to reset both the index and the working tree for a specific file/version. I'm asking how to update the index for a specific file/version *without* overwriting the working tree. Dai: `reset --mixed` is exactly what I was looking for, thank you! If you create an answer I'll accept it – AAudibert Oct 29 '18 at 23:45
  • In this case I used `git reset --mixed HEAD^ -- b.txt` – AAudibert Oct 29 '18 at 23:52
  • Actually `--mixed` isn't required because it's the default mode for `git reset` – AAudibert Oct 30 '18 at 00:03

4 Answers4

1

Basically, I want to check out directly into the staging area without modifying my local files. Is there a way to accomplish this with a single command in Git?

You can undo changes on a single file or directory from commit, but retain them in the unstaged state.

git reset commit-id <file>

fabfas
  • 2,200
  • 1
  • 21
  • 21
0

You can git checkout a specific file for a specific commit. For example, if the commit a12345 holds the changes of b.txt that you would like to revert to, you can run:

git checkout a12345 -- path/to/file/to/restore/b.txt

See the git checkout command for more info.

qgoehrig
  • 195
  • 1
  • 2
  • 15
0

What you can do it to create a new branch on your actual commit and then reset your master back to the previews state. Then your commit is in the new branch.

git branch new_branch
git reset --hard HEAD~1
git checkout new_branch

something like this.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

I'm assuming rewriting the history is fine for you. First, undo the commit by using reset --soft, which will keep the files added. Then unstage your file, and finally commit again:

git reset --soft HEAD~
git reset -- b.txt
git commit -C HEAD@{1}

(Committing with -C HEAD@{1} to keep the old commit message.)

alfunx
  • 3,080
  • 1
  • 12
  • 23