Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between origin/branch_name and branch_name?

Tags:

for pushing to bitbucket.

If I do: git push origin origin/branch_name my commits are not pushed.

Total 0 (delta 0), reused 0 (delta 0) 

If I do git push origin branch_name my commits are pushed:

Counting objects: 160, done. Delta compression using up to 8 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (20/20), 2.10 KiB | 0 bytes/s, done. Total 20 (delta 6), reused 0 (delta 0) 

so what is the origin/ mean in front of the branch_name? And why does it matter?

like image 543
stewart99 Avatar asked Sep 30 '14 15:09

stewart99


People also ask

What is the difference between git push origin and git push?

Simply, we can say that git push command updates the remote repository with local commits. And origin represents a remote name where the user wants to push the changes.

What is origin and branch name in git?

While “master” is the default name for a starting branch when you run git init which is the only reason it's widely used, “origin” is the default name for a remote when you run git clone . If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.

What is the meaning of git push origin?

In simple words git push command updates the remote repository with local commits. The origin represents a remote name where the user wants to push the changes. git push command push commits made on a local branch to a remote repository.

What does git branch command do?

The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands.


2 Answers

You have to keep in mind that there are different types of branches:

  • (Purely) local branches, i.e. branches you commit to,
  • Branches that live in a remote repository, for lack of a better term. You may know the remote repository in question under a remote name, such as origin. From that repository's point of view, though, such a branch is local. Welcome to Distributed Version Control! :)
  • Remote-tracking branches. Also simply called remote branches, as in the Pro Git book, but I find this phrase confusing; remote-tracking branch is more accurate. They're special local branches whose sole purpose is to reflect the state of branches that live in a remote repository at the time of your last communication with the server. In particular, you can't commit to branches of this type.

Here, branch_name is a local branch, whereas origin/branch_name is a remote-tracking branch; it reflects the state of the corresponding branch that lives in origin.

Right after running

git fetch 

the remote-tracking branch origin/master and the corresponding branch that live in origin should be perfectly in sync (modulo concurrent pushes to the remote server, of course). It shouldn't be a surprise, then, that

git push origin origin/branch_name 

doesn't push anything: you're essentially attempting to push stuff that is already present in the ancestry of the corresponding branch that live in origin .

However, if your local branch, branch_name, is ahead by one or more commits,

enter image description here

then running

git push origin branch_name 

will push the commits contained in branch_name but not in the branch that live in origin:

enter image description here

like image 181
jub0bs Avatar answered Oct 06 '22 02:10

jub0bs


using a graphical tree viewer (like gitk --all) will show you, that origin/mybranch and mybranch might differ.

origin is just the default name for a cloned remote, which (in your case) contains a branch mybranch (just like your local repository)

so when you ask to push origin/mybranch to origin, you are synchronizing the origin remote with itself, hence it doesn't do anything (luckily the remote is always in synch with itself).

the name origin is arbitrary, and could have been set with the --origin flag when cloning.

like image 45
umläute Avatar answered Oct 06 '22 01:10

umläute