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?
5 Answers
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.
- 14,508
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 changes
- 1,493
-
11
-
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 begit checkout path/to/file/to/revertto clean up your working copy too. – dmnd Oct 16 '13 at 17:56 -
1Alternatively
git rm --cached path/to/file/to/revertto un-add file without deleting it from the tree. – Jan Hudec May 06 '14 at 07:45 -
Note that this will add all currently staged changes into the commit as well, so you might want to run
git restore --staged -- $(git rev-parse --git-dir)beforehand. See also https://stackoverflow.com/a/70197343 – xeruf Dec 02 '21 at 17:22
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.
- 1,035
-
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 May 06 '14 at 07:58
-
That one works! I didn't have succes with other options for that particular task. Thanks for the tip! – Serguzest Aug 21 '16 at 21:41
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)
- 129
-
-
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 May 06 '14 at 10:57
-
Yes, but this removes/deletes the file
bwhich was very possibly in index in previous commit. So it changes thebin the commit which is not what OP wants. – yurtesen Sep 26 '21 at 17:40
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.
- 723
- 2
- 6
- 11
-
what does this part do
git commit -C HEAD@{1}in particular the HEAD@{1} part? – gMale Dec 19 '20 at 00:57 -
2Commit using the commit message from the previous commit in the reflog, namely the commit we just undid. – Justin L. Dec 20 '20 at 01:03
... Then stash/delete b, re-commit.., here shouldn't the wordThenbeafter? ----amendafter stach/delete b, ... – Lenik Jan 05 '11 at 10:17