Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to move a branch between repositories

Tags:

git

github

Here is my situation: I have cloned a master branch from a repository and created a local branch to make the changes. After a few days working I tried to push this local branch I have created so the team could also work on it. Then I realized that I don't have the permission to push to this repository, so I created a fork and a branch so I can push my changes.

Now I want to move this branch to my fork and keep it saved in the same place. The really ideal scenario would be just point this fork branch to the exiting location.

I'm using sourcetree and bitbucket

How can I do that?

Thanks for any help

like image 359
André Luiz Avatar asked Jun 10 '16 15:06

André Luiz


1 Answers

Git is a distributed version control system. So you can have the same repo in multiple locations. When you first clone a repo locally, you have your own copy with a "remote" called origin (see it typing git remote -v). You simply need to add the second fork you created as a second remote:

git remote add other [email protected]:foo/bar.git

In the previous command "other" is just a name for the remote, so later you can do: git push other master (push my local master to other's master) or git push other local_branch:remote_branch

The possibilities are endless, you can pull/push from any remote's branches to any local branches.

Git has a book available for free hosted on their site: https://git-scm.com/book/en/v2. It's a great start for everyone, it was for me a while back.

like image 174
Leonel Galán Avatar answered Oct 25 '22 21:10

Leonel Galán