How to push remote1:master to remote2:branch123 safely

Viewed 31

Problem: I want to push branch from one remote to another remote. Here is what I have done so far:

note: I have one remote already let say origin1

  1. git remote add origin2 repolink.git
  2. git fetch origin2

now I have local origin2 and its branches.

Structure:

local
 master
remote
 origin1
  master
 origin2
  master
  branch123 

Require: I want to push local:master to orgin2:branch123. branch123 already have source files and commits.

option:1 origin2:branch123 last commit can be parent of local:master init commit. I guess that might be rebasing.
option:2 origin2:branch123 is forcefully replaced by local:master. In which case I will lose commits of origin2:branch123 which was branched by origin2:master. I don't know whether in this case local:master will conflict with origin2:master in doing so (option:2)

which option is safe. I prefer rebasing because origin2:branch123 last commit is almost identical to remote1:master init commit.
Please see image:
enter image description here

Please advise which option is safe or best and what commands are useful. I am a bit naive right now in version control management.

1 Answers

Here is what worked for me:

git push origin2 master:newbranch // master is origin1:master
git fetch origin2
git checkout newbranch // from origin2 i.e. copy of origin1:master
git checkout branch123 //make sure you don't have any staged or untracked files in branch123
// HEAD is on branch123
git rebase --onto newbranch
git push // this will push branch123 which is now have commits of newbranch as well but git will prompt to merge or rebase, I did rebase and origin2:branch123 HEAD moved to local:branch123 HEAD which was rebased onto origin2:newbranch. (one can search rebasing a remote branch etc. I did it in pycharm IDE so it prompted me two option merge or rebase.

Anyways. A bit of luck now I have one unrelated branch history origin1:master added to another branch origin2:branch123 which I can merge in origin2:main/master branch.

Related