简体   繁体   中英

How to automatically keeping specific files in sync between git branches?

Is there a way for git to automatically manage sharing/syncing specific files between branches? eg. If I have branches: branch1 and master, and I change/commit file1 on branch1, it automatically updates/commits file1 with the same content on master (and vice versa). In essence, I want a certain set of files to always be in synch in both branches.

I've looked at git submodules, but the files that I want to keep in sync are in various locations (it's the majority of files in the project). I'm fairly new to git and I haven't been able to completely grasp what git subtree can do. Does git subtree allow what I need? (If so I can spend the time to learn it)

The other option I can think of is to to create a script that uses git-cherry-pick or checkout --patch to keep the files in sync. If I were to go this route, is cherry pick or patch a better option?

Git does not work on a per file basis. It works with patches on the complete repository.

You could commit changes in this file as a patch with only that file and cherrypick it in the other branch and vice versa. This could be done as a script. Or more complicated as a post commit hook.

But the first time the changes touch functionality in another file or if you commit more than that one file in such a sync patch you will have a mess.

Keep the sync manually will give you more comfort and savety but some dicipline is needed.

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