Knight71
Knight71

Reputation: 2949

Git shows two branches in remote

I cloned a git repo and created a branch A using git checkout -b A . After that I did some changes and committed in branch A and pushed it to the remote branch using

    git push -u origin/A

Meanwhile,I have deleted my local workarea . Now I have to make changes in A. So I cloned the repo again. Now I have two branches origin/A and A . Should I do

     git checkout A 
       (or)
     git checkout -b branchname origin/A  

Question: 1) why two branches of the same name when both are same?

Upvotes: 1

Views: 942

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476649

Perhaps the word branch is poorly chosen here. You might say it is a reference point: it is a reference to a commit in the commit graph.

The reason is that git is distributed: every peer stores a (part) of the commit graph. There is no guarantee that the graph on your machine is exactly the same as the one on the remote machine (origin), or on the machine of a friend that collaborates on the project as well.

It is for instance possible that the local graph looks like:

master    abc----ghi---mno
             \      \
              \      \
A              def----jkl

That means that there are two pointers master pointing to mno and A pointing to jkl. Now the remote can not have been updated recently and only contain:

master    abc----ghi---mno
             \
              \
A              def

It is also possible that another developer made contribution xyz in the remote resulting in a graph:

master    abc----ghi---mno
             \
              \
A              def---xyz

In that case the union looks like:

master    abc----ghi---mno
             \      \
              \      \
A              def----jkl
                  \
                   \
origin/A            xyz

In that case origin/A points to def and A points to jkl.

So both are the same if the remote is completely in sync with the local repository.

A git checkout means you are setting the active branch, it means that active state of the project now switches to where the reference is pointing to. If you thus do checkout A, the system will modify files such that it looks like the state stored in commit jkl, furthermore jkl will be marked as the "current" state.

If you would run git checkout -b B A (mind, not origin/A). You simply create a new branch. From now on, both A and B point to jkl. Thus:

master    abc----ghi---mno
             \      \
              \      \
               def----jkl
                     / |
                   <A> |
                       /
                    <B>

If you run git checkout -b B A: and as said before the origin is not fully synchronized (or is synchronized further), it will result in:

master    abc----ghi---mno
             \      \
              \      \
               def----jkl
              /      /
           <B>    <A>

You can run git fetch --all or git pull --all origin to synchronize the graphs.

Upvotes: 1

KyleK
KyleK

Reputation: 5056

origin/A is the remote, A is your local, so they can be different if remote or local is not up to date. But you do not need to clone again to get a branch, you just have to do git fetch --all

Upvotes: 1

Related Questions