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

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

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

When I use this command $ git push origin master it display an error message.

! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:zapnaa/abcappp.git'

ベストアンサー1

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.

ただし、前者を要求されました。常に git を使用する場合でも、1) を常に選択してください。これは良い習慣だからです。

おすすめ記事