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

后端 未结 24 2858
说谎
说谎 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:17

    The reason it happened in my case was when creating the GitHub rep link, I initialized it with README file

    While creating Git remote do not initialize it with README file otherwise it would show err

    Don't do that & it will definitely work fine Instead initialize it with the readme file if you wish to after pushing to the master branch

    0 讨论(0)
  • 2020-12-02 04:18

    Its Simple use this command:

    git push -f origin master

    and it will get your work done

    0 讨论(0)
  • 2020-12-02 04:19

    This worked for me, since none of the other solutions worked for me. NOT EVEN FORCE!

    https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line

    Just had to go through Git Bash

    cd REPOSITORY-NAME
    git add .
    git commit -m "Resolved merge conflict by incorporating both suggestions."
    

    Then back to my cmd and I could: git push heroku master which in my case was the problem.

    0 讨论(0)
  • 2020-12-02 04:20

    --force option worked for me I used git push origin master --force

    0 讨论(0)
  • 2020-12-02 04:23

    The answer is there, git is telling you to fetch first.

    Probably somebody else has pushed to master already, and your commit is behind. Therefore you have to fetch, merge the changeset, and then you'll be able to push again.

    If you don't (or even worse, if you force it by using the --force option), you can mess up the commit history.

    EDIT: I get into more detail about the last point, since a guy here just gave the Very Bad Advice of using the --force option.

    As git is a DVCS, ideally many other developers are working on the same project as you, using the same repository (or a fork of it). If you overwrite forcefully with your changeset, your repository will mismatch other people's, because "you rewrote history". You will make other people unhappy and the repository will suffer. Probably a kitten in the world will cry, too.

    TL;DR

    1. If you want to solve, fetch first (and then merge).
    2. If you want to hack, use the --force option.

    You asked for the former, though. Go for 1) always, even if you will always use git by yourself, because it is a good practice.

    0 讨论(0)
  • 2020-12-02 04:24

    Use this command:

    git push -f origin master --force
    
    0 讨论(0)
提交回复
热议问题