c++ - git 冲突解决文件并不总是代表源文件中的每一行吗?

标签 c++ git version-control conflict

我在一个包含三个分支的代码库中工作:master、stage 和 dev。我已经在 dev 上工作了一段时间,准备好推送我的更改,并且已经将我的本地提交压缩为一个提交以供发布。我想用团队成员推送到远程“阶段”分支的任何工作来更新我的本地存储库。我想:

  1. 将我在开发时推送到舞台上的任何更改 pull 过来
  2. 将代表我的工作的“dev”上的单个提交重新设置为“stage”
  3. 其他与出版相关的东西

(没有其他人提到我的本地开发分支,所以我很乐意 rebase 。)

我愿意:

git checkout stage
git pull
git checkout dev

情况是这样的:

分支 dev 上的 foo.cpp(过时)

...
if (conditional)
{
    Datastructure data = someValue;
    cout << "Some text" << endl;
    cout << "More text" << endl;
}

return 0; // rest of function NYI

if (someOtherConditional) {

    Bob bob = makeBob();
    Fred fred = makeFred();
    ...
}
...

分支阶段的 foo.cpp(最近)

...
if (conditional)
{
    Datastructure data = someValue;
    cout << "Some text" << endl;
    cout << "More text" << endl;

    cout << "Even more text" << endl;
    cout << "Lots of text" << endl;

    // todo: fix this hackish code
    if (anotherConditional) {
        myBool = true;
    }
}

if (yetAnotherConditional) {
    Bob bob = makeBob();
    Fred fred = makeFred();
    ...
}
...

最后,我这样做:

git rebase stage

我遇到了一些冲突,包括 foo.cpp。我打开 foo.cpp 并得到:

foo.cpp 在开始 rebase dev 到 stage 之后

...
if (conditional)
{
    Datastructure data = someValue;
    cout << "Some text" << endl;
    cout << "More text" << endl;

<<<<<<<< HEAD
    cout << "Even more text" << endl;
    cout << "Lots of text" << endl;

    // todo: fix this hackish code
    if (anotherConditional) {
        myBool = true;
    }
}
=======
return 0; // rest of function NYI

if (someOtherConditional) {
>>>>>>> mostRecentDevCommit

if (yetAnotherConditional) {
    Bob bob = makeBob();
    Fred fred = makeFred();
    ...
}
...

mostRecentDevCommit 中的第一个 if (conditional) { 匹配的丢失的右括号发生了什么?

为什么 if (yetAnotherConditional) { 在冲突解决部分之外 - 分支“dev”上的 foo.cpp 没有那个!

谢谢 - 我实际上不需要解决这个问题来解决冲突,但我想知道 Git 在这里做了什么。

最佳答案

对于这种情况,要做的事情就是检查原件,看看最初是什么让这两个替代大块头看起来像是一个最小的编辑。

为了将来引用,您可以请求“diff3”样式的冲突报告,它将在报告中包含原始 block ,根据特定文件的需要或作为默认值:

git -c merge.conflictstyle=diff3 checkout -m --paths# just these for now
git config --global merge.conflictstyle diff3 # or as global default

您可以通过检查您的引用日志来一次性重新运行任何 merge 或 rebase ,这里 git reflog dev 将包含一对

9d7143d dev@{1}: rebase finished: refs/heads/dev onto 0f30b87ba7a2499c2b5d87b17d26ea8d353b8e34
5e58907 dev@{2}: commit: -

用这些数字你会

git -c merge.conflictstyle=diff3 rebase dev@{2} 0f30b87
# (look at foo.cpp, if the conflict report is still confusing, post it?)
git rebase --abort
git checkout @{-1}     # <-- takes you back to your previous checkout

如果没有它,我能想到的最好的可能是(在两个替换中可能删除了更多行):

    }
    return 0;
}

void func(type parm ...)
{
    if (someOtherConditional) {

这似乎对所有三个大块头版本都是正确的。

关于c++ - git 冲突解决文件并不总是代表源文件中的每一行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130479/

相关文章:

git - 如何在本地 pull 远程分支?

windows - 什么是 Git 的好(免费)可视化 merge 工具? (在 window 上)

c++ - 如何在 Windows 中确定文件的创建日期?

git - 防止在 git 中提交带有特定于平台的非法字符的文件名

从类名转换的 C++ 指针类型

git - 使用 GPG 签署 git 提交

git - 在多用户环境中使用 git 决定应该实现什么、不应该实现什么

ios - 开发新版本的 iOS 应用程序。需要新的 bundle ID?

c++ - C++ 流中的无效输入

c++ - Julia 表演建议