Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Git want me to pull before I push?

Tags:

git

github

I was setting up a new repository on GitHub and was trying to push to it but Git kept giving me an error saying:

error: failed to push some refs to...

I eventually tried pulling first and then pushing and that worked. But why?

like image 886
paulducsantos Avatar asked Nov 01 '15 02:11

paulducsantos


People also ask

Why do I have to git pull before push?

Always Pull Before a PushDoing so will ensure that your local copy is in sync with the remote repository. Remember, other people have been pushing to the remote copy, and if you push before syncing up, you could end up with multiple heads or merge conflicts when you push.

Can I git push without pulling?

Force Pushing Git prevents you from overwriting the central repository's history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need to pull the remote branch and merge it into your local one, then try pushing again.

Why do we perform git pull first?

The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.

Should I git pull before commit?

If you have uncommitted changes, the merge part of the git pull command will fail and your local branch will be untouched. Thus, you should always commit your changes in a branch before pulling new commits from a remote repository.


2 Answers

You mentioned that you were creating a new repository.

While the answer's and comments are also true, it is likely you're the only person interacting with the repository. You were required to pull because you initialized the repository with a README on GitHub (this is likely the tutorial you followed).

If you did not initialize the repository with a README, meaning GitHub didn't make a first commit of 'README.md', you would have a completely empty repository you can directly push to.

The reason why GitHub has that option is most likely to assist users who are starting a new project (such as yourself) to very easily get going after setting up a repository on GitHub by doing a pull/clone and having that initial commit in, allowing you to quickly add new files and push.

Additionally, by initializing a repository with a README, you'll have a master branch ready to clone and start tracking files. While on a completely empty repository you will receive notifications from Git like:

warning: You appear to have cloned an empty repository.

Without initializing, you will also later have to push your first commits the first time explicitly to master with git push origin master, as Git will politely tell you:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
Everything up-to-date

To summarize, it was that first commit (see your commits and you'll see the first README commit) that prevented you from pushing without pulling as your local repository is not in sync with the repository on GitHub.

like image 199
matrixanomaly Avatar answered Oct 01 '22 14:10

matrixanomaly


That's so that any parallel edits made by someone else and then checked in are brought into your development environment for merging and testing. In this way, the number of parallel, non-integrated changes is kept to a manageable minimum

like image 28
Chris Gerken Avatar answered Oct 01 '22 12:10

Chris Gerken