World map

 

BM: Baca dalam Bahasa Melayu

World 11: Rescue Center

My push was rejected

You will learn: how to fix a rejected push without forcing it.

The problem

git push: main -> main is rejected (fetch first). The hint says the remote has work you do not have locally. Terminal output: $ git push To https://github.com/you/diary.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/you/diary.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

You type git push. GitHub says rejected. Did you break something? No! This happens to everyone. Here’s the safe way out.

Try it

What is GitHub telling you?

Pick one answer.

Watch

git fetch brings Aisyah's commit. origin/main and your main now split into two lines from Add style.
6d3a9b1a8feb63270a76e8d0c56d

git fetch shows it: Aisyah pushed a commit. Your main and GitHub’s main went separate ways.

Try it

Put your commit on top of Aisyah’s. Then push again.

6d3a9b1a8feb63270a76e8d0c56d

Check

Good move after a rejected push, or needs work? Swipe each one.

Swipe right for good, left for needs work. Or use the buttons.

1 / 4git pull --rebase
git pull --rebase

You did it!

You can now fix a rejected push without forcing it.

Go deeper (optional)

Plain git pull may ask how to join the two lines. Pick --rebase or --no-rebase. Or set it once: git config --global pull.rebase true. Did the pull stop with a conflict? See “Messy pull: abort”. Never use --force on a shared branch.