简体   繁体   English

GIT从remote1 / master获取本地分支,并推送到remote2 / legacy

[英]GIT Make local branch pull from remote1/master and push to remote2/legacy

I have seen that you can add a remote and state that it's pushurl is another remote. 我已经看到你可以添加一个遥控器并声明它的pushurl是另一个遥控器。

For example, if I set my origin as 例如,如果我将原点设置为

git remote add origin git@github.com:newrepo/newrepo.git

I can add a new remote and set its pushurl to my origin: 我可以添加一个新的遥控器,并将其pushurl设置为我的原点:

git remote add oldremote git@github.com:oldrepo/oldrepo.git
git remote set-url --push oldremote git@github.com:newrepo/newrepo.git

So if I create a branch branch1 from oldremote/branch1 所以,如果我创建一个分支branch1oldremote/branch1

git fetch oldremote branch1
git checkout -b branch1 oldremote/branch1

It will pull from oldremote/branch1 and push to origin/branch1 它将从oldremote/branch1并推送到origin/branch1

This works as expected. 这按预期工作。 Doing git pull pulls from oldremote (and says we're up to date) and pushes to origin . oldremotegit pull pulls(并说我们是最新的)并推送到origin Nice and easy. 好,易于。

Now I'm trying to create a branch that doesn't have the same name as its remote branch. 现在我正在尝试创建一个与其远程分支名称不同的分支。 Secifically, I want to track oldremote/master as local branch legacy , and have this local branch to push to origin/legacy . 确切地说,我想跟踪oldremote/master作为本地分支legacy ,并将此本地分支推送到origin/legacy

git fetch oldremote master
git checkout -b legacy oldremote/master

Now, this has two side effects. 现在,这有两个副作用。

First, doing git push doesn't push to origin/legacy . 首先,做git push并不会推动origin/legacy I have to do git push origin legacy . 我必须做git push origin legacy

Second, doing git pull doesn't just fetch and merge from oldremote/master . 其次,执行git pull不仅仅是从oldremote/master获取和合并。 It fetches every branch from oldremote . 它从oldremote获取每个分支。

TL/DR TL / DR

Is there a way to pull from a branch in one remote and push to another branch in another remote when branch names are different? 当分支名称不同时,有没有办法从一个遥控器中的分支拉出并推送到另一个遥控器中的另一个分支?

git config branch.legacy.remote oldremote
git config branch.legacy.pushremote origin
git config branch.legacy.merge refs/heads/master

git config --local -e should look like: git config --local -e应如下所示:

[remote "oldremote"]
    url = git@github.com:oldrepo/oldrepo.git
    fetch = +refs/heads/*:refs/remotes/oldremote/*
[remote "origin"]
    url = git@github.com:newrepo/newrepo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = oldremote
    merge = refs/heads/master
[branch "legacy"]
    remote = oldremote
    pushremote = origin
    merge = refs/heads/master

Note that the value of push.default should be unset or simple . 请注意, push.default的值应该是unset或simple As the manual says, simple has become the default in Git 2.0. 正如手册所说, simple已经成为Git 2.0中的默认设置。 If its value is not simple or unset, git push may fail. 如果它的值simple或未设置, git push可能会失败。

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

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