default_noob_network
default_noob_network

Reputation: 1315

Safe to git push without git fetch first? (or git pull)

I am finally starting to understand the fundamentals of git, but I just want to make sure I understand this particular part of it.

I've been using the git functionality inside Visual Studio, so I've been accustomed to the "sync" button. Ugh. Now I am doing everything from the command line, however I just want to be certain that when I push up to origin master I am doing things correctly. I am the only developer working in the remote so this is a low risk situation (relatively speaking).... but should I always at least git fetch before I push? Seems like I should have to to ensure I push into a remote repo that my local repo is update to date with. Right? What would happen if I pushed and remote / local were out of sync? Do I get an error?

Upvotes: 1

Views: 2261

Answers (2)

Roman
Roman

Reputation: 6657

You don't have to pull/fetch every time before you push. If your push conflicts with what is already in the remote repository, git will give you an error, like this:

$ git push
To [email protected]:your/repository.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:your/repository.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Upvotes: 2

filmor
filmor

Reputation: 32298

git will simply reject the push if you have incompatible changes.

Upvotes: 1

Related Questions