dhblah
dhblah

Reputation: 10151

git push vs git push origin <branchname>

I'm quite new to Git.

I'm creating a branch and then want to push it to origin.

I think that simply issuing git push (while standing on my branch) should be sufficient.

Is it possible/reasonable to do that (by specifying push.default simple)?

Upvotes: 72

Views: 202760

Answers (3)

DaniloMourelle
DaniloMourelle

Reputation: 173

The commands git pull and git push are used to move commits between a local and a remote repository. But under the hood, the local branch is totally different/separated from the remote one. What happens in push and pull is kind of a merge between both of them with a fast forward condition if possible.

We usually try to keep local and remote in sync with each other, so it's normal to give the same name to a branch in local and in remote, but this is not required. You could have a local branch feature_A being represented by a remote one any_other_name with no problem.

So if you put it together, we have different branches not being tied by name, which makes it possible for you to pull and push (merging) any remote branch to any local branch. That is why only a git push doesn't work. Because you could push it to any of the existing branches on remote and git doesn't know what you mean.

But we clearly do pull and push more often to sync between local and remote. So for the first time you pushing a new local branch, with the intention to create a corresponding branch with the same name into remote, you have to use the set-upstream option on command to tie each other. After this, a simple git push will do the job to sync, but it would be still possible to push (merging) it to another remote branch, but in this case, you should use the full command.

NOTE: Just be aware of the side effects you can have using just git push if your git has more than one remote configured

Upvotes: 1

vkulkarni
vkulkarni

Reputation: 897

First, you need to create your branch locally

git checkout -b your_branch

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch

Your Teammates/colleagues can push to your branch by doing commits and then push explicitly

... work ...
git commit
... work ...
git commit
git push origin HEAD:refs/heads/your_branch 

Upvotes: 35

VonC
VonC

Reputation: 1323145

The first push should be a:

git push -u origin branchname

That would make sure:

Any future git push will, with that default policy, only push the current branch, and only if that branch has an upstream branch with the same name.
that avoid pushing all matching branches (previous default policy), where tons of test branches were pushed even though they aren't ready to be visible on the upstream repo.

Upvotes: 64

Related Questions