Index

Copy a branch between Git repositories

Git is tricky to use: after 4 years I still have a hard time figuring out how to do simple operations with it. I just spent 30 minutes on that one:

Say you have 2 copies of the same repository repo1 & repo2. In repo1 there’s a branch called copybranch that you want to copy to repo2 without merging it: just copy the branch. git pull repo1 copybranch from repo2 doesn’t work because it will try to merge the copybranch into the current branch: no good.

It looks like git fetch repo1 copybranch would be the way to go, but when I did it, here’s what I saw:

From repo1
 * branch            copybranch -> FETCH_HEAD

After that a quick look at the logs doesn’t show copybranch, FETCH_HEAD, or any of the commits from copybranch. What happenned? Git copied the content of copybranch, but instead of creating another branch named copybranch it creates a temporary reference called FETCH_HEAD, but FETCH_HEAD doesn’t appear in the logs. In summary: Git copied the branch, & made it invisible, because you know… it makes perfect sense to hide what you just copied.

So how do you copy the branch, and create a branch with the same name referencing the commits? Here it is:

git fetch repo1 copybranch:copybranch