1

I cloned a Git repo locally--call it A. This repo A has a branch named aaa. I created a new local branch--bbb. I then created a new GitHub repo (empty)--call it B--and pushed bbb to B. Next, I changed the origin of my local repository to point to B. I also then added a remote named "upstream" to my local repository that points to back to the original source, A. I set the "push" url of this upstream remote to be something bogus ("no-pushing") since I never want to accidentally push from my local repo back to A. I only want to pull from A into my local repo. enter image description here

During a transition period, some developers will be committing changes to A/aaa. Others are committing changes to B/bbb.

I want to be able to pull changes from A/aaa into my local bbb branch and then push those to B/bbb so that the code on B will have all of the changes from the team committing to A as well as all of the change from the team committing to B. I also want to pull changes from B/bbb into my local bbb branch. Essentially, I want to use my local repo as a bridge between the two temporarily until we stop using A at some future point.

What git commands can I run to accomplish this? (and what are each of the commands doing so I am not just blinding copying commands)

So far nothing I have tried has worked, and I think I must be missing some key element in my understanding.

Shawn
  • 8,374
  • 5
  • 37
  • 60
  • A local repository can have two or more origins. Why not just set both sources as the origin, `git pull` from both (resolving any conflicts manually), then `git push` back to `B`? – Obsidian Age Feb 22 '22 at 23:26
  • That is what I thought I did. I have [multiple remotes](https://stackoverflow.com/a/11690868/230055). I suspect it the branch name differences that are causing me to not see the changes from A after I do a `git pull`. – Shawn Feb 22 '22 at 23:35

2 Answers2

0

I want to be able to pull changes from A/aaa into my local bbb branch and then push those to B/bbb so that the code on B will have all of the changes from the team committing to A as well as all of the change from the team committing to B

You can make sure:

cd /path/to/local
git switch bbb
git branch -u A/aaa bbb
git push -u B bbb
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Ultimately, I removed the remote named "upstream" and re-added it with a different name just to repeat the steps from scratch. These are the commands I used that worked:

git remote add A_upstream <<url_to_git_repo>>
git remote remove upstream
git remote set-url --push A_upstream no-pushing
git fetch --all
git checkout bbb
git merge A_upstream/aaa
git pull origin
git push origin

The first command added a new remote for A. The next command removed the prior remote, and then in the next step I again set the push URL for the new remote to something bogus since I only want to pull from A. Next, the fetch --all got everything from A and B, and then I merged the changes from the aaa branch into my local bbb branch with the next command. The pull from origin ensured I was up to date with the commits in bbb, then the push to origin ensured the changes I had gotten from aaa went to B.

Shawn
  • 8,374
  • 5
  • 37
  • 60