views:

1142

answers:

1

I recently committed a file to the HEAD of my branch which has errors in it. I need to do the following things:

  • Get that file from one commit previous to HEAD

  • Commit that file back into HEAD

What's the best way of going about that?

+6  A: 

You've practically said it yourself:

First get the file back from one commit before:

$> git checkout HEAD~1 path/to/file.ext

Then commit it:

$> git commit -a -m 'Retrieved file from older revision'

If only the changes to that file where present in the last commit, you can even use git-revert:

$> git revert HEAD

I think it would be better to make this a separate commit, because it tells you exactly what you've reverted, and why. However, you can squash this into the previous commit by using the --amend switch to git-commit.

sykora
--amend would be a switch for git commit, not git add
bdonlan
Oops, you're right. Fixed.
sykora
git-revert *would* make a separate commit (it was not entirely clear from your description). Using "git commit --amend" would correct last commit, instead of creating a new on (but you cannot do this if you published this part of history)
Jakub Narębski
@bdonlan Even if you choose to use --amend you will still need to add the new file using either 'git add the_file.c' or using both options: git commit -a --amend
Pat Notz