简体   繁体   中英

Branches diverged after pull instead of push

I accidentally did git pull instead of git push and got a merge conflict. I was surprised that git tried a merge at all, because the remote branch didn't receive any commits since my last pull/push. Since I didn't want to do a pull/merge, I did git reset --hard followed by git merge --abort . I tried to push, but got the message:

On branch my_branch
Your branch and 'origin/my_branch' have diverged,
and have 17 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean

How can I get back to the state before the accidental pull to be able to push my local commits to the remote branch?

your remote did get a commit pushed. You can have a look at the git tree using this command:

git log --all --graph --decorate --oneline

maybe you did a git commit --amend or something like that on the one commit that is already on the remote. Analyse the output of the command above (and post it maybe in your question) to see which commit you want to keep.

Since you are working on your personal branch, you might consider (note that this is not ideal) to crush your (1) remote commit with your (17) local commits, using

git push -f

WARNING: in this case you will "lose" that one remote commit.

If you want to keep that one remote commit, you might consider first rebasing your local work onto that commit, before pushing

git rebase origin/my_branch
git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM