问题
I'm pretty new to using revert
in git, and I'm wondering if something like the following can be done. Let's say I have made a few commits already, and I have a (self-explanitory) text file which looks like the following:
This line was added at commit AAAAA
This line was added at commit BBBBB
This line was added at commit CCCCC
I would like to use revert to remove the changes made by commit BBBBB, to get a file that looks like this:
This line was added at commit AAAAA
This line was added at commit CCCCC
However, reverting commit BBBBB results in a merge conflict. Is there any way to avoid the conflict?
回答1:
However, reverting commit BBBBB results in a merge conflict. Is there any way to avoid the conflict?
To find out exactly what's causing the conflict, do
git checkout --merge --conflict=diff3 -- path/to/file
which will show you the original and both changed versions of the text git's unwilling to automerge.
What boils down to is, git is merging the change from the reverted commit to its parent with the change from the reverted commit to the current version:
bbbbb verson -> aaaaa version,
and
bbbbb version -> cccccc version (+ whatever else)
and the change hunks overlap, so, git's not going to automerge.
As a practical matter, given the amount of merging being done in several huge projects, you can be sure any automerging git refuses to do produces bad results too often to tolerate.
回答2:
if you want to revert changes only on single file, assuming, that you want to revert part of the last commit, you can just checkout the version of file from previous commit
git checkout HEAD~1 directory/path/file.txt
git add directory/path/file.txt
git commit -m "directory/path/file.txt reverted"
Basically git checkout HEAD .
reset all files from working directory or only specified directories or only single files, to specified state of given commit.
Also if you want to revert one directory to state of 3 commits ago, you can:
git checkout HEAD~3 some/directory/
来源:https://stackoverflow.com/questions/31493383/git-revert-old-commit-on-single-file