! [rejected] master -> master (fetch first) is a Git error which indicates that you need to fetch the repository first before committing to it.
In Git, sometimes you get the error –
! [rejected] master -> master (fetch first) error: failed to push some refs to '[email protected]:superhero/ironman.git'
(fetch first) shows that you can’t commit to this repository before fetching. This happens because Git is protective to version maintaining.
Suppose a number of contributors are working on your library and everybody is committing the work done by them. It is possible that the snapshot you have in your local system is older than that on the Git platform. So, if you try to commit then you may mess up with the latest changes.
That’s why Git forces you to keep the latest copy in your local system before making any changes. Also don’t ever use this command –
git push origin master --force
Never force your repository to merge with your commit because that may corrupt the git tree.
To safely resolve this error, run your commands in this sequence –
git fetch origin master git merge origin master git add . git commit -m 'your commit message' git push origin master