Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Update (couple of years later)

Jan Hudec

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.


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.

share|improve this answer
    
... Then stash/delete b, re-commit.., here shouldn't the word Then be after? -- --amend after stach/delete b, ... –  Xiè Jìléi Jan 5 '11 at 10:17
    
@谢继雷: in other words, save b first, then commit --amend, then restore? True. I have updated the answer. –  VonC Jan 5 '11 at 11:21
    
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 May 6 at 7:44
    
@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 May 6 at 7:51
  1. git diff --name-only HEAD^ - (optional) use to list the files that changed in the last commit.
  2. git reset HEAD^ path/to/file/to/revert - to reset the index to that last version, leaving the working copy untouched.
  3. git commit --amend - to amend that last commit to include the index changes
share|improve this answer
4  
Imho it's a much better answer then the accepted one. –  m01 Jun 8 '13 at 7:11
    
Note 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 Oct 16 '13 at 17:56
    
Alternatively git rm --cached path/to/file/to/revert to un-add file without deleting it from the tree. –  Jan Hudec May 6 at 7:45

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)
share|improve this answer
    
git rm --cached and do away with the backup/restore dance (-1). –  Jan Hudec May 6 at 7:46
    
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 6 at 10:57

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.

share|improve this answer
    
+1 Interesting alternative using the GUI. –  VonC May 6 at 7:50
    
@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 6 at 7:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.