简体   繁体   English

还原 Git 中的更改并保留以后的更改集

[英]Reverting changes in Git and keeping later changesets

I'm unsure if I wrongly understood how the revert works or is it Visual Studio just doing something weird.我不确定我是否错误地理解了还原的工作原理,还是 Visual Studio 只是在做一些奇怪的事情。 Firstly I made the following commit inside of master branch首先,我在 master 分支中进行了以下提交

WriteNumbers(100, 2);

void WriteNumbers(int toWhere, int dividableByWhat)
{
    for (int i = 1; i <= toWhere; i++)
        if (i % dividableByWhat == 0)
            Console.WriteLine(i);
    Console.WriteLine();
}

Then I created new branch, switched to it and just added the new line as follows然后我创建了新分支,切换到它并添加了新行如下

WriteNumbers(100, 2);
WriteNumbers(100, 3);

void WriteNumbers(int toWhere, int dividableByWhat)
{
    for (int i = 1; i <= toWhere; i++)
        if (i % dividableByWhat == 0)
            Console.WriteLine(i);
    Console.WriteLine();
}

I merged this branch into master.我将这个分支合并到 master 中。 Afterwards I made another commit in master where I just added a new WriteNumbers(100, 4);之后我在 master 中做了另一个提交,我刚刚添加了一个新的WriteNumbers(100, 4); line.线。

Now from my understanding If I revert the changeset which introduced WriteNumbers(100, 3);现在根据我的理解,如果我还原引入WriteNumbers(100, 3); I should still have WriteNumbers(100, 4);我应该还有WriteNumbers(100, 4); in my file but that just doesn't seem to be case, at least in Visual Studio.在我的文件中,但似乎并非如此,至少在 Visual Studio 中是这样。 试图恢复变更集

错误的合并选项

As it can be seen when I run revert on changeset, I get option to either delete both lines (as was before I merged the second branch into master) or to keep both changes (which is also invalid state).正如我在变更集上运行恢复时所看到的,我可以选择删除这两行(就像我将第二个分支合并到 master 之前一样)或保留这两个更改(这也是无效状态)。 Is there any other way to just delete WriteNumbers(100, 3);有没有其他方法可以删除WriteNumbers(100, 3); line or I'm just doing something wrongly?行还是我只是做错了什么?

When you revert a commit which changes a line that was altered in later commits (or its surrounding lines were altered), then you get a merge conflict .当您还原一个提交,该提交更改了在以后的提交中更改的行(或其周围的行被更改),那么您会得到一个merge conflict You must resolve this conflict manually and that is what Visual Studio is asking you to do.您必须手动解决此冲突,而这正是 Visual Studio 要求您执行的操作。

Git nor Visual Studio can know which lines are the correct ones after the revert, so you have to pick. Git 和 Visual Studio 都无法知道还原后哪些行是正确的,因此您必须选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM