I am consider this question: Revert changes to a file in a commit
Can I just run git checkout HEAD^ -- <file>
and then git add <file>
and git commit
to revert the change to a file in a commit?
Thanks.
I am consider this question: Revert changes to a file in a commit
Can I just run git checkout HEAD^ -- <file>
and then git add <file>
and git commit
to revert the change to a file in a commit?
Thanks.
Yes, but note that git checkout
first writes the file from the <tree-ish>
argument to the index, then copies the updated index copy of the file to the work-tree:
git checkout HEAD^ -- <file>
Now <file>
is the same in the index and work-tree, and matches the version in the commit found by HEAD^
.
Hence:
and then
git add <file>
is unnecessary, since this just copies the work-tree version into the index.1 You can just do the commit.
1The work-tree version already matches the index, and Git skips the copying. This is true even if you have modified a set of clean and/or smudge filters, or changed line-ending settings. In that particular case, where you want to force Git to re-clean the file on its way from work-tree to index, you must either trick Git into thinking that the work-tree file has changed:
touch <file>
or use the --renormalize
option of git add
.
Yes, you can do something like this:
git checkout HEAD -- <path>
git reset <path>
git add <path>
git commit