125

I have a local git repository which is a clone of a repository on github. Someone has forked the repository and made changes in a new branch on a new repository. I want to move this new branch into my repository (locally to work on it first before merging with the master).

I tried creating a new branch and then pulling from the forked repository but it complains because the new branch is a copy of the master branch as well as the local file changes, so it says

error: Your local changes to the following files would be overwritten by merge.

So how can I pull the branch in the other repository into a new branch on my local repository?

I hope that makes sense. If not, this is my repository: https://github.com/MatthewLM/cbitcoin

As you can see, someone has created a new repository with the branch "linuxBuild": https://github.com/austonst/cbitcoin/tree/linuxBuild

I want that branch on my local repository for MatthewLM/cbitcoin.

How can I do this?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Matthew Mitchell
  • 5,293
  • 14
  • 70
  • 122

4 Answers4

192

You need to make sure that git status shows that no unstaged changes exist in your local repository.
You can do this by first stashing your local changes and than pulling that branch. Afterward you can apply your stash.


If you want to re-create the branch structure of the fork in your local repository, you can do the following:

git remote add fork <url of fork>
git fetch fork <branch>
git checkout -b fork_branch fork/<branch>

This will create the local branch fork_branch with the same history like <branch> in the fork, i.e. fork_branch will branch off of your master where <branch> does in the fork. Additionally, your local branch will now track that branch in the fork, so you can easily pull in new changes committed in the fork.

I think you still need to make sure beforehand that your working copy doesn't contain any changes.

If you do not need to track the fork:

git remote remove fork
unional
  • 14,651
  • 5
  • 32
  • 56
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • OK, thanks. This would be great if I wanted my local changes and the commits not found in the branch from the fork. But what if I want the new branch to be exactly like it is on the fork repository? That includes removing any commits from my own repository after the fork happened for this new branch. So that way it would be as if the branch was made on my own repository rather than a forked one. – Matthew Mitchell Jan 17 '13 at 16:17
  • Well your answer is an acceptable solution for what I need to do, yet I would find it more straight-forward if I could pull a new branch into a repository. I'll wait a while to see what others say. – Matthew Mitchell Jan 17 '13 at 16:34
  • @MatthewMitchell: Please check update. Is that what you were looking for? – Daniel Hilgarth Jan 17 '13 at 17:11
  • Well I did use the method of merging it with another branch (I just merged it with the master) in the end, but in the case where I just want a branch exactly as it is from another repository, I'll try the fetch and checkout method you described. Thanks. – Matthew Mitchell Jan 17 '13 at 18:04
  • Used to migrate another the branch "gh-pages" from another repo! – Marcello DeSales May 05 '16 at 05:50
  • Does this solution allow me to download updates from the remote branch and how do you stop tracking the remote branch? – Dave Bergschneider May 01 '17 at 18:17
  • Thanks, this answer helped me as well. I ended up added a new remote as you suggested and rebased the branches in that against the master branch of the main repository. Worked out well after some rebase mistake repairs. I removed the temporary remote afterwards using SourceTree (not sure what the git command is, but it worked) and the repo looks very clean now. – emilast May 09 '17 at 21:25
  • what is the reset accomplishing here? if you haven't done anything on the new branch yet why is a reset necessary? – Joseph Garvin Dec 22 '21 at 01:57
74

Method without adding remote.

git checkout --orphan fork_branch
git reset --hard
git pull <url of fork> <branch>
Alex78191
  • 2,383
  • 2
  • 17
  • 24
5

I was able to do something similar using...

git switch -c new_branch
git pull https://github.com/<user>/<forked-repo>.git <branch>
Integralist
  • 495
  • 3
  • 7
1

If you want to fetch without tracking from (repository1 > branch1) to (repository2 > branch2),

  • cd repository2
  • git fetch repository1 branch1:branch2
FullStackCoder
  • 420
  • 4
  • 6