0

I have gone through some Q&A

In Git, what is the difference between origin/master vs origin master?

Git branching: master vs. origin/master vs. remotes/origin/master

I got to know

origin/master is a remote branch (which is a local copy of the branch named "master" on the remote named "origin")

remotes/origin/master is a branch named master on the remote named origin.

Now I want to know

  • if origin/master is a local copy then why git branch does not display this branch.
  • can origin/master be treated like any other ordinary branch in local?

If not then I am in trouble. this is what I did:

I have cloned a repository. I have added a remote let say remoteA(same projet in another repo) and rebase and merge master with dev branch on remoteA. I used below command for this(current branch is master):

git pull --rebase remoteA/dev

then rebase master with origin/master

git rebase origin/master

then I created a another branch from master pushed it into remote and got successfully merged(could not push master branch as it is protected).

Now if I run git status it says

Your branch and 'origin/master' have diverged, (use "git pull" to merge the remote branch into yours)

when I do git pull origin master its showing all conflicts what I have resolved during rebase. Idon't wanna resolve them again. all I want is fresh copy of master without cloning it again. what did I do wrong in this process? how to correct it?
plz explain. thanks for any help.

Community
  • 1
  • 1
Suraj
  • 1,625
  • 1
  • 15
  • 33

2 Answers2

1

If origin/master is a local copy then why git branch does not display this branch.

Using git branch does show the local tracking branches, if you run it like this:

git branch -a

The reason why plain vanilla git branch doesn't show tracking branches by default is because normally you would not be manipulating these branches directly. This is a good segway to your second question.

can origin/master be treated like any other ordinary branch in local?

As far as I know, you can use this branch much in the same way you would use any other branch. However, this is not recommended or very typical. Local tracking branches serve as local proxies for the true remote branches which exist in your remote repository (e.g. Bitbucket, GitHub). I would advise against directly working with the tracking branches, unless there be a very good reason to do so.

You can think of the local tracking branches as being, for all intents and purposes, the actual remote branch. Of course there is the caveat that a tracking branch itself can be behind what is really on the remote.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • if I do git reset --hard in origin/master, it will only change the local copy. right? it has nothing to do with master branch at origin? – Suraj Feb 10 '17 at 08:58
  • Yes, that command will reset whatever branch you are on (presumably local `master`) to the local tracking branch which is called `origin/master`. It doesn't touch the remote; you'd need to push in order for that to happen. – Tim Biegeleisen Feb 10 '17 at 08:59
  • thanks. can you see my question and tell me what I did wrong in order to rebase my local branch with a remote branch. – Suraj Feb 10 '17 at 09:11
  • You rebased your local `master` branch _twice_, on two _different_ bases. This is a big mess. I would say just start over and save yourself the trouble. – Tim Biegeleisen Feb 10 '17 at 09:14
0

origin/master just mark the position of the master branch in remote repo. git branch only show the branches exist in local. If you want to find the position of origin/master, you can use git log --oneline --decorate --graph --all.

No, it’s can’t be treated as local branch. Even you can also check origin/master history (git log origin/master) or you can compare diff with local branches (git diff master origin/master) etc. But it still has some different features with local branch. Such as you can delete/create local branches but you cannot do that for origin/master.

To correct the situation that origin/master is diverged:

If you don’t want the changes for local master branch, you can use :

git checkout <another local branch>
git branch -D master
git checkout master

If you want the changes for local master branch, and resolve the conflict by using origin/master, you can use:

git checkout master
git pull -X theirs origin master
git push origin master
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • "git branch -D master" will delete the remote master branch. isn't it? and if it got deleted "git checkout master" won't work. – Suraj Feb 13 '17 at 08:52
  • `git branch -D master` will delete local master branch. `git checkout master` will create a new local branch point to the same commit as `origin/master`. – Marina Liu Feb 13 '17 at 08:58