! [rejected] master -> master (fetch first)

后端 未结 24 2999
说谎
说谎 2020-12-02 04:15

Is there a good way to explain how to resolve \"! [rejected] master -> master (fetch first)\'\" in Git?

When I use this command $ git push or

24条回答
  •  半阙折子戏
    2020-12-02 04:26

    It's likely that someone else (e.g. your colleague) has put commits onto origin/master that aren't in your local master branch, and you are trying to push some commits from your local branch to the server. In 99% of cases, assuming you don't want to erase their work from origin, you have two options:

    2) Merge their changes into your local branch, and then push the merged result. git checkout master git pull # resolve conflicts here git push

    (Note that git pull is essentially just a git fetch and a git merge in this case.)

    1) Rebase your local branch, so that it looks like your colleague made their commits first, and then you made your commits. This keeps the commit history nice and linear - and avoids a "merge commit". However, if you have conflicts with your colleague's changes, you may have to resolve those conflicts for each of your commits (rather than just once) in the worst case. Essentially this is nicer for everyone else but more effort for you. git pull --rebase # resolve conflicts here git push

    (Note that git pull --rebase is essentially a git fetch and a git rebase origin/master.)

提交回复
热议问题