887

Say that someone created a branch xyz. How do I pull the branch xyz from the remote server (e.g. GitHub) and merge it into an existing branch xyz in my local repo?

The answer to Push branches to Git gives me the error "! [rejected]" and mentions "non fast forward".

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
tybro0103
  • 48,327
  • 33
  • 144
  • 170
  • 4
    what is the actual command you're running? – Alex N. Nov 10 '09 at 16:20
  • 1
    It is fetch that can fail with 'non fast forward' message. Did you modify remote-tracking branch (origin/xyz), or was the branch rewound / rewritten in remote repository? Youmight need to use "`git fetch origin --force`", but please read documentation before doing it. – Jakub Narębski Nov 10 '09 at 18:46
  • The remote could be, for example, a github URL, with a selected branch.. (see comment to Cabri's answer) – Philip Oakley Feb 12 '22 at 16:09

16 Answers16

1023

But I get an error "! [rejected]" and something about "non fast forward"

That's because Git can't merge the changes from the branches into your current master. Let's say you've checked out branch master, and you want to merge in the remote branch other-branch. When you do this:

$ git pull origin other-branch

Git is basically doing this:

$ git fetch origin other-branch && git merge other-branch

That is, a pull is just a fetch followed by a merge. However, when pull-ing, Git will only merge other-branch if it can perform a fast-forward merge. A fast-forward merge is a merge in which the head of the branch you are trying to merge into is a direct descendent of the head of the branch you want to merge. For example, if you have this history tree, then merging other-branch would result in a fast-forward merge:

O-O-O-O-O-O
^         ^
master    other-branch

However, this would not be a fast-forward merge:

    v master
O-O-O
\
 \-O-O-O-O
         ^ other-branch

To solve your problem, first fetch the remote branch:

$ git fetch origin other-branch

Then merge it into your current branch (I'll assume that's master), and fix any merge conflicts:

$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit    # And commit the merge!
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • No, the problem is with fetching, not with merge step. – Jakub Narębski Nov 10 '09 at 22:02
  • 3
    Normally, remotes are set up such that fetches are forced, even if they don't result in a fast-forward commit, so it shouldn't occur on fetch unless the OP changed something with the usual configuration. The fast-forward issue can occur during fetch *or* merge. What makes you say that the problem is definitely in fetching, and not in merging? – mipadi Nov 10 '09 at 23:31
  • I follow these steps (fetch, merge). Git tells me there's nothing to do. When I try to commit, it falls over moaning about fast-forwards. – Jean Jordaan Nov 08 '11 at 17:08
  • 1
    @mipadi I had same issue as Jean and, while I can't say the remote is setup in the non default way you've mentioned I can say using `git fetch -f` have fixed my issue! Thanks! – cregox Feb 28 '12 at 13:50
  • 4
    This merges remote branch `xzy` into local branch `master`, which is not what was implied by the original question; "How can I pull branch xyz from GitHub and merge it into branch xyz on my localhost?" – user5359531 Mar 10 '17 at 21:04
  • just FYI and my future reference as well, "git pull --rebase origin other-remote-branch" also works if you want to rebase. – nishantbhardwaj2002 Sep 13 '19 at 12:21
  • What is you don't want to merge? – Brendan Metcalfe Jan 08 '21 at 15:18
377

Simply track your remote branches explicitly and a simple git pull will do just what you want:

git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name

The latter is a local operation.

Or even more fitting in with the GitHub documentation on forking:

git branch -f new_local_branch_name upstream/remote_branch_name
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
innaM
  • 47,505
  • 4
  • 67
  • 87
208

A safe approach is to create a local branch (i.e. xyz) first and then pull the remote branch into your locals.

# create a local branch
git checkout -b xyz

# make sure you are on the newly created branch
git branch

# finally pull the remote branch to your local branch
git pull origin xyz

Here is the syntax that could pull a remote branch to a local branch.

git pull {repo} {remotebranchname}:{localbranchname}

git pull origin xyz:xyz
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45
Robert Cabri
  • 3,821
  • 1
  • 20
  • 17
  • 2
    Perfect! I just didn't know that syntax: git pull {repo} {remotebranchname}:{localbranchname}. Question, if that pull doesn't work (maybe someone's updated the branch and there would be merge conflicts) what are my options? – Costa Michailidis Oct 29 '13 at 14:18
  • 2
    Phrogz - looks like this behavior changed in recent versions of Git. I used this before and it worked perfectly well. – Pawan Jan 10 '17 at 13:08
  • The answer is seriously misleading!!! I applied the answer without noticing @Phrogz 's comment and now suffering from the problem s/he mentioned. Either which branch it tries to merge should be expressed explicitly or the answer should be deleted !!!! – buraky Jan 25 '21 at 08:48
  • @Phrogz,@Letitbe which command did you try? I can change the answer accordingly. Never had the issue of pulling it into master. – Robert Cabri Feb 12 '21 at 15:18
  • The `{repo}` can also be a URL, so for quick looks at some other repo it can be nicer than having to create a remote first (stick the fetch/pull command in the new branch description;-) – Philip Oakley Feb 12 '22 at 16:07
  • @Phrogz the answer was adjusted, it now has 'git branch' to make sure the person is on the right branch. Like that when doing 'git pull' it will pull to that branch and not master, right? – Robert Sinclair Dec 29 '22 at 13:27
119

The best way is:

git checkout -b <new_branch> <remote repo name>/<new_branch>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mohit
  • 1,913
  • 3
  • 16
  • 23
  • 2
    After creating a new "dev" branch on github, and trying the above, I got the following error message: "fatal: origin/dev is not a commit and a branch 'dev' cannot be created from it" Solution was to "git fetch" per Bradley Flood's solution below, and then re-running mohit's answer. – TomEberhard Oct 30 '18 at 13:51
65

git fetch will grab the latest list of branches.

Now you can git checkout MyNewBranch

Done :)


For more info see docs: git fetch

Bradley Flood
  • 10,233
  • 3
  • 46
  • 43
44

I am not sure I fully understand the problem, but pulling an existing branch is done like this (at least it works for me :)

git pull origin BRANCH

This is assuming that your local branch is created off of the origin/BRANCH.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex N.
  • 14,805
  • 10
  • 46
  • 54
  • 2
    This will pull the branch into your current local branch, so when you are on `dev` and to `git pull origin feature` it will pull the feature branch into `dev`. The person who asked the question wants to make a new branch `feature` and pull into this branch – Jonas Sep 06 '21 at 13:51
  • 3
    but first you need to do `git checkout -b xyz` then you do `git pull origin xyz` – milos Apr 09 '22 at 23:30
24

Simply put, If you want to pull from GitHub the branch the-branch-I-want:

git fetch origin
git branch -f the-branch-I-want origin/the-branch-I-want
git checkout the-branch-I-want
Adrien Renaud
  • 2,439
  • 18
  • 22
18

This helped me to get remote branch before merging it into other:

git fetch repo xyz:xyz
git checkout xyz
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
17

for pulling the branch from GitHub you can use

git checkout --track origin/the-branch-name

Make sure that the branch name is exactly the same.

9

I did

git branch -f new_local_branch_name origin/remote_branch_name

Instead of

git branch -f new_local_branch_name upstream/remote_branch_name

As suggested by @innaM. When I used the upstream version, it said 'fatal: Not a valid object name: 'upstream/remote_branch_name''. I did not do git fetch origin as a comment suggested, but instead simply replaced upstream with origin. I guess they are equivalent.

Vicky
  • 271
  • 3
  • 6
8

These work for me.

  1. To pull a specific remote branch to the current local branch you are in. (Where <remote_repo> is the remote repository and <remote_branch> is the specific remote branch you want to pull)
git pull <remote_repo> <remote_branch>

e.g.

git pull origin remote_master
  1. To pull a specific remote branch to a specific local branch. (Where <local_branch> is the specific local branch you want to pull into)
git pull <remote_repo> <remote_branch>:<local_branch>

e.g.

git pull origin remote_master:local_master
Banty
  • 532
  • 6
  • 10
6
git pull <gitreponame> <branchname>

Usually if you have only repo assigned to your code then the gitreponame would be origin.

If you are working on two repo's like one is local and another one for remote like you can check repo's list from git remote -v. this shows how many repo's are assigned to your current code.

BranchName should exists into corresponding gitreponame.

you can use following two commands to add or remove repo's

git remote add <gitreponame> <repourl>
git remote remove <gitreponame>
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
prathap
  • 61
  • 1
  • 2
5

you may also do

git pull -r origin master

fix merge conflicts if any

git rebase --continue

-r is for rebase. This will make you branch structure from

        v  master       
o-o-o-o-o
     \o-o-o
          ^ other branch

to

        v  master       
o-o-o-o-o-o-o-o
              ^ other branch

This will lead to a cleaner history. Note: In case you have already pushed your other-branch to origin( or any other remote), you may have to force push your branch after rebase.

git push -f origin other-branch
Praveen Kumar
  • 181
  • 2
  • 6
4

you can try with

git branch -a

or git fetch to get latest list of branches.

git checkout theBranch //go in to the branch

git checkout -b "yourNewBranch" // to work in your own branch in base "theBranch"

rnewed_user
  • 1,386
  • 7
  • 13
3

A simple solution to get new branch from remote to local

  1. fetching the remote branch

    git fetch xyz

  2. switch to that branch locally

    git switch xyz

  3. finally, check if xyz appears on your local branches by running

    git branch

Nivethan
  • 2,339
  • 19
  • 22
0

The Local User Needs to Update Both the Master Branch and the XYZ Branches Separately

None of these posts answers the original question!

How do I pull the branch xyz from the remote server (e.g. GitHub) and merge it into an existing branch xyz in my local repo? The answer to Push branches to Git gives me the error "! [rejected]" and mentions "non fast forward".

If you want to merge a remote xyz branch into a local xyz branch, where both branches exist off a master branch in a remote and local repository, just select or "checkout" the local xyz branch first, then do a "pull" on the same remote branch. Simple!

git checkout xyz
git pull origin xyz

But, the user got an error! That update did not work. Why?

If this is failing, it has nothing to do with the two xyz branches on remote and local repositories not being able to merge. It sounds like the master branch on the remote repository has been changed with new commits the local repository does not have. The fast-forward message likely means that the xyz branch on the remote repo cannot update the local repo's xyz branch until the local repo gets all the changes added to its master branch from the remote repo first.

These could be new commits or changes the other developer added to the remote master branch, who then ran a rebase on their xyz branch to move it to the end of those new commit HEADS in the remote master. The local repo user has no way to merge that change as its master branch is missing those new added commits at the end, so cannot update the xyz rebase change until its local master branch on its repo is updated with a merge via a pull, first.

So go ahead and update the local master with the remote master's updates first using a pull, then try your xyz branch pull again...

git checkout master
git pull origin

git checkout xyz
git pull origin xyz

Remember, a pull is just a fetch then merge from a remote repo to your local repo on whatever branch you are currently focused on.

Stokely
  • 12,444
  • 2
  • 35
  • 23