Git without fear · Chapter 21 of 28

Push rejected

By the end of this chapter you can get your commit onto GitHub when someone pushed before you, without losing their work and without a merge commit. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Teamwork
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Meera wants elaichi chai on the menu today. You add it, commit on main and push, and GitHub says no. Ten minutes earlier Ravi pushed the new opening hours (7 am, and Sundays too) to the same main.

Bunty's answer is "add --force". That replaces GitHub's main with yours, and Ravi's hours vanish from GitHub. A plain git pull, the other thing you might try, stops with a page of hints. What you know from the last chapter only works when GitHub has not moved since your last push.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Get your elaichi chai commit onto GitHub

    Your push is rejected, because Ravi pushed a commit first. Send yours without losing his.

    1. Run git push and read why GitHub rejects it.

    2. Bring Ravi's commit into your main with a pull that rebases (task 2).

    3. Push again with a plain git push.

    4. Do not use --force. It deletes Ravi's commit from GitHub.

    To pass: GitHub's main has your commit "Add elaichi chai to the menu" and Ravi's "Open at 7 am, and on Sundays". GitHub's menu.txt has Elaichi chai 35, and its hours.txt still has Sun 9 am to 9 pm.

  2. Task 2

    Put your commit on top of Ravi's, with no merge commit

    Put your commit on top of Ravi's, in one straight line.

    1. A plain git pull stops here. It asks you to choose: merge or rebase.

    2. Choose rebase: run git pull with the --rebase option.

    3. Run git log --oneline --graph. Your commit is on top of Ravi's.

    4. Then push.

    To pass: GitHub's main has no merge commit, and its two newest commits are your commit on top of Ravi's. Made a merge by mistake? Run lab reset and start again.

  3. Task 3

    Leave your main the same as GitHub's main

    At the end, your main and GitHub's main must be the same commit.

    1. Push everything you have committed.

    2. Run git status. It should say your branch is up to date with 'origin/main'.

    3. If it says ahead, push again. If it says behind, pull with --rebase and push again.

    To pass: your main, your origin/main and GitHub's main are all the same commit when you press Check.

  4. Task 4 · interview question

    Interview: fetch or pull? Name the one that only downloads

    Interview question: which Git command only downloads new commits, and does not change your branch?

    1. Think of the two commands. One only downloads. The other downloads and then joins the commits into your branch.

    2. Write the one that only downloads into answer.txt, starting with git.

    3. Name just that one command.

    To pass: answer.txt names the command that only downloads, and the word pull is nowhere in it.

What you will learn

  • A rejected push: what "fetch first" means, and why Git refuses.
  • git fetch: download GitHub's new commits without touching your branch or your files.
  • git pull --rebase: replay your unpushed commit on top of what GitHub has, so the history stays a straight line.

More in Teamwork

Build "Push rejected" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.