11

Despite pulling from the remote master and being told everything is up to date, I can't push to the remote master because the current branch is behind the tip of the remote. What?

$ git pull origin master
From https://github.com
 * branch            master     -> FETCH_HEAD
Already up-to-date.
$ git push origin master
To https://github.com
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
user3067865
  • 427
  • 7
  • 14

2 Answers2

3

You are probably on a different branch. git checkout master and try.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 2
    Be careful with this. git checkout master can overwrite your changes if they're not pushed – Ev. May 25 '14 at 23:33
  • Mano you might want to put a highlighted note to state what @Ev said. Cause all my work has gone down the drain.. – Gokigooooks Jul 20 '16 at 09:03
  • This question deserves more up votes. Like others have said, you should probably backup your new code before running this command. – Mikey Sep 04 '17 at 10:24
0

You ARE probable on a different branch. You probable should back up your changes. Since folks are losing their changes, they need to always save to local repository, then save from the local repo to the remote repository. Merges are done from inside the target branch, which requires you to have saved your work to your source dev branch before merging with master.. Here are the steps.

is a git command [ is something you need to change ]

First know what branch you are on..

  1. git branch
    will tell you what branch you are on.

Then you need to check your code into your Dev branch so you don't lose it.

  1. git status
    list the files that have changed
  2. git add [path filename]
    add all the files which have changed just copy the path and file names from the status command you want to add.
  3. git commit -m ["comment"]
    inserts your files to your local repository with a comment saying what you changed. The files inserted will the ones you added.
  4. git push origin [branch]
    push your local changes from your local repository to remote repository for your dev branch.

Now your local and remote repositories are all in order.... If you want to update Master branch, You can safely check out that branch and merge from inside of master. If you are doing pull requests you'll probable want to create a new branch from master and merge with that. Then do the pull request on that new branch.

But if you aren't using pull requests and want to update master directly to your dev branch. First you check out master.

  1. git checkout master
    This will delete your local code but you have saved it both locally and to the remote server.. so you're good.

  2. git merge [dev branch]
    This will merge your dev code into master. If there are any conflicts then open the file which conflicted and search for the '>>>' string to see where the conflict is. resolve the conflict by correcting the code. Then check your changes in locally and then from the local repository to the remote repository.

  3. git status
    See what changed

  4. git add master
    Add the modified files into the update
  5. git commit -m "[comment what changed]"
    update the local repository
  6. git push origin [branch]
    update the remote repository.. branch will be master here unlesss you are synking to a new branch in order to do pull request on it.
JMS
  • 181
  • 2
  • 6