Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why has Git allowed me to create two branches with the same name?

Tags:

git

github

I'm still relatively new to Git and I have made a bit of a mess of my repository. I'm hoping there is a way to fix it without re-cloning.

I have a repository which I have cloned from Github. The repository has several branches. I worked on the master branch for a while but then needed to switch to one of the other branches.

So, I had:

$ git branch --all * master   remotes/origin/abc   remotes/origin/def   remotes/origin/HEAD -> origin/master   remotes/origin/ghi 

Problem: I wanted to switch to the 'abc' branch but instead of doing git checkout remotes/origin/abc I accidentally did git branch remotes/origin/abc which leaves me with the following:

$ git branch --all * master   remotes/origin/abc   remotes/origin/abc   remotes/origin/def   remotes/origin/HEAD -> origin/master   remotes/origin/ghi 

My questions are:

  • Why on Earth does Git allow you to create two branches with the same name?
  • How do I identify which is the real remotes/origin/abc branch?
  • How do I remove the unwanted remotes/origin/abc that I created by accident?

Any help much appreciated.

like image 466
MrCrinkle Avatar asked Jun 28 '13 08:06

MrCrinkle


People also ask

Why do I have two branches in git?

Git offers a feature referred to as a worktree, and what it does is allow you to have multiple branches running at the same time. It does this by creating a new directory for you with a copy of your git repository that is synced between the two directories where they are stored.

Can two branches have the same commit?

Yes, two branches can point to the same commits.

Can you be in two git branches at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.


1 Answers

You can't create two local branches or two distant branches with the same name.

  • Here you have a local branch named remotes/origin/abc and a distant branch named abc on the remote origin. They have not the same name, but it seems to when you use the git branch --all command.

  • To identify which branch is which, you can show local branches with git branch, or show remote branches with git branch --remote. You could also easily differentiate them even while using git branch --all with the branch syntax coloration (git config --global color.branch auto).

  • To remove the accidentally created local branch abc, you have to do git branch -d abc (or git branch -D abc to force deletion, see man git-branch).

like image 135
aymericbeaumet Avatar answered Sep 23 '22 10:09

aymericbeaumet