简体   繁体   中英

How to apply a single git commit in one repository to another repository?

I have several git repositories which contain configuration files for Linux systems. Sometimes I need the same change to be applied to all repositories. How can I automate this?

You can add multiple remotes to a single git repository, then do anything you want including cherry-picking and then push whatever wherever you want.

For example - let's make two repositories (both bare and "working" ones to simulate real scenario):

$ git init --bare a
$ git init --bare b
$ git clone a a-work
$ ( cd a-work && echo 'This is repo A' >README.txt && git add README.txt && git commit -am 'First in A' )
$ ( cd a-work && echo 'This is some important file' >important.txt && git add important.txt && git commit -am 'Important file' )
$ ( cd a-work && git push )
$ git clone b b-work
$ ( cd b-work && echo 'This is repo B' >README.txt && git add README.txt && git commit -am 'First in B' )
$ ( cd b-work && git push )

Now you can add both repos as remotes, fetch from them, cherrypick some commit and push it to any other repo:

$ git init both
$ cd both
$ git remote add a ../a
$ git remote add b ../b
$ git fetch --all
$ git log --all --oneline --decorate --graph
* d9225a1 (a/master) Important file
* 2fb3ae7 First in A
* bdeae08 (b/master) First in B
$ git checkout -b b-master b/master
$ git cherry-pick d9225a1
$ git log --all --oneline --decorate --graph
* b338dd3 (HEAD, b-master) Important file
* bdeae08 (b/master) First in B
* d9225a1 (a/master) Important file
* 2fb3ae7 First in A
$ git push b HEAD:master

Visualisation of the structure in repo both , better visible that commits are not linear there because multiple separate repos were fetched there:

在此处输入图片说明

Then when you go back to b-work :

$ cd ../b-work
$ git pull
$ git log --oneline --graph --decorate --all
* b338dd3 (HEAD, origin/master, master) Important file
* bdeae08 First in B

The file cherry-picked from repo A is there.

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