I have a scenario similar to what is asked in this question: Push local new Git repo to existing remote repo as branch?
The scenario is: I have a local repo A which I want to push to a remote repo B as a new branch. I do not want to merge, per se, but only create a new branch in the remote repo without losing history of repo A. Local repo A only has one branch called main.
The scenario looks as:
Local Repo A
Remote Repo B
Desired Result: Remote Repo B has branch named A
But the answer to this question (Push local new Git repo to existing remote repo as branch?) doesn't explain how to switch paths between repo A and repo B when executing the push.
I found another solution: How do you merge two Git repositories? (which has detailed steps listed in the answers) but I do not want to create a sub-directory but rather a branch from the local repo. I am thinking that there is some step that is missing from the following to make the repo A as a branch:
cd path/to/Repo-B
git remote add Repo-A /path/to/Repo-A
git fetch Repo-A --tags
git merge --allow-unrelated-histories Repo-A/master
git remote remove Repo-A
Is there some way I can accomplish moving local repo to remote repo as a new branch and not lose any history?
whether I execute the push while being present in repo A
Yes. You cannot push without being in repo A. There're two (rather similar) ways. 1st, you can manually cd
into required directory:
cd /path/to/A
git push
Or you can tell git
to do this:
git -C /path/to/A push
how do I tell git where repo B is?
Example setup: local repo A with the single branch master
. Remote repo B with URL http://host/user/repos/B
. You want to push master
from A to B as branch A
. Again, there're two similar ways. 1st, you can push directly to the URL:
git -C /path/to/A push http://host/user/repos/B master:A
(master:A
means "push local branch master
to remote branch A
"). If you gonna push only once this is the simplest way.
Or you can add a short alias (called "remote") and can use the alias to push many times:
cd /path/to/A
git remote add B http://host/user/repos/B
git push B master:A # <- push master:A to remote B