Fix "Updates were rejected because the remote contains work that you do not have locally"

Someone pushed commits to the branch after you last pulled, so pushing would erase their work. Run git pull to combine their commits with yours (resolving conflicts if any), then git push again.

Step by step

  1. Get your teammates' commits and merge them into yours:

    $ git pull
  2. If there are conflicts, resolve them, then:

    $ git add <file>
    $ git commit
  3. Push again:

    $ git push

The full message is ! [rejected] main -> main (fetch first) and hint: Updates were rejected because the remote contains work that you do not have locally. Avoid git push --force: it overwrites your teammates' commits.

Try it in the interactive sandbox →Practice “Rejected!” with a real repository and teammates, right in your browser.

Related guides