tags:

views:

309

answers:

3

The reason I am asking this is that I had accidentally done a git commit -a that included a file I did not yet want to commit. My solution was to do the following:

git reset --soft HEAD^
git reset -- file/with/changes_not_to_committed
git commit -C HEAD@{1}

Here, I’ve rewound the branch by one commit while keeping the index and working tree, then pulled file/with/changes_not_to_committed from a yet older revision into the index, and then I committed the index using the commit message from the previous branch head commit. Neither git-reset invocation touches the working copy, so my modifications to file/with/changes_not_to_committed persist, but are no longer recorded in the HEAD revision.

However, it would have been easier if I could pull file/with/changes_not_to_committed from the HEAD^ revision right into the index without touching the working copy. Since the index otherwise already represents the state I want, I could then just amend the HEAD commit, producing a sequence like this:

git magic-pony HEAD^ file/with/changes_not_to_committed
git commit --amend -C HEAD

This is almost what I would get by replacing git-magic-pony with git-checkout, except for the requirement that the working copy be left untouched and only the index updated. It seems there is no way to make git-checkout not touch both.

My question is: does git-magic-pony exist, and if so, under what name?

+1  A: 

It looks like you can do something like this using git update-index:

git update-index --cacheinfo 100644 5be7e154c284fb8de8ddca0bb18b72a73089df9b filename

You will need to get the mode (100644 above) and sha1 of the file you want to go back to (you can get this info all from git ls-tree). Then you can git commit --amend and your working copy will not be affected.

Greg Hewgill
+2  A: 
Aristotle Pagaltzis
+3  A: 

Right. When you want to move revisions from HEAD or another revision to the index, you use 'git reset REVISION -- file' - then, you'd use 'git commit --amend' to revise the commit. As it happens I'm currently working on a review aimed towards making it more obvious how files can be moved from A to B like that.

Of course a much easier way is to crank up 'git-gui', click the 'amend last commit' button, then click on the icon by the file in the commit inventory section of the gui, and then 'commit'.

Thanks! That’s exactly what I was looking for. I disagree about the GUI being easier, though. :-) More discoverable, perhaps.
Aristotle Pagaltzis