Bostonian
Bostonian

Reputation: 687

How to revert part of changes in the previous commit

In the previous git commit, I add a new file (lets call it C) and make some changes to file A and file B.

Is there a way to remove file C and also revert the change made to file A by git commit --amend?

Upvotes: 0

Views: 338

Answers (2)

tmaj
tmaj

Reputation: 35135

If not pushed

You can reset to the previous commit and attempt to do what you really intended again.

git reset HEAD~1
git add A B
git commit ...

If pushed

Since you're asking about git commit --amend I assumed the changes are local only. If the changes are already in the remote repo (aka pushed) then it may be better to remove C in a separate commit.

git rm C 
git commit -m "Removing C"

Upvotes: 1

Gavin H
Gavin H

Reputation: 10502

Sounds like you mostly want to revert the commit but keep changes to one file.

First, do a git revert but leave the changes staged:

git revert -n HEAD

Then unstage changes you don't want reverted (file B)

git reset HEAD -- B

And discard unstaged changes:

git checkout -- .

This should leave staged changes to revert changes to files A and C. Then you can commit as you wish (new commit or amending previous commit if it hasn't yet been pushed elsewhere).

git revert docs

Upvotes: 2

Related Questions