Reputation: 69
I have cloned a private repository locally that has 3 branches - master, dev and staging. When it is cloned I am in the master branch. When I list branches:
git branch -a
I see all the branches. When I run:
git checkout dev
nothing happens and I am still in the master branch. I have tried to pull and fetch to get the other branches with no luck. I can't figure out how to switch to any of the other branches.
Upvotes: 1
Views: 6485
Reputation: 488193
This is a guess, but I've reproduced the behavior:
sh-3.2$ mkdir txx
sh-3.2$ cd txx
sh-3.2$ git init
Initialized empty Git repository in ...
sh-3.2$ echo test branch name thing > README.md
sh-3.2$ git add README.md
sh-3.2$ git commit -m initial
[master (root-commit) 7dc0be2] initial
1 file changed, 1 insertion(+)
create mode 100644 README.md
sh-3.2$ echo this is a file named dev > dev
sh-3.2$ git add dev
sh-3.2$ git commit -m 'add file named dev'
[master ac01ba6] add file named dev
1 file changed, 1 insertion(+)
create mode 100644 dev
sh-3.2$ git remote add origin .
sh-3.2$ git update-ref refs/remotes/origin/master master
sh-3.2$ git update-ref refs/remotes/origin/dev master
sh-3.2$ echo foo >> dev
sh-3.2$ git checkout dev
Nothing seems to have happened here, but actually, something did: the file dev
was checked out, destructively. Here are its contents:
sh-3.2$ cat dev
this is a file named dev
and here is the confirmation that we are still on master
:
sh-3.2$ git status
On branch master
nothing to commit, working tree clean
The working tree clean
is a consequence of destroying the updated dev
, replacing it with the copy extracted from the index by git checkout
.
Note that all of this requires a slightly older version of Git:
sh-3.2$ git --version
git version 2.20.1
A more modern one tells us about the problem. Doing the same thing in Git 2.24 produces:
$ git checkout dev
fatal: 'dev' could be both a local file and a tracking branch.
Please use -- (and optionally --no-guess) to disambiguate
This sort of thing is the reason git checkout
is now two separate commands, git switch
and git restore
. Using git switch
:
$ git switch dev
M dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
does the desired thing.
Upvotes: 10
Reputation: 49
You can try below command and see if it works "git clone --branch url" It is fetching all the branches and checking out to the branch we specify one.
Upvotes: -1
Reputation: 47
My guess is that you haven't fetched the remote branch. By default, git checkout only gets one branch. You need to do a combination of
git fetch
followed by
git checkout dev
After that, you should see a message of
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'
Upvotes: 0