问题
I recently started using Git (previously I used subversion but I am now doing some collaborative work on a project that uses bitbucket and git).
All has been going well up until today when I find 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?
回答1:
If you have already made some commits, you can do the following
git pull --rebase
This will place all your local commits on top of newly pulled changes.
BE VERY CAREFUL WITH THIS: this will probably overwrite all your present files with the files as they are at the head of the branch in the remote repo! If this happens and you didn't want it to you can UNDO THIS CHANGE with
git rebase --abort
... naturally you have to do that before doing any new commits!
回答2:
I would do it this this way:
Stage all unstaged changes.
git add .
Stash the changes.
git stash save
Sync with remote.
git pull -r
Reapply the local changes.
git stash pop
or
git stash apply
回答3:
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
- Switched to the master branch (git checkout master)
- Did a pull on the master (git pull)
- Created new branch (git branch C) - note here that we are now branching from master
- Now when you are on branch C, merge with branch B (git merge B)
- 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.
回答4:
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!
回答5:
I was able to overcome this issue with the following Visual Studio 2017 change:
- 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.
- 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
来源:https://stackoverflow.com/questions/22532943/how-to-resolve-git-error-updates-were-rejected-because-the-tip-of-your-current