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?
A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.
Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.
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!
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
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
Now you can delete branch B and then rename branch C to branch B.
Hope this helps.
This worked for me:
git pull origin $(git branch --show-current)
git push
fyi git branch --show-current
returns the name of the current branch.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With