Fix "git push rejected (fetch first)" without losing work
Git refused your push because GitHub has commits you don't have yet. Nothing is broken and nothing is lost. Get their work first, then push again. The error You run git push and see something like this: ! [ rejected ] main - > main ( fetch first ) error: failed to push some refs to 'github.com:you/shop-app.git' hint: Updates were rejected because the remote contains work that you do not hint:…
Git rejected your push because the remote repository contains commits you don't have locally. This typically happens when another repository pushes to the same ref after you last pulled. It's not a broken system or lost work, but Git protecting the team by not letting your push replace commits you've never seen. To resolve this, first get the remote's work by running git fetch, which downloads new commits without altering your files.
Then, check which commits you're missing by running git log --oneline main..origin/main. This shows the commits in the remote that you don't have yet. Finally, merge these with your local branch using git pull or git pull --rebase. If you encounter merge conflicts, resolve them by keeping the appropriate lines and deleting the marker lines, then add and commit the fixed files.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.