Git - 在合并期间忽略文件

问题描述 投票:83回答:7

我在远程myrepo服务器上有一个名为beanstalk的仓库。

我把它克隆到我的本地机器上。创建了另外两个分支:stagingdev。将这些分支推向远程。

现在:

 local                   remote                   server
 --------------------------------------------------------  
 master  ==> Pushes to  `master`  ==> deployed to `prod`
 staging ==> Pushes to  `staging` ==> deployed to `staging`
 dev     ==> Pushes to  `dev`     ==> deployed to `dev`

我有一个名为config.xml的文件,每个分支都有不同的文件。

我想仅在合并期间忽略此文件。但是我希望在结帐或从/向repo分支提交时包含它。

我想要这个的原因是,我们有一个部署脚本,可以拉出(签出)特定分支并在各自的服务器上部署。所以我们需要特定分支的config.xml文件在部署时如上所示进入特定服务器。

我想.gitignore不会工作。还有什么其他选择?请注意,被忽略的文件应该是checkout和commit的一部分,这很重要。它只应在合并期间被忽略。

谢谢!

git merge push ignore repository
7个回答
72
投票

我通过使用带有--no-commit选项的git merge命令解决了这个问题,然后显式删除了暂存文件并忽略了对文件的更改。例如:说我想忽略对myfile.txt的任何更改我按如下方式进行:

git merge --no-ff --no-commit <merge-branch>
git reset HEAD myfile.txt
git checkout -- myfile.txt
git commit -m "merged <merge-branch>"

如果要有要跳过的文件列表,可以将语句2和3放在for循环中。


43
投票

我最终找到了git attributes。试试吧。到目前为止工作。尚未检查所有方案。但它应该是解决方案。

Merge Strategies - Git attributes


12
投票

.gitattributes - 是存储库的根级文件,用于定义子目录或文件子集的属性。

您可以指定该属性以告知Git对特定文件使用不同的合并策略。在这里,我们希望为我们的分支保留现有的config.xml。我们需要在merge=ours文件中将config.xml设置为.gitattributes

如果发生合并冲突,merge=ours告诉git使用我们的(当前分支)文件。

  1. 在存储库的根级别添加.gitattributes文件
  2. 您可以在.gitattributes文件中为confix.xml设置属性 config.xml merge=ours
  3. 然后用以下方法定义我们的虚拟合并策略: $ git config --global merge.ours.driver true

如果你合并stag形式dev分支,而不是让合并与config.xml文件冲突,stag branch的config.xml保留在你最初的任何版本。


5
投票

您可以首先使用git merge --no-commit,然后根据需要编辑合并,即通过取消暂存config.xml或任何其他文件,然后提交。我怀疑你想在使用钩子后进一步自动化它,但我认为值得手动至少进行一次。


3
投票

您可以使用.gitignoreconfig.xml保留在存储库之外,然后使用post commit hook将相应的config.xml文件上载到服务器。


1
投票

这里git-update-index - 将工作树中的文件内容注册到索引。

git update-index --assume-unchanged <PATH_OF_THE_FILE>

例:-

git update-index --assume-unchanged somelocation/pom.xml


0
投票

例:

  1. 你有两个分支:masterdevelop
  2. 您在develop分支中创建了文件,并希望在合并时忽略它

码:

git config --global merge.ours.driver true
git checkout master
echo "path/file_to_ignore merge=ours" >> .gitattributes
git merge develop

您也可以忽略具有相同扩展名的文件

例如,所有带有.txt扩展名的文件:

echo "*.txt merge=ours" >> .gitattributes
© www.soinside.com 2019 - 2024. All rights reserved.