简体   繁体   中英

Can I exclude a file in a git repo from branching?

I have a file (specifically my .gitignore file) in the repo that I want to track and push to a central repository, so others will receive a copy when cloning the project.

But I want to exclude the file from being duplicated by branching. I want to have only one version stable in all branches, so when I change it in one, I don't have to commit and merge (or cherry-pick) this change to all other branches manually.

Is there a way to achieve this with git?

As there are several valid answers posted as comment, I'll self-answer here giving credit to Users Alexey Ten, wonce and Darryl.

Setting a file as 'stable' or 'pinned' between branches is probably not possible.

But in the special case of wanting the .gitignore file to be stable, this is most likely a case of wrong workflow.

Files should be included in the .gitignore file when they are generated, which has to happen in one branch eventually. Other branches will then not have these new files hence no need for a modified .gitignore. Once other branches also get the new files via merge or rebase, they should also get the modified .gitignore. Wanting to merge just the .gitignore file means you probably have added a file also present in another branch manually, which probably was bad practice as you should have created it with a merge from the original branch.

A notable exception to this rule is if you want the .gitignore file for a wildcard-rule you previously created in an other branch. Adding a new file matching the wildcard-rule but not necessarily being the same as the file in the original branch is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM