Home > Net >  When pushing to remote repositories, what is the difference between these two rejection messages?
When pushing to remote repositories, what is the difference between these two rejection messages?

Time:11-21

  1. "the remote contains work that you do not have locally"
 ! [rejected]        feature/addition -> feature/addition (fetch first)
error: failed to push some refs to 'github.com:<github_username>/<reponame>.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.
  1. "The tip of your current branch is behind its remote counterpart"
 ! [rejected]        feature/addition -> feature/addition (non-fast-forward)
error: failed to push some refs to 'github.com:<github_username>/<reponame>.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

CodePudding user response:

TL;DR

  1. "The remote contains work that you do not have locally" means changes happened on the remote since you last fetched.

  2. "The tip of your current branch is behind its remote counterpart" means you've fetch changes from the remote but have not dealt with them yet.

In more details

They're closely related messages: in both cases, your local branch is not up to date with the remote branch, and you need to fix that in one of many ways: git pull, git pull --rebase, git fetch update your local branch in the way of your choosing. In the second case, just git merge or git rebase would be sufficient, too.

The difference is:

  • The first message says you haven't even fetched those new commits from the remote and your local origin/<branchname> remote tracking branch was behind. By just inspecting locally, you did not have the information about those new commits at all, and Git is telling you "wait a sec, something changed on the remote since you last looked!"

    A fetch or pull is required at this point, then you should decide how you want to combine your work and the new remote work.

  • The second message says that you did indeed fetch from the remote, and origin/<branchname> is in synch with the remote, but you did not merge the new commits into <branchname>. In this case, you had all the information locally, but you didn't deal with it yet.

    Maybe you want to merge, or rebase, or possibly even force push if you're sure that's what you need to do. But a fetch or pull is not actually required.

As @jmargolisvt said, a git pull will fix both of these cases, but my preferred approach is a git fetch followed by inspecting the new history with a graph log viewer, and then more often than not a rebase, rarely a merge. (I like my history to be mostly linear, and I tend to use merge commits for PRs but not much else.)

  • Related