Git - 在合并期间忽略文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15232000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git - Ignore files during merge
提问by Kevin Rave
I have a repo called myrepo
on the remote beanstalk
server.
我myrepo
在远程beanstalk
服务器上调用了一个 repo 。
I cloned it to my local machine. Created two additional branches: staging
and dev
.
Pushed these branches to remote as well.
我把它克隆到我的本地机器上。创建了两个额外的分支:staging
和dev
. 也将这些分支推送到远程。
Now:
现在:
local remote server
--------------------------------------------------------
master ==> Pushes to `master` ==> deployed to `prod`
staging ==> Pushes to `staging` ==> deployed to `staging`
dev ==> Pushes to `dev` ==> deployed to `dev`
I have a file called config.xml
which is different on each branch.
我有一个名为的文件config.xml
,它在每个分支上都不同。
I want to ignore this file only during merges. But I want this to be included when I checkout or commit from / to the repo branch.
我只想在合并期间忽略此文件。但是我希望在我从 / 到 repo 分支结帐或提交时将其包含在内。
The reason I want this is, we have a deploy script that pulls (checkout) the specific branch and deploys on the respective servers. So we need config.xml
file of that specific branch go into the specific server as indicated above when deployed.
我想要这个的原因是,我们有一个部署脚本,可以拉取(签出)特定分支并部署在相应的服务器上。所以我们需要config.xml
该特定分支的文件在部署时进入特定服务器,如上所示。
I guess .gitignore
wont work. What are the other options? Note that the ignored file should be part of checkout and commit, which is important. it should be ignored only during merges.
我想.gitignore
行不通。其他选项是什么?请注意,被忽略的文件应该是 checkout 和 commit 的一部分,这很重要。它应该只在合并期间被忽略。
Thanks!
谢谢!
采纳答案by Kevin Rave
I ended up finding git attributes
. Trying it. Working so far. Did not check all scenarios yet. But it should be the solution.
我最终找到了 git attributes
。正在尝试。工作至今。还没有检查所有场景。但它应该是解决方案。
回答by unmesh-gurjar
I got over this issue by using git merge command with the --no-commit
option and then explicitly removed the staged file and ignore the changes to the file.
E.g.: say I want to ignore any changes to myfile.txt
I proceed as follows:
我通过使用带有--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>"
You can put statements 2 & 3 in a for loop, if you have a list of files to skip.
如果您有要跳过的文件列表,则可以将语句 2 和 3 放入 for 循环中。
回答by eigenharsha
.gitattributes
- is a root level file of your repository that defines the attributes for a subdirectory or subset of files.
.gitattributes
- 是存储库的根级文件,用于定义子目录或文件子集的属性。
You can specify the attribute to tell Git to use different merge strategies for a specific file. Here, we want to preserve the existing config.xml
for our branch.
We need to set the merge=ours
to config.xml
in .gitattributes
file.
您可以指定属性来告诉 Git 对特定文件使用不同的合并策略。在这里,我们希望config.xml
为我们的分支保留现有的。我们需要在文件中设置merge=ours
to 。config.xml
.gitattributes
merge=ours
tell git to use our(current branch) file, if merge conflict occurs.
merge=ours
如果发生合并冲突,告诉 git 使用我们的(当前分支)文件。
Add a
.gitattributes
file at the root level of repositoryYou can set up an attribute for confix.xml in
.gitattributes
fileconfig.xml merge=ours
And then define a dummy ours merge strategy with:
$ git config --global merge.ours.driver true
.gitattributes
在存储库的根级别添加一个文件您可以在
.gitattributes
文件中为 confix.xml 设置一个属性config.xml merge=ours
然后定义一个虚拟的我们的合并策略:
$ git config --global merge.ours.driver true
If you merge stag
form dev
branch, instead of having the merge conflicts with config.xml file, stag branch's config.xml preserve at whatever version you originally had.
如果您合并stag
表单dev
分支,而不是与 config.xml 文件发生合并冲突,stag 分支的 config.xml 会保留您最初拥有的任何版本。
回答by gcbenison
You could start by using git merge --no-commit
, and then edit the merge however you like i.e. by unstaging config.xml
or any other file, then commit. I suspect you'd want to automate it further after that using hooks, but I think it'd be worth going through manually at least once.
您可以从使用开始git merge --no-commit
,然后编辑您喜欢的合并,即通过取消暂存config.xml
或任何其他文件,然后提交。我怀疑你会想在使用钩子之后进一步自动化它,但我认为至少手动一次是值得的。
回答by tlehman
You could use .gitignore
to keep the config.xml
out of the repository, and then use a post commit hookto upload the appropriate config.xml
file to the server.
您可以使用.gitignore
将其保留在config.xml
存储库之外,然后使用提交后挂钩将适当的config.xml
文件上传到服务器。
回答by Sireesh Yarlagadda
Here git-update-index - Register file contents in the working tree to the index.
这里 git-update-index - 将工作树中的文件内容注册到索引。
git update-index --assume-unchanged <PATH_OF_THE_FILE>
Example:-
例子:-
git update-index --assume-unchanged somelocation/pom.xml
git update-index --assume-unchanged somelocation/pom.xml
回答by Sole Sensei
Example:
例子:
- You have two branches:
master
,develop
- You created file in
develop
branch and want to ignore it while merging
- 你有两个分支:
master
,develop
- 您在
develop
分支中创建了文件并希望在合并时忽略它
Code:
代码:
git config --global merge.ours.driver true
git checkout master
echo "path/file_to_ignore merge=ours" >> .gitattributes
git merge develop
You can also ignore files with same extension
您也可以忽略具有相同扩展名的文件
for example all files with .txt
extension:
例如所有带有.txt
扩展名的文件:
echo "*.txt merge=ours" >> .gitattributes