Rodrigo Borba
Rodrigo Borba

Reputation: 1474

Difference between git push origin development and git push origin HEAD:development

after some weeks using git as a machine, now I'm on my way to figure out how GIT really works. Than I started with a simple thing:

git checkout -b test origin/development`   //Creating new local branch from development

Create new text file called test.txt

git add . //Set files that will be committed
git commit -m "this is a test" // Committing

And then comes my doubt:

When I try to push doing:

git push origin development

The log says: Everything is up to date

If I try to push doing:

git push origin HEAD:development

It works as it should.

Doing my research I found that HEAD means the name of the current branch. So, if it is right, it doesn't make any sense to me.

What is the difference between

git push origin development

and

git push origin HEAD:development

Upvotes: 3

Views: 13034

Answers (3)

ely
ely

Reputation: 77504

git push remote-name some-name

attempts to push any local branch named some-name to a remote branch also named some-name resolved at remote-name/some-name. Under the hood, it's looking for branch names under refs at each location (local, remote) that match.

Since your local copy of development hasn't been modified, you get a message that it's up to date, nothing to push.

The alternate version

git push remote-name HEAD:development

skips the part about looking for matching branch names in local and remote refs because you've given it an explicit branch via HEAD.

It still does use remote refs to determine the remote branch development. But then it uses HEAD of the current branch (your new test branch) for the commits to be pushed.

If you want to simply do git push from test branch and correctly push from test to remote/development, you can configure the default behavior of push to the "upstream" setting (note that the term "tracking" is an older term for "upstream" which is preferred).

Do

git config --global push.default upstream

to ensure that a plain git push will only attempt to push from the current branch to its registered upstream branch.

Without the setting, prior to git version 2.0, the default mode for a plain git push results in attempting to push all local branches to upstream branches with matching names at their configured remotes. For git 2.0 and newer, the default setting is simple which acts like upstream except that it also fails to push if the current branch doesn't share the same name as its configured remote.

The "upstream" setting is a good one because (a) it allows easily pushing branches regardless of whether their names match to their upstream names; (b) for versions prior to 2.0, it restricts a bare invocation of git push to affecting only the current branch, so if you're actively developing on multiple branches, git push will no longer unintentionally push other work unrelated to current branch work; (c) you will not need the form of git push that specifically names the remote and branch unless you're attempting to push to some branch that is not the configured upstream for your current branch -- and I'd argue this makes the most sense: you should only need to verbosely spell out the branches if you're not pushing from a local branch to its natural, configured upstream target. Even the 2.0 simple setting doesn't satisfy this.

remote-name is often origin, but can be any configured remote, added via various git commands or directly as in a .gitconfig file.

Upvotes: 3

Basilevs
Basilevs

Reputation: 23968

git push origin development pushes development branch

git push origin HEAD:development pushes current branch to remote's development

Most likely you are not in local development branch. Consider checking this with

git status

command.

Upvotes: 3

gzh
gzh

Reputation: 3616

git push origin development

will try to push your local development branch. By this type of git push command, git will think local branch and remote tracking branch have the same branch nanme.

From manpage of git push

git push origin master Find a ref that matches master in the source repository (most likely, it would find refs/heads/master), and update the same ref (e.g. refs/heads/master) in origin repository with it. If master did not exist remotely, it would be created.

Upvotes: 0

Related Questions