【问题标题】:how to merge all files from one branch to another branch but except one file如何将一个分支的所有文件合并到另一个分支,但一个文件除外
【发布时间】:2020-10-07 07:18:02
【问题描述】:

假设,

一个分支:

file1,
No-add-another-branch
file3

另一个分支:

file1, 
file2, 
....

我想将一个分支中的所有文件合并到另一个分支中,但“No-add-another-branch”文件除外。

我可以将“No-add-another-branch”文件移动到某个地方并将一个分支合并到另一个分支,但我正在寻找一种快捷方式。或者 git方式。

【问题讨论】:

标签: git


【解决方案1】:
  1. 正常将一个分支合并到另一个分支(包括No-add-another-branch
  2. 阶段删除No-add-another-branch(使用git rm No-add-another-beanch
  3. 修改合并提交 (git commit --amend)

另一种方法是在合并之前删除临时分支中的文件。这会创建一个额外的提交,但会使合并更清晰,并且历史记录将更易于阅读。

  1. git checkout -b tmp OneBranch
  2. git rm No-add-another-branch 后跟 git commit -m 'Remove the file before merging
  3. git checkout AnotherBranchgit merge tmp
  4. git branch -d tmp

历史应该如下所示:

 o--o--o--o Another Branch
         /
        o Remove the file before merging
       /
o--o--o One Branch

如果您不想删除文件,而是希望保持文件不变,则可以将第一种方法的第 2 步替换为 git checkout AnotherBranch^ -- No-add-another-branch(即,恢复其合并前的先前版本),或替换第一种方法的第 2 步第二种方法是git checkout AnotherBranch -- No-add-another-branch,然后是git commit -m 'Make the file equal to Another Branch before merging'

【讨论】:

  • 我以为我们只能在“git commit -amend”之前添加文件
  • 您还可以删除、重命名或更改文件,或者更改提交消息。
  • 我刚刚提出了另一个问题:如果两个分支具有相同的文件名但不想合并它们怎么办。我的意思是合并两个分支中除相同文件名之外的所有内容
猜你喜欢
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2013-11-09
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 2010-12-15
相关资源
最近更新 更多