Git - Ignore files during merge

丶灬走出姿态 提交于 2019-11-26 08:46:23

问题


I have a repo called myrepo on the remote beanstalk server.

I cloned it to my local machine. Created two additional branches: staging and dev. Pushed these branches to remote as well.

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.

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.

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.

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.

Thanks!


回答1:


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:

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:


I ended up finding git attributes. Trying it. Working so far. Did not check all scenarios yet. But it should be the solution.

Merge Strategies - Git attributes




回答3:


.gitattributes - is a root level file of your repository that defines the attributes for a subdirectory or subset of files.

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.

merge=ours tell git to use our(current branch) file, if merge conflict occurs.

  1. Add a .gitattributes file at the root level of repository

  2. You can set up an attribute for confix.xml in .gitattributes file

    config.xml merge=ours
    
  3. And then define a dummy ours merge strategy with:

    $ 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.




回答4:


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.




回答5:


You could use .gitignore to keep the config.xml out of the repository, and then use a post commit hook to upload the appropriate config.xml file to the server.




回答6:


Here git-update-index - Register file contents in the working tree to the index.

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

Example:-

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




回答7:


Example:

  1. You have two branches: master, develop
  2. You created file in develop branch and want to ignore it while merging

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:

echo "*.txt merge=ours" >> .gitattributes


来源:https://stackoverflow.com/questions/15232000/git-ignore-files-during-merge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!