5

将一个分支合并到另一个分支时,可以定义合并策略,如

git merge release -X ours

当将发布分支中的内容合并到当前分支时,这将全局应用“我们的”策略。是否可以仅在一个特定文件上应用此策略,例如

git merge release -X docs/release-notes.md ours 

这样“我们的”策略仅在合并 docs/release-notes.md 文件时使用?或者有人知道实现这一目标的另一种选择吗?

4

3 回答 3

1

如果您查看帮助,我认为这是不可能的

       ours
           This option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree
           that do not conflict with our side are reflected to the merge result. For a binary file, the entire contents are taken
           from our side.

           This should not be confused with the ours merge strategy, which does not even look at what the other tree contains at
           all. It discards everything the other tree did, declaring our history contains all that happened in it.

您会看到没有可用选项的指示,而不是subtree可能的策略

       subtree[=<path>]
           This option is a more advanced form of subtree strategy, where the strategy makes a guess on how two trees must be
           shifted to match with each other when merging. Instead, the specified path is prefixed (or stripped from the beginning)
           to make the shape of two trees to match.
于 2013-04-26T08:00:50.797 回答
1

git 合并对跟踪内容的不同状态进行操作,而不是对单个文件进行操作。换个说法,git 没有看到提交涉及两个文件或提交涉及一个文件之间有任何根本区别。

因此,在一个干净的 git 工作流程中,人们期望提交和分支策略具有主题一致性。更具体地说,生成修复“主题”的提交比修复特定文件的提交更“混乱”。拥有包含针对特定“主题”完成的工作的分支而不是解决特定文件开发的分支更加“混乱”。

将这与您的问题联系起来,在不了解您的代码库或 git 策略的情况下,可能需要根据各个文件的状态以不同方式合并特定分支,这表明分支模型可能需要一些改进。

如果您想要发布分支中的更改而不是发布分支中 README.md 文件中的更改,那么可能需要以某种方式拆分发布中的 README.md 文件中的更改,以便您可以合并发布分支而无需合并对 ​​README.md 的更改。但是,鉴于该分支被称为发布,我想您根本不想非常努力地敲击它。

如果您经常需要从发布中合并但忽略 README.md 更改,您可以考虑设置一个并行分支来发布,如 release-noreadme。然后,您可以随心所欲地使用 release-noreadme,包括按摩 README.md 文件以满足您的需求,这样您就可以从 release-noreadme 合并,而不必担心在特定文件中的特定更改周围小心翼翼。

tl;dr:考虑让一个分支同步到发布,您可以在其中消除不需要的更改,然后从该分支合并而不是发布。

于 2013-04-26T08:50:51.327 回答
0

我在这个答案中回答了一个非常相似的问题

使用我在那里提出的 git-merge-file包装器,你可以做你想做的事情

git merge release 
git-resolve-conflict --ours docs/release-notes.md

这是我编写的包装器 bash 函数:

git-resolve-conflict() {
  STRATEGY="$1"
  FILE_PATH="$2"

  if [ -z "$FILE_PATH" ] || [ -z "$STRATEGY" ]; then
    echo "Usage:   git-resolve-conflict <strategy> <file>"
    echo ""
    echo "Example: git-resolve-conflict --ours package.json"
    echo "Example: git-resolve-conflict --union package.json"
    echo "Example: git-resolve-conflict --theirs package.json"
    return
  fi

  git show :1:"$FILE_PATH" > ./tmp.common
  git show :2:"$FILE_PATH" > ./tmp.ours
  git show :3:"$FILE_PATH" > ./tmp.theirs

  git merge-file "$STRATEGY" -p ./tmp.ours ./tmp.common ./tmp.theirs > "$FILE_PATH"
  git add "$FILE_PATH"

  rm ./tmp.common
  rm ./tmp.ours
  rm ./tmp.theirs
}
于 2016-08-31T11:40:37.977 回答