简体   繁体   English

如何将一个存储库中的单个git commit应用于另一个存储库?

[英]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. 我有几个git存储库,其中包含Linux系统的配置文件。 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. 您可以将多个远程对象添加到单个git存储库中,然后执行您想要的任何操作(包括挑选),然后将所需的内容推送到任何地方。

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: 仓库中结构的可视化both可以看到,提交在那里不是线性的,因为在那里提取了多个单独的仓库:

在此处输入图片说明

Then when you go back to b-work : 然后,当您回到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. 那里是从仓库A挑选的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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