Why does Git require me to pull before I push?

13,704

Solution 1

git telling you that you must first:

git fetch

and later

git add /commit /push

fetch is similar to pull but, pull merge the data in files in your local branch , fetch update only the branch structure and id..

if fetch does not work, it means that someone else committed to changing, and now your version must be upgraded before being released

Solution 2

You are asked to pull before you push, because someone pushed changes to the server, after your last pull, so our local copy and the current server copy are not in sync. Pulling will merge the remote copy with your local one, which brings them back into sync and allows you to push.

The reason that you are not allowed to push straight away, is that this will need merging and might lead to conflicts which need manual resolving. To avoid merging errors and conflicts, merging must always be done on the client side, never on the server.

Share:
13,704
Zack
Author by

Zack

Updated on July 23, 2022

Comments

  • Zack
    Zack almost 2 years

    I keep getting an error saying: rejected master-> master (fetch first), failed to push some refs.... because remote contains work you do not have locally.

    I just want git to overwrite the files currently in the repository with the new uploads so I've been trying to use git push -u origin master, but this error keeps popping up. I'm brand new to git/github. Why is this happening?

    I've tried to merge the existing files in the repo with the files on my desktop, but I keep getting merge conflicts. Not sure how to deal with these.