ZakTaccardi
ZakTaccardi

Reputation: 12467

Move file change from one commit to the working copy

Let's say I just made commit1 that had two changes:

Then I realized I made a mistake - I wasn't ready to commit my fileB.txt changes! I want the fileB.txt modifications to be in my working copy, and I want to amend the previous commit1 to only have fileA.txt modifications. So the final outcome is:

commit1

working copy

What's the best way to achieve this? Is it possible to do this without changing branches?

Upvotes: 2

Views: 390

Answers (2)

Mark Jeronimus
Mark Jeronimus

Reputation: 9543

I found a quite simple method (with the help of the other answer), which doesn't need to recreate the commit message.

git reset HEAD~ -- fileB.txt

Now the positive changes are in the working directory and the negative changes are staged (i.e. in the index). So the working directory isn't changed.

git commit --amend --no-edit

Negative changes are pushed, in other words, the file is removed from the commit but it's still in the working directory.

That's it.

Upvotes: 0

eftshift0
eftshift0

Reputation: 30212

Sure..... do this:

git reset --soft HEAD~1 # asking git to move the branch _pointer_ one revision back.... files won't change, both files will end up on index
git reset fileB.txt # take fileB out of index
git commit -m "Here´s the revision, only fileA is affected"

And now fileB is modified on the working tree. Another way would be:

git checkout HEAD~ -- fileB.txt # get fileB as it was before
git commit --amend --no-edit
git checkout HEAD@{1} -- fileB.txt # get fileB from the previous broken revision
git commit -m "some comment"

That should work. (Use the revision ID on the second commit if you don't feel confident of using the reference with HEAD@. The revision ID can be seen in git reflog)

Upvotes: 4

Related Questions