Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to pull before I can push in Git?

Tags:

git

If A and B are working on the same git repo and A pushes a change on a feature branch: A-feature to the repo BEFORE B, B now needs to do a pull before he can push.

But where are the exact condition for this described? E.g. does it apply to any change on any branch pushed to the repo? Or is it only if A and B are pushing to the same branch? Meaning that B could push to B-feature even though A just pushed on A-feature before him?

like image 932
u123 Avatar asked Dec 25 '22 01:12

u123


1 Answers

Meaning that B could push to B-feature even though A just pushed on A-feature before him?

Yes. The warning you get is only when you are pushing to a branch updated by someone else:

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.

The only alternative to a git pull would be a git push --force (which would overwrite the history published by A with B's, if B were to push on A-feature, and that is bad.).
See "git push --force, behind the scenes".

If nobody but B is working on B-feature, no pull is needed when B wants to push the local B-feature branch to the upstream repo: the push will be a fast-forward one: you are adding new commits on top of the remote branch.

like image 132
VonC Avatar answered Jan 05 '23 06:01

VonC