Reputation: 57
I have 2 diffrent remotes
Each origin have a branch with same name. (branch_xxx)
How can i merge "origin1/branch_xxx" changes to "origin2/branch_xxx" using command line
Upvotes: 4
Views: 4367
Reputation: 1279
Check your remotes with git remote -v
. If a remote is missing, add with git add remotename
.
You need to have the tip or latest commit of both branches in your local machine. Run: git fetch --all
. Or, git fetch origin1 branch_xxx
and git fetch origin2 branch_xxx
.
Checkout to the remote branch you want to merge to: git checkout origin2/branch_xxx
. Note: now you're in a detached HEAD
state (you're not in a branch). Eventhough you checked out to a remote branch, you're still in your local machine.
Now create a branch that will track the remote branch: git checkout -b origin2-branch_xxx
and set it to track the remote: git branch --set-upstream-to origin2/branch_xxx
. To check if a local branch has already tracked a remote branch, run git branch -vv
.
Now you're in origin2-branch_xxx
which tracks origin2/main
, merge origin1/main
with git merge origin1/main
. Then push the merge to origin2
with git push
:
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin2 HEAD:branch_xxx
To push to the branch of the same name on the remote, use
git push origin2 HEAD
To choose either option permanently, see push.default in 'git help config'.
Oops.. for safety reason, you can't push directly if the local and the remote branch have different name. So, run git push origin2 HEAD:branch_xxx
instead.
Upvotes: 0
Reputation: 21908
You can't work directly on origin2/branch_xxx
which is not a local branch but a mere image of the last position of branch_xxx
last time you fetched from origin2
(what's called a remote-tracking branch).
What you can do is work on a local copy (3 steps) :
1) Create a local copy of origin2/branch_xxx which will receive the merge
git checkout -b branch_xxx origin2/branch_xxx
2) Then merge origin1/branch_xxx into it (for the source branch no need for a copy)
git merge origin1/branch_xxx
(and deal with potential conflicts as usual)
3) Finally, push the result to origin2
git push origin2 branch_xxx
Note : to have a better understanding of what are these remote-tracking refs and how to work with them, take a look at this excellent summation in the official doc.
Upvotes: 2