111
25
I have modified two files a
, b
in the last commit. But file b
should not be commited, what's the workflow to amend this?
111
25
I have modified two files a
, b
in the last commit. But file b
should not be commited, what's the workflow to amend this?
112
Update (couple of years later)
It's trivial to remove it from index only.
True: you can reset a file to its index content easily enough, as the more recent answer (written by Matt Connolly) suggests:
git reset HEAD^ path/to/file/to/revert
HEAD^
allows the file to access its content in the previous commit before the last one.
Then you can git commit --amend
, as I originally wrote below.
With Git 2.23 (August 2019), you might use the new git restore
command
git restore --source=HEAD^ --staged -- path/to/file/to/revert
shorter:
git restore -s@^ -S -- path/to/file/to/revert
Again, you then can git commit --amend
, as I originally wrote below.
Original answer (January 2011)
If this is your last commit (and you haven't pushed it anywhere), you can amend it:
(first stash or save b
)
git commit --amend
Then delete b, re-commit. Restore b and you're done.
--amend
Used to amend the tip of the current branch.
Prepare the tree object you would want to replace the latest commit as usual (this includes the usual -i/-o and explicit paths), and the commit log editor is seeded with the commit message from the tip of the current branch.
The commit you create replaces the current tip — if it was a merge, it will have the parents of the current tip as parents — so the current top commit is discarded.
66
git diff --name-only HEAD^
- (optional) use to list the files that changed in
the last commit.git reset HEAD^ path/to/file/to/revert
- to
reset the index to that last version, leaving the working copy
untouched. git commit --amend
- to amend that last commit to
include the index changes9Imho it's a much better answer then the accepted one. – mik01aj – 2013-06-08T07:11:23.120
1Note that you shouldn't use git commit -a --amend
(i.e. don't add files) for step 3, or you'll commit your working copy changes which are the edits you are trying to remove. An optional step 2.5 could be git checkout path/to/file/to/revert
to clean up your working copy too. – dmnd – 2013-10-16T17:56:16.283
1Alternatively git rm --cached path/to/file/to/revert
to un-add file without deleting it from the tree. – Jan Hudec – 2014-05-06T07:45:33.120
13
Alternatively if you are using git gui
, you just select the "Amend last commit" option, the added file appears in the "Staged" list, click on it's icon to move it to the "Unstaged" list and commit.
That one works! I didn't have succes with other options for that particular task. Thanks for the tip! – Serguzest – 2016-08-21T21:41:30.463
1@VonC: Editing and splitting patches is quite common operation for heavy git users, so the gui was designed to make it easy and is IMO best tool for it. – Jan Hudec – 2014-05-06T07:58:38.117
10
If you want to delete b from your last commit
git rm --cached b (will preserve the file in the working tree but remove it from the index)
git commit --amend
If you want to remove all changes to b in your last commit
(backup b)
(modify b to state before incorrect commit)
git commit --amend
(restore b)
git rm --cached
and do away with the backup/restore dance (-1). – Jan Hudec – 2014-05-06T07:46:08.780
Thanks for pointing that out. I wanted to share how I did this because this was the approach I felt most comfortable with even after reading the whole thread. – pingo – 2014-05-06T10:57:56.047
4
An alternative that doesn't require index hackery, but nonetheless preserves the old commit message:
$ git reset HEAD^
$ git add <all the files you want, excluding the one you don't want>
$ git commit -C HEAD@{1}
I like this because (a) it uses commands I regularly use, and (b) I can do git add -p
to figure out exactly what it is I want to commit.
... Then stash/delete b, re-commit..
, here shouldn't the wordThen
beafter
? ----amend
after stach/delete b, ... – Xiè Jìléi – 2011-01-05T10:17:40.823@谢继雷: in other words, save b first, then commit --amend, then restore? True. I have updated the answer. – VonC – 2011-01-05T11:21:12.310
With the power of git index, telling anybody to stash/save the file is just plain silly (-1). It's trivial to remove it from index only. – Jan Hudec – 2014-05-06T07:44:20.393
1@JanHudec True, I have edited the answer accordingly. I didn't follow that old answer as closely as I do on Stack Overflow. 99% of git questions on SU should be migrated on SO anyway. – VonC – 2014-05-06T07:51:33.423