36

I've just pulled a new branch, made some local changes, committed and tried to push. I was given this error: ! [rejected] groups -> groups (non-fast forward) So I tried a to pull but was told Already up-to-date.

Here's what I get pulling then pushing.

~/dev$ git pull origin groups
Already up-to-date.
~/dev$ git push origin groups
To /mnt/ebs/git/repo.git
 ! [rejected]        groups -> groups (non-fast forward)
error: failed to push some refs to '/mnt/ebs/git/repo.git'

Can anyone explain how this can be happening and how I can fix it?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Jake
  • 12,713
  • 18
  • 66
  • 96
  • @dan: -v doesn't give any more output other than the path to the repo again. `Pushing to /mnt/ebs/git/repo.git` – Jake Nov 30 '10 at 09:21
  • i'd look at the hashes of the heads of groups in both repositories they should match but if they didn't that would be the right error if they didn't and the remote wasn't a prefix of the local – Dan D. Nov 30 '10 at 09:26
  • The absolute best way for you to address this is to view the local and remote branches in gitk (`gitk groups origin/groups`), and see for yourself how they've diverged. You can directly see the history we have to try to infer from your question. – Cascabel Nov 30 '10 at 13:54
  • To make things trickier, this is on a server I access via SSH. I assume that means gitk is out of the question. – Jake Nov 30 '10 at 20:36
  • Nope! Make sure you've fetched everything (`git fetch origin`) and then fire up gitk. `origin/groups` is a remote branch, which is a local pointer to where it saw the branch on the remote pointing. – Cascabel Nov 30 '10 at 22:53
  • 1
    In my case I was trying to push my namedbranch to a different remote's `master` branch, the correct syntax is `git push myremote namedbranch:master`; if you don't specify the `localbranch:remotebranch`, or rather if you only specify the remote branch name it tries to push your local branch named what I expected to be the remote branch name. – ThorSummoner Jul 18 '14 at 16:16

3 Answers3

29

When you pulled the branch, did you use the "--track" option (in order to keep you local branch tracking the remote branch). If you did not, it can explain that the "merge" command that does not work.

You can do the merge manually:

git fetch
git merge origin/groups

To compare local and remote repos, I suggest you this command (add it in an alias, it is usefull):

git log --graph --oneline --all --decorate

It will print the project history tree, showing the branch labels. So you will see where your branch and the origin branch diverge.

Note: if you want to preserve a linear history, instead of a "merge", you can do a "rebase" of your local branch on the remote before pushing:

git rebase origin/groups
git push origin groups
Benoit Courtine
  • 7,014
  • 31
  • 42
  • I've never had any luck with --graph, I have the latest version of git as far as I know but it says it's an unrecognized argument. – Jake Nov 30 '10 at 20:18
  • 2
    Turns out I had a local branch named origin/groups as well as a remote one. – Jake Nov 30 '10 at 21:41
6

I came here with a different problem.

My git was set up to push all branches. I was on a branch FOO, but it was also trying to push master, which was not up to date. The trick was noticing it was trying to push master:

To git@git.machine:repo
 ! [rejected]        master -> master (non-fast-forward)

I added the following to my .gitconfig to only push the current branch by default:

[push]
    default = current
dfrankow
  • 20,191
  • 41
  • 152
  • 214
2

This is not an answer to the question asked. I had a different problem with the same error message.

My local branch did not have the remote branch to pull the changes from configured properly [git pull]. This was evident from the o/p of git remote show origin. So, I had to use git pull origin <branch_name> instead of git pull to pull the changes.

garnet
  • 551
  • 5
  • 12
  • Hi Garnet, this seems to be the same problem as mine. I wanna know what could be the solution to map my current branch with remote... – Mukesh Kumar Oct 26 '17 at 10:15
  • Hi Mukesh, I have not tried to add the mapping. I just specified the remote repo alias 'origin' and did the git pull as mentioned in the answer: `git pull origin ` instead of `git pull` to pull the changes. – garnet Oct 27 '17 at 08:40
  • Thanks Garnet for responding. That solution seems to be working for me. However, I need a command to configure it for git pull without origin. It should work as it used to. – Mukesh Kumar Oct 27 '17 at 08:52