删除特定的提交 [英] Remove specific commit
问题描述
我正在和一个朋友一起工作,他编辑了一些本不应该被编辑的文件。不知何故,我把他的作品合并到我的作品中,无论是当我拉我的作品时,还是当我试图只挑选出我想要的特定文件时。我一直在寻找并玩了很长一段时间,试图找出如何删除包含对这些文件的编辑的提交,似乎是回退和rebase之间的折腾,并没有直接的例子,而且docs认为我知道的比我多。
I was working with a friend on a project, and he edited a bunch of files that shouldn't have been edited. Somehow I merged his work into mine, either when I pulled it, or when I tried to just pick the specific files out that I wanted. I've been looking and playing for a long time, trying to figure out how to remove the commits that contain the edits to those files, it seems to be a toss up between revert and rebase, and there are no straightforward examples, and the docs assume I know more than I do.
所以这里是问题的简化版本:
So here is a simplified version of the question:
下面的场景,我该如何删除提交2?
Given the following scenario, how do I remove commit 2?
$ mkdir git_revert_test && cd git_revert_test
$ git init
Initialized empty Git repository in /Users/josh/deleteme/git_revert_test/.git/
$ echo "line 1" > myfile
$ git add -A
$ git commit -m "commit 1"
[master (root-commit) 8230fa3] commit 1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 myfile
$ echo "line 2" >> myfile
$ git commit -am "commit 2"
[master 342f9bb] commit 2
1 files changed, 1 insertions(+), 0 deletions(-)
$ echo "line 3" >> myfile
$ git commit -am "commit 3"
[master 1bcb872] commit 3
1 files changed, 1 insertions(+), 0 deletions(-)
预期结果是
The expected result is
$ cat myfile
line 1
line 3
这里是我已经试图恢复的一个例子
Here is an example of how I have been trying to revert
$ git revert 342f9bb
Automatic revert failed. After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
and commit the result.
推荐答案
Git在计算diff时要使用的算法被还原要求
The algorithm that Git uses when calculating diff's to be reverted requires that
- 被恢复的行不会被任何稍后的提交所修改。
- 在历史后面的任何其他相邻提交。
相邻的定义基于来自一个context diff,它是3.所以如果'myfile'是这样构造的:
The definition of "adjacent" is based on the default number of lines from a context diff, which is 3. So if 'myfile' was constructed like this:
$ cat >myfile <<EOF
line 1
junk
junk
junk
junk
line 2
junk
junk
junk
junk
line 3
EOF
$ git add myfile
$ git commit -m "initial check-in"
1 files changed, 11 insertions(+), 0 deletions(-)
create mode 100644 myfile
$ perl -p -i -e 's/line 2/this is the second line/;' myfile
$ git commit -am "changed line 2 to second line"
[master d6cbb19] changed line 2
1 files changed, 1 insertions(+), 1 deletions(-)
$ perl -p -i -e 's/line 3/this is the third line/;' myfile
$ git commit -am "changed line 3 to third line"
[master dd054fe] changed line 3
1 files changed, 1 insertions(+), 1 deletions(-)
$ git revert d6cbb19
Finished one revert.
[master 2db5c47] Revert "changed line 2"
1 files changed, 1 insertions(+), 1 deletions(-)
然后这一切都按预期工作。
Then it all works as expected.
第二个答案非常有趣。还有一个功能尚未正式发布(尽管可以在Git v1.7.2-rc2中找到),称为Revert Strategy。你可以这样调用git:
$ b
The second answer was very interesting. There is a feature which has not yet been officially released (though it is available in Git v1.7.2-rc2) called Revert Strategy. You can invoke git like this:
git revert - 战略解决< commit>
git revert --strategy resolve <commit>
,它应该更好地确定你的意思。我不知道可用策略的列表是什么,也不知道任何策略的定义。
and it should do a better job figuring out what you meant. I do not know what the list of available strategies is, nor do I know the definition of any strategy.
这篇关于删除特定的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!