0

the / between origin and branch some time i need put / between origin and branch, somemtime not, could anyone explain me why?

for example here is no /

git push -u origin branch_name

here is with /

git diff master origin/branch_name
halfelf
  • 9,737
  • 13
  • 54
  • 63
stewchicken
  • 463
  • 1
  • 8
  • 20
  • 1
    At the moment, your question is much too broad, since it requires a full Git tutorial to answer. You need to narrow down what *precisely* it is that you don't understand. Do you know what a repository is? Do you know what a branch is? Do you know what a ref is? Do you know what a refspec is? Do you know what a refspec looks like? Do you know what `git push` does? Do you know the parameter syntax of `git push`? Have you read its manpage? Do you know what `git diff` does? Do you know the parameter syntax of `git diff`? Have you read its manpage? – Jörg W Mittag Aug 31 '18 at 08:55
  • @JörgWMittag: I think the question is legitimate. In `git` commands some times you write `origin/master`, but other times you write `origin master`. There is a rationale for that, and understanding it gives you a better insight on how `git` works – rodrigo Aug 31 '18 at 11:20

2 Answers2

1

One funny thing with git is that you cannot look at branches in a remote, only local ones.

When you do git fetch origin you are telling git to sync all the X branches from that remote into local branches named remotes/origin/X. Then when you do git log origin/X you are actually looking the history of the local branch remotes/origin/X (the remotes part is assumed).

Some people call these remote branches, but they are actually local branches that mimick remote branches.

In the command line, when you name a commit (branch, tag...) and you want to use a remote branch (actually a local branch that mimicks a remote one) you must use the origin/master format or remotes/origin/master.

But if you use a command that uses a remote proper and a real remote branch then you use two arguments, one for the remote and another one for the branch or branches. In this case, the real remote branch name will not have the remote/ part.

For example:

  • git fetch origin master: Fetch the real remote branch master from remote origin and clone it into local branch remotes/origin/master (with default config).
  • git fetch origin master:foo: Fetch the real remote branch master from remote origin and clone it into local branch foo.
  • git log origin/master: Show log of local branch remotes/origin/master that will have the same content of remote master in origin at the time of the previous fetch.
  • git push origin foo:bar: Upload the local branch foo into origin as a remote branch named bar. If then you do git fetch origin you will get a new remotes/origin/bar branch identical to foo.
  • git push: If the current branch is configured with upstream, then it will be equivalent to git push <upstream-remote> <upstream-branch>.

NOTE: Note the default refspec when you do a git fetch origin is +refs/heads/*:refs/remotes/origin/*. See how it makes sense if you remember that the refs/ part means just the directory of branches and tags, the heads/ means local branches, and remotes/ means local clone of remote branches.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
0

origin conventionally refers to the remote from which you cloned your repo. Similarly master conventionally refers to the main branch in your repository.

In the first command, you are asking git to push a branch called master in your local to a remote called origin.

In the second command, you are asking git to show you the difference in the branch called master in your local and a branch called master in a remote called origin.

Contrast it with something like

git pull upstream feature1

or

git diff origin/master upstream/master

In the first case, you are pulling from a branch called feature1 in a remote called upstream in to your current local branch.

In the second one, you are checking the difference between the master branch in your remote called origin and the master branch in a remote called upstream

TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33