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:
Any help much appreciated.
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.
Yes, two branches can point to the same commits.
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With