Somebody pushed a branch called test
with git push origin test
to a shared repository. I can see the branch with git branch -r
.
I tried the above solution, but it didn't work. Try this, it works:
git fetch origin 'remote_branch':'local_branch_name'
This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name
and track the remote one in it.
You can start tracking all remote branches with the following Bash script:
#!/bin/bash
git fetch --all
for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`
do git branch -f --track "$branch" "origin/$branch"
done
Here is also a single-line version:
git fetch --all; for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`; do git branch --track "$branch" "origin/$branch" ; done ;
Commands
git fetch --all
git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>
are equal to
git fetch --all
and then
git checkout -b fixes_for_dev origin/development
Both will create a latest fixes_for_dev
from development
First, you need to do:
git fetch
# If you don't know about branch name
git fetch origin branch_name
Second, you can check out remote branch into your local by:
git checkout -b branch_name origin/branch_name
-b
will create new branch in specified name from your selected remote branch.
Other guys and gals give the solutions, but maybe I can tell you why.
git checkout test which does nothing
Does nothing
doesn't equal doesn't work
, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?
If the answer is 'yes', I can tell you the cause.
The cause is that there is a file (or folder) named 'test' in your work tree.
When git checkout xxx
parsed,
xxx
as a branch name at first, but there isn't any branch named test.xxx
is a path, and fortunately (or unfortunately), there is a file named test. So git checkout xxx
means discard any modification in xxx
file.xxx
either, then Git will try to create the xxx
according to some rules. One of the rules is create a branch named xxx
if remotes/origin/xxx
exists.In this case, you probably want to create a local test
branch which is tracking the remote test
branch:
$ git branch test origin/test
In earlier versions of git
, you needed an explicit --track
option, but that is the default now when you are branching off a remote branch.