[git] How to resolve git error: "Updates were rejected because the tip of your current branch is behind"

A well meaning colleague has pushed changes to the Master instead of making a branch. This means that when I try to commit I get the error:

Updates were rejected because the tip of your current branch is behind

I know this should be resolved by making a pull request to re-sync things but I don't want to lose the changes I have made locally and I equally don't want to force the commit and wipe out the changes made by someone else.

What is the correct approach to allow me to merge the changes without losing either?

This question is related to git bitbucket git-merge

The answer is


You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use

git push origin HEAD:<name-of-remote-branch>

I would do it this this way:

  1. Stage all unstaged changes.

    git add .
    
  2. Stash the changes.

    git stash save
    
  3. Sync with remote.

    git pull -r
    
  4. Reapply the local changes.

    git stash pop
    

    or

    git stash apply
    

I had this issue and I realized that .gitignore file is changed. So, I changed .gitignore and I could pull the changes.


This issue occurs when someone has commited the code to develop/master and latest code has not been rebased from develop/master and you're trying to overwrite new changes to develop/master branch

Solution:

  • Take a backup if you're working on feature branch and switch to master/develop branch by doing git checkout develop/master
  • Do git pull
  • You will get changes and merge conflicts occur when you have made changes in the same file which has not been rebased from develop/master
  • Resolve the conflicts if it occurs and do git push,this should work

This worked for me:

git branch

Copy the current branch name to clipboard

git pull origin <paste-branch-name>
git push

I was able to overcome this issue with the following Visual Studio 2017 change:

  1. In Team Explorer, go to Settings. Go to Global Settings to configure this option at the global level; go to Repository Settings to configure this option at the repo level.
  2. Set Rebase local branch when pulling to the desired setting (for me it was True), and select Update to save.

See: https://docs.microsoft.com/en-us/vsts/git/concepts/git-config?view=vsts&tabs=visual-studio#rebase-local-branch-when-pulling


I had the exact same issue on my branch(lets call it branch B) and I followed three simple steps to get make it work

  1. Switched to the master branch (git checkout master)
  2. Did a pull on the master (git pull)
  3. Created new branch (git branch C) - note here that we are now branching from master
  4. Now when you are on branch C, merge with branch B (git merge B)
  5. Now do a push (git push origin C) - works :)

Now you can delete branch B and then rename branch C to branch B.

Hope this helps.


I had the same problem. Unfortunately I was in wrong catalog level.

I tried to: git push -u origin master -> there was a error

Then I tried: git pull --rebase -> there still was a problem
Finally i change directory cd your_directory

Then I tried again ( git push) and it works!


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to bitbucket

How to markdown nested list items in Bitbucket? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? Bitbucket git credentials if signed up with Google What I can do to resolve "1 commit behind master"? Bitbucket fails to authenticate on git pull Change remote repository credentials (authentication) on Intellij IDEA 14 git: updates were rejected because the remote contains work that you do not have locally How do I push a local repo to Bitbucket using SourceTree without creating a repo on bitbucket first? Clone private git repo with dockerfile How to move git repository with all branches from bitbucket to github?

Examples related to git-merge

Abort a Git Merge Git pull - Please move or remove them before you can merge Git: How configure KDiff3 as merge tool and diff tool Git: How to pull a single file from a server repository in Git? How to resolve git error: "Updates were rejected because the tip of your current branch is behind" error: Your local changes to the following files would be overwritten by checkout Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How to merge specific files from Git branches git remove merge commit from history The following untracked working tree files would be overwritten by merge, but I don't care