Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

git - How to push remote1:master to remote2:branch123 safely

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.

question from:https://stackoverflow.com/questions/65862913/how-to-push-remote1master-to-remote2branch123-safely

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...