【问题标题】:Exclude certain changes (additions and deletions) to branch when merging, but keep tracking them in their branches合并时将某些更改(添加和删除)排除到分支,但在其分支中继续跟踪它们
【发布时间】:2013-10-24 18:42:29
【问题描述】:

我是 git 的新用户,但我以前使用过 tHg,并且我熟悉分支和合并的基本内容。 (比如说,直到使用 diff 工具并解决与它的冲突) 我正在使用 tortoiseGit 为我做我的工作,所以我可以有 Windows 资源管理器/shell 集成。我缺乏关于使用 tGit/git 进行合并的知识。 (或者更好的说法是:我以前从未尝试对任何 SCM 做这样的事情)

我已经从现有代码库启动了一个存储库。使用我的新 git 存储库,我已经分支了一个开发和一个测试分支。我开始回购的旧代码库已经包含了一些测试。

现在我想对这些测试进行版本控制,以便在测试被证明是错误的或其他情况时回滚测试分支中的内容。我认为只是忽略它们不会是解决方案,除非有办法在测试分支中仍然对它们进行版本控制。我想从 dev 分支中删除我的测试,因此我在 dev 分支中有一个(更)干净的项目文件结构,但将它们保留在 test 分支中。所以,如果我删除测试,当我更新代码进行测试时,删除应该从 dev 分支传播到 test 分支,对吧?这就是我不想发生的事情。有没有办法不传播删除?

那么,是否可以仅将一组选定的文件更改从开发推送回测试,反之亦然?如果是这样,我该怎么做?

【问题讨论】:

    标签: git version-control branching-and-merging


    【解决方案1】:

    当然:在测试分支上执行 -s ours 合并 dev-branch 提交,删除所有测试。但在我看来,您希望完全独立于源代码对测试进行版本控制。您可以将所有子模块都放在上面,但是对于这样的事情,只需将它们放在完全独立的分支中,然后合并到您的“测试”分支中:

    T---o---o-------o        tests (contains tests/*)
             \       \
              1---2---3      testing
             /   /   /
    D---o---o---o---o        dev   (contains all but tests/*)
    

    要启动测试分支,

    git checkout --orphan tests
    ls|grep -v tests|xargs git rm -r
    git commit -m"just the tests"
    

    做你的测试是

    git checkout testing  (#git checkout -B testing the first time)
    git merge dev tests
    # test test fix fix commit commit
    

    然后如果测试确实产生了修复修复提交部分,则选择性地合并回来:

    # selective merges from testing
    git checkout tests
    git merge -s ours --no-commit testing
    git checkout --theirs tests    # or however else you want to select fixes
    git commit
    
    git checkout dev
    git merge -s ours --no-commit testing
    git checkout --theirs path/to/fixed/this and/so/on   # likewise
    # or git checkout --theirs .; git rm -rf tests
    git commit
    

    或者您可以进行两步合并:

    # selective merges from testing
    
    git checkout -B from-testing testing
    # rm everything but tests/*
    git add -A; git commit
    git checkout tests
    git merge from-testing
    
    git checkout -B from-testing testing
    # rm tests/*
    git add -A; git commit
    git checkout dev
    git merge from-testing
    

    随着您对这类东西越来越熟悉,您将能够通过重置而不是检查来避免不必要的工作树操作,这是值得学习的大型项目。

    【讨论】:

    • 不错的答案!我将阅读你刚才提到的事情。你碰巧知道如何用 tortoiseGit 做你刚才提到的事情吗?我不是一个 cmd 用户。
    • 谢谢。重新 TG,我不知道,这可能是偏见,但我怀疑是否有任何超出 pidgin 使用范围的内容。
    猜你喜欢
    • 2021-12-24
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多