mfaani
mfaani

Reputation: 36297

What exactly happens in a git push? Why isn't a git push considered just like a git merge?

So as mentioned here a git pull is just fetching the latest and then merging them. Isn't that the same for a git push? ie from the remote...you fetch the local and merge it into the branch?

Or is that you're not merging anything, rather you're totally re-writing history of commits...ignoring whatever was there and just replacing commit lists with a newer one?

(I'm fully aware of when to use git push ie I use when I'm done with a feature in my local. I push into my origin. My question is more about what happens under the hood)

Upvotes: 0

Views: 2666

Answers (1)

nvioli
nvioli

Reputation: 4209

git pull pulls the latest commits from the remote to your local client.

git push pushes your latest local commits from your local client to the remote.

When you push, if your local repository does not have all commits from the remote, the push is rejected, not merged into the remote branch as you seem to think. In this case you must first pull the remote's latest changes and either merge or rebase your local changes. Now your local repository is ahead of the remote and you can proceed to push.

In the special case where local and remote histories match, you could look at the push as symmetrically opposite to the pull, i.e. the remote "pulls" the changes from the local, and you're right that in this case. The merge strategy would be equivalent to pulling new changes from remote (specifically the merge strategy is fast-forward; explained about halfway down this page). However a remote repo doesn't work exactly like a local repo, there's no way to login to the remote and pull; rather the command is being run on the local so we must use the correct verb, push.

Upvotes: 2

Related Questions