I have a master
and a development
branch, both pushed to GitHub. I've clone
d, pull
ed, and fetch
ed, but I remain unable to get anything other than the master
branch back.
I'm sure I'm missing something obvious, but I have read the manual and I'm getting no joy at all.
This question is related to
git
git-branch
git-clone
remote-branch
Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs
) from one or more other repositories along with the objects necessary to complete their histories. In other words it fetches the objects which are reachable by the objects that are already downloaded. See: What does git fetch
really do?
Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all
/git fetch --all
won't help in that case, but you can list them by:
git ls-remote -h -t origin
and fetch them manually by knowing the ref names.
So to fetch them all, try:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.
Then check all your branches again:
git branch -avv
If above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):
$ git remote -v show origin
...
Remote branches:
master tracked
by git remote set-branches
like:
git remote set-branches --add origin missing_branch
so it may appear under remotes/origin
after fetch:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
If you still cannot get anything other than the master branch, check the followings:
git remote -v
), e.g.
git config branch.master.remote
is origin
.origin
points to the right URL via: git remote show origin
(see this post).As of early 2017, the answer in this comment works:
git fetch <origin-name> <branch-name>
brings the branch down for you. While this doesn't pull all branches at once, you can singularly execute this per-branch.
A git clone
is supposed to copy the entire repository. Try cloning it, and then run git branch -a
. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo
.
gitCloneAllBranches() { # clone all git branches at once easily and cd in
# clone as "bare repo"
git clone --mirror $1
# rename without .git extension
with_extension=$(basename $1)
without_extension=$(echo $with_extension | sed 's/.git//')
mv $with_extension $without_extension
cd $without_extension
# change from "bare repository" to not
git config --bool core.bare false
# check if still bare repository if so
if [[ $(git rev-parse --is-bare-repository) == false ]]; then
echo "ready to go"
else
echo "WARNING: STILL BARE GIT REPOSITORY"
fi
# EXAMPLES:
# gitCloneAllBranches https://github.com/something/something.git
}
This variation will clone a remote repo with all branches available locally without having to checkout each branch one by one. No fancy scripts needed.
Make a folder with the same name of the repo you wish to clone and cd into for example:
mkdir somerepo
cd somerepo
Now do these commands but with actual repo usersname/reponame
git clone --bare [email protected]:someuser/somerepo.git .git
git config --bool core.bare false
git reset --hard
git branch
Voiala! you have all the branches there!
When you do "git clone git://location", all branches and tags are fetched.
In order to work on top of a specific remote branch, assuming it's the origin remote:
git checkout -b branch origin/branchname
Regarding,
$ git checkout -b experimental origin/experimental
using
$ git checkout -t origin/experimental
or the more verbose but easier to remember
$ git checkout --track origin/experimental
might be better, in terms of tracking a remote repository.
A little late to the party, but I think this does the trick:
mkdir YourRepo
cd YourRepo
git init --bare .git # create a bare repo
git remote add origin REMOTE_URL # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/* # fetch tags
git init # reinit work tree
git checkout master # checkout a branch
If this does something undesirable, I'd love to know. However, so far, this works for me.
git clone --mirror
on the original repo works well for this.
git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin
A better alternative solution for developers using vscode is to use Git Shadow Extension. This vscode extension allows cloning repository content and directories, that can be filtered by branch name or commit hash. That way, branches or commits can be used as boilerplates/templates for new projects.
For copy-paste into command line:
git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master
For more readibility:
git checkout master ;
remote=origin ;
for brname in `
git branch -r | grep $remote | grep -v master | grep -v HEAD
| awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do
git branch -D $brname ;
git checkout -b $brname $remote/$brname ;
done ;
git checkout master
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.
To create a local branch based on a remote branch, do something like:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
A git clone
is supposed to copy the entire repository. Try cloning it, and then run git branch -a
. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo
.
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax. You can do:
git checkout somebranch
Git will automatically do the right thing:
$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'
Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 1.8.2.1:
If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to
$ git checkout -b <branch> --track <remote>/<branch>
If you’re looking for a self-contained clone or backup that includes all remote branches and commit logs, use:
git clone http://[email protected]
git pull --all
The accepted answer of git branch -a
only shows the remote branches. If you attempt to checkout
the branches you'll be unable to unless you still have network access to the origin server.
Credit: Gabe Kopley's for suggesting using git pull --all
.
Note:
Of course, if you no longer have network access to the remote/origin
server, remote/origin branches
will not have any updates reflected in them. Their revisions will reflect commits from the date and time you performed the 2 commands above.
To checkout ALL your clone branches to local branches with one command, use one of the bash commands below:
$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done
OR
If your repo has nested branches then this command will take that into account:
for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done
The above commands will checkout
a local branch into your local git repository, named the same as the remote/origin/<branchname>
and set it to --track
changes from the remote branch on the remote/origin
server should you regain network access to your origin repo server once more and perform a git pull
command in the usual way.
Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched.
To get a clone with all branches and tags.
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
To get a clone with all branches and tags but also with a working copy:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
For copy-paste into command line:
git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master
For more readibility:
git checkout master ;
remote=origin ;
for brname in `
git branch -r | grep $remote | grep -v master | grep -v HEAD
| awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do
git branch -D $brname ;
git checkout -b $brname $remote/$brname ;
done ;
git checkout master
If you have many remote branches that you want to fetch at once, do:
$ git pull --all
Now you can checkout any branch as you need to, without hitting the remote repository.
Note: This will not create working copies of any non-checked out branches, which is what the question was asking. For that, see
Just do this:
$ git clone git://example.com/myproject
$ cd myproject
$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'
$ git pull
Already up-to-date.
$ git branch
* branchxyz
master
$ git branch -a
* branchxyz
master
remotes/origin/HEAD -> origin/master
remotes/origin/branchxyz
remotes/origin/branch123
You see, 'git clone git://example.com/myprojectt' fetches everything, even the branches, you just have to checkout them, then your local branch will be created.
A git clone
is supposed to copy the entire repository. Try cloning it, and then run git branch -a
. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo
.
I needed to do exactly the same. Here is my Ruby script.
#!/usr/bin/env ruby
local = []
remote = {}
# Prepare
%x[git reset --hard HEAD]
%x[git checkout master] # Makes sure that * is on master.
%x[git branch -a].each_line do |line|
line.strip!
if /origin\//.match(line)
remote[line.gsub(/origin\//, '')] = line
else
local << line
end
end
# Update
remote.each_pair do |loc, rem|
next if local.include?(loc)
%x[git checkout --track -b #{loc} #{rem}]
end
%x[git fetch]
Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched.
To get a clone with all branches and tags.
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
To get a clone with all branches and tags but also with a working copy:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
gitCloneAllBranches() { # clone all git branches at once easily and cd in
# clone as "bare repo"
git clone --mirror $1
# rename without .git extension
with_extension=$(basename $1)
without_extension=$(echo $with_extension | sed 's/.git//')
mv $with_extension $without_extension
cd $without_extension
# change from "bare repository" to not
git config --bool core.bare false
# check if still bare repository if so
if [[ $(git rev-parse --is-bare-repository) == false ]]; then
echo "ready to go"
else
echo "WARNING: STILL BARE GIT REPOSITORY"
fi
# EXAMPLES:
# gitCloneAllBranches https://github.com/something/something.git
}
Using the --mirror
option seems to copy the remote
tracking branches properly.
However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.
git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch
Reference: Git FAQ: How do I clone a repository with all remotely tracked branches?
Here's an answer that uses awk. This method should suffice if used on a new repo.
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
Existing branches will simply be checked out, or declared as already in it, but filters can be added to avoid the conflicts.
It can also be modified so it calls an explicit git checkout -b <branch> -t <remote>/<branch>
command.
This answer follows Nikos C.'s idea.
Alternatively we can specify the remote branch instead. This is based on murphytalk's answer.
git branch -r | awk '{ system("git checkout -t " $NF) }'
It throws fatal error messages on conflicts but I see them harmless.
Both commands can be aliased.
Using nobody's answer as reference, we can have the following commands to create the aliases:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
Personally I'd use track-all
or track-all-branches
.
Script to download all braches from a Git project
sudo git clone https://github.com/marceloviana/allBranches.git && sudo cp -rfv allBranches/allBranches.sh /usr/bin/allBranches && sudo chmod +x /usr/bin/allBranches && sudo rm -rf allBranches
Ready! Now just call the command (allBranches) and tell the Git project directory that you want to download all branches
Use
Example 1:
~$ allBranches /var/www/myproject1/
Example 2:
~$ allBranches /var/www/myproject2/
Example 3 (if already inside the project directory):
~$ allBranches ./
or
~$ allBranches .
View result:
git branch
Repository allBranches GitHub: https://github.com/marceloviana/allBranches
Here's a cross-platform PowerShell 7 function adapted from the previous answers.
function Invoke-GitCloneAll($url) {
$repo = $url.Split('/')[-1].Replace('.git', '')
$repo_d = Join-Path $pwd $repo
if (Test-Path $repo_d) {
Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
} else {
Write-Host "`nCloning all branches of $repo..."
git -c fetch.prune=false clone $url -q --progress &&
git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
Write-Host "" #newline
}
}
Note: -c fetch.prune=false
makes it include stale branches that would normally be excluded, remove that if you're not interested in it.
You can make this work with PowerShell 5.1 (the default in Windows 10) by removing &&
from the function, but that makes it try to git pull
even when the previous command failed. So, I strongly recommend just using the cross-platform PowerShell it's always bugging you about trying.
I'm going to add my 2 cents here because I got here trying to find out how to pull down a remote branch I had deleted locally. Origin was not mine, and I didn't want to go through the hassle of re-cloning everything
This worked for me:
assuming you need to recreate the branch locally:
git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name
So if I forked from gituser/master to sjp and then branched it to sjp/mynewbranch it would look like this:
$ git checkout -b mynewbranch
$ git branch -a
master
remotes/sjp/master
remotes/sjp/mynewbranch
$ git fetch (habit to always do before)
$ git rebase remotes/sjp/mynewbranch
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.
To create a local branch based on a remote branch, do something like:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
Here is another short one-liner command which creates local branches for all remote branches:
(git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout
It works also properly if tracking local branches are already created.
You can call it after the first git clone
or any time later.
If you do not need to have master
branch checked out after cloning, use
git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout
git clone
downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master". By default, git branch
only shows local branches, which is why you only see "master".
git branch -a
shows all branches, including remote branches.
If you actually want to work on a branch, you'll probably want a "local" version of it. To simply create local branches from remote branches (without checking them out and thereby changing the contents of your working directory), you can do that like this:
git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree
In this example, branchone
is the name of a local branch you're creating based on origin/branchone
; if you instead want to create local branches with different names, you can do this:
git branch localbranchname origin/branchone
Once you've created a local branch, you can see it with git branch
(remember, you don't need -a
to see local branches).
When you do "git clone git://location", all branches and tags are fetched.
In order to work on top of a specific remote branch, assuming it's the origin remote:
git checkout -b branch origin/branchname
Just do this:
$ git clone git://example.com/myproject
$ cd myproject
$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'
$ git pull
Already up-to-date.
$ git branch
* branchxyz
master
$ git branch -a
* branchxyz
master
remotes/origin/HEAD -> origin/master
remotes/origin/branchxyz
remotes/origin/branch123
You see, 'git clone git://example.com/myprojectt' fetches everything, even the branches, you just have to checkout them, then your local branch will be created.
If you have many remote branches that you want to fetch at once, do:
$ git pull --all
Now you can checkout any branch as you need to, without hitting the remote repository.
Note: This will not create working copies of any non-checked out branches, which is what the question was asking. For that, see
You only need to use "git clone" to get all branches.
git clone <your_http_url>
Even though you only see master branch, you can use "git branch -a" to see all branches.
git branch -a
And you can switch to any branch which you already have.
git checkout <your_branch_name>
Don't worry that after you "git clone", you don't need to connect with the remote repo, "git branch -a" and "git checkout " can be run successfully when you close your wifi. So it is proved that when you do "git clone", it already has copied all branches from the remote repo. After that, you don't need the remote repo, your local already has all branches' codes.
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done
These code will pull all remote branches code to local repo.
None of these answers cut it, except user nobody is on the right track.
I was having trouble with moving a repo from one server/system to another. When I cloned the repo, it only created a local branch for master so when I pushed to the new remote, only master branch was pushed.
So I found these two methods VERY useful. Hope they help someone else.
Method 1:
git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
Method 2:
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
all the answers I saw here are valid but there is a much cleaner way to clone a repository and to pull all the branches at once.
When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command
$ git branch -a
you can show all the branches of the repository, and with the command
$ git checkout -b branchname origin/branchname
you can then "download" them manually one at a time.
However, when you want to clone a repo with a lot of branches all the ways illustrated are above are lengthy and tedious in respect to a much cleaner and quicker way that I am going to show, though it's a bit complicated. You need three steps to accomplish this:
create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:
$ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
$ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.
switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:
$ git config --bool core.bare false
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.
$ git reset --hard
So now you can just type the command "git branch" and you can see that all the branches are downloaded.
This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.
git clone
downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master". By default, git branch
only shows local branches, which is why you only see "master".
git branch -a
shows all branches, including remote branches.
If you actually want to work on a branch, you'll probably want a "local" version of it. To simply create local branches from remote branches (without checking them out and thereby changing the contents of your working directory), you can do that like this:
git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree
In this example, branchone
is the name of a local branch you're creating based on origin/branchone
; if you instead want to create local branches with different names, you can do this:
git branch localbranchname origin/branchone
Once you've created a local branch, you can see it with git branch
(remember, you don't need -a
to see local branches).
$ mkdir -p -- newproject_folder
$ cd newproject_folder
$ git clone --mirror https://github.com/USER_NAME/RepositoryName.git .git
$ git config --bool core.bare false
$ git reset --hard
When you do "git clone git://location", all branches and tags are fetched.
In order to work on top of a specific remote branch, assuming it's the origin remote:
git checkout -b branch origin/branchname
This isn't too much complicated, very simple and straight forward steps are as follows;
git fetch origin
This will bring all the remote branches to your local.
git branch -a
This will show you all the remote branches.
git checkout --track origin/<branch you want to checkout>
Verify whether you are in the desired branch by the following command;
git branch
The output will like this;
*your current branch
some branch2
some branch3
Notice the * sign that denotes the current branch.
This variation will clone a remote repo with all branches available locally without having to checkout each branch one by one. No fancy scripts needed.
Make a folder with the same name of the repo you wish to clone and cd into for example:
mkdir somerepo
cd somerepo
Now do these commands but with actual repo usersname/reponame
git clone --bare [email protected]:someuser/somerepo.git .git
git config --bool core.bare false
git reset --hard
git branch
Voiala! you have all the branches there!
If you use BitBucket,
you can use import Repository, this will import all git history ( all the branches and commits)
If you’re looking for a self-contained clone or backup that includes all remote branches and commit logs, use:
git clone http://[email protected]
git pull --all
The accepted answer of git branch -a
only shows the remote branches. If you attempt to checkout
the branches you'll be unable to unless you still have network access to the origin server.
Credit: Gabe Kopley's for suggesting using git pull --all
.
Note:
Of course, if you no longer have network access to the remote/origin
server, remote/origin branches
will not have any updates reflected in them. Their revisions will reflect commits from the date and time you performed the 2 commands above.
To checkout ALL your clone branches to local branches with one command, use one of the bash commands below:
$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done
OR
If your repo has nested branches then this command will take that into account:
for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done
The above commands will checkout
a local branch into your local git repository, named the same as the remote/origin/<branchname>
and set it to --track
changes from the remote branch on the remote/origin
server should you regain network access to your origin repo server once more and perform a git pull
command in the usual way.
This solution worked for me to "copy" a repository to another one:
git merge path/to/source.git --mirror
cd source.git
git remote remove origin
git remote add origin path/to/target.git
git push origin --all
git push origin --tags
On target repository I can see the same branches and tags than the origin repo.
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.
To create a local branch based on a remote branch, do something like:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
Regarding,
$ git checkout -b experimental origin/experimental
using
$ git checkout -t origin/experimental
or the more verbose but easier to remember
$ git checkout --track origin/experimental
might be better, in terms of tracking a remote repository.
git clone --mirror
on the original repo works well for this.
git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin
Better late than never, but here is the best way to do this:
mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard
At this point you have a complete copy of the remote repo with all of it's branches (verify with git branch
). You can use --mirror
instead of --bare
if your remote repo has remotes of its own.
None of these answers cut it, except user nobody is on the right track.
I was having trouble with moving a repo from one server/system to another. When I cloned the repo, it only created a local branch for master so when I pushed to the new remote, only master branch was pushed.
So I found these two methods VERY useful. Hope they help someone else.
Method 1:
git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
Method 2:
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
Using the --mirror
option seems to copy the remote
tracking branches properly.
However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.
git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch
Reference: Git FAQ: How do I clone a repository with all remotely tracked branches?
I'm using Bitbucket, a Repository Hosting Service of Atlassian. So I try to follow their docs. And that works perfectly for me. With the following easy and short commands you can checkout your remote branch.
At first clone your repository, then change into the destination folder. And last but not least fetch and checkout:
git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>
That's it. Here a litte more real world example:
git clone https://[email protected]/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop
You will find detail information about the commands in the docs: Clone Command, Fetch Command, Checkout Command
Use aliases. Though there aren't any native Git one-liners, you can define your own as
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
and then use it as
git clone-branches
Looking at one of answers to the question I noticed that it's possible to shorten it:
for branch in `git branch -r | grep -v 'HEAD\|master'`; do
git branch --track ${branch##*/} $branch;
done
But beware, if one of remote branches is named as e.g. admin_master it won't get downloaded!
Thanks to bigfish for original idea
Here is another short one-liner command which creates local branches for all remote branches:
(git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout
It works also properly if tracking local branches are already created.
You can call it after the first git clone
or any time later.
If you do not need to have master
branch checked out after cloning, use
git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout
I wrote this small Powershell functions to be able to checkout all my git branches, that are on origin remote.
Function git-GetAllRemoteBranches {
iex "git branch -r" <# get all remote branches #> `
| % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> `
| % { Out-Null; $matches['name'] } <# write does names #>
}
Function git-CheckoutAllBranches {
git-GetAllRemoteBranches `
| % { iex "git checkout $_" } <# execute ' git checkout <branch>' #>
}
More git functions can be found on my git settings repo
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax. You can do:
git checkout somebranch
Git will automatically do the right thing:
$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'
Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. From the git-checkout man page of Git 1.8.2.1:
If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to
$ git checkout -b <branch> --track <remote>/<branch>
This solution worked for me to "copy" a repository to another one:
git merge path/to/source.git --mirror
cd source.git
git remote remove origin
git remote add origin path/to/target.git
git push origin --all
git push origin --tags
On target repository I can see the same branches and tags than the origin repo.
If you use BitBucket,
you can use import Repository, this will import all git history ( all the branches and commits)
As of early 2017, the answer in this comment works:
git fetch <origin-name> <branch-name>
brings the branch down for you. While this doesn't pull all branches at once, you can singularly execute this per-branch.
$ mkdir -p -- newproject_folder
$ cd newproject_folder
$ git clone --mirror https://github.com/USER_NAME/RepositoryName.git .git
$ git config --bool core.bare false
$ git reset --hard
I'm going to add my 2 cents here because I got here trying to find out how to pull down a remote branch I had deleted locally. Origin was not mine, and I didn't want to go through the hassle of re-cloning everything
This worked for me:
assuming you need to recreate the branch locally:
git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name
So if I forked from gituser/master to sjp and then branched it to sjp/mynewbranch it would look like this:
$ git checkout -b mynewbranch
$ git branch -a
master
remotes/sjp/master
remotes/sjp/mynewbranch
$ git fetch (habit to always do before)
$ git rebase remotes/sjp/mynewbranch
A better alternative solution for developers using vscode is to use Git Shadow Extension. This vscode extension allows cloning repository content and directories, that can be filtered by branch name or commit hash. That way, branches or commits can be used as boilerplates/templates for new projects.
This Bash script helped me out:
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a
git fetch --all
git pull --all
to be sure.
One liner:
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
As usual: test in your setup before copying rm -rf universe as we know itCredits for one-liner go to user cfi
A little late to the party, but I think this does the trick:
mkdir YourRepo
cd YourRepo
git init --bare .git # create a bare repo
git remote add origin REMOTE_URL # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/* # fetch tags
git init # reinit work tree
git checkout master # checkout a branch
If this does something undesirable, I'd love to know. However, so far, this works for me.
Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs
) from one or more other repositories along with the objects necessary to complete their histories. In other words it fetches the objects which are reachable by the objects that are already downloaded. See: What does git fetch
really do?
Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all
/git fetch --all
won't help in that case, but you can list them by:
git ls-remote -h -t origin
and fetch them manually by knowing the ref names.
So to fetch them all, try:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.
Then check all your branches again:
git branch -avv
If above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):
$ git remote -v show origin
...
Remote branches:
master tracked
by git remote set-branches
like:
git remote set-branches --add origin missing_branch
so it may appear under remotes/origin
after fetch:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
If you still cannot get anything other than the master branch, check the followings:
git remote -v
), e.g.
git config branch.master.remote
is origin
.origin
points to the right URL via: git remote show origin
(see this post).I'm using Bitbucket, a Repository Hosting Service of Atlassian. So I try to follow their docs. And that works perfectly for me. With the following easy and short commands you can checkout your remote branch.
At first clone your repository, then change into the destination folder. And last but not least fetch and checkout:
git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>
That's it. Here a litte more real world example:
git clone https://[email protected]/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop
You will find detail information about the commands in the docs: Clone Command, Fetch Command, Checkout Command
OK, when you clone your repo, you have all branches there...
If you just do git branch
, they are kind of hidden...
So if you'd like to see all branches name, just simply add --all
flag like this:
git branch --all
or git branch -a
If you just checkout to the branch, you get all you need.
But how about if the branch created by someone else after you clone?
In this case, just do:
git fetch
and check all branches again...
If you like to fetch and checkout at the same time, you can do:
git fetch && git checkout your_branch_name
Also created the image below for you to simplify what I said:
Use my tool git_remote_branch (you need Ruby installed on your machine). It's built specifically to make remote branch manipulations dead easy.
Each time it does an operation on your behalf, it prints it in red at the console. Over time, they finally stick into your brain :-)
If you don't want grb to run commands on your behalf, just use the 'explain' feature. The commands will be printed to your console instead of executed for you.
Finally, all commands have aliases, to make memorization easier.
Note that this is alpha software ;-)
Here's the help when you run grb help:
git_remote_branch version 0.2.6 Usage: grb create branch_name [origin_server] grb publish branch_name [origin_server] grb rename branch_name [origin_server] grb delete branch_name [origin_server] grb track branch_name [origin_server] Notes: - If origin_server is not specified, the name 'origin' is assumed (git's default) - The rename functionality renames the current branch The explain meta-command: you can also prepend any command with the keyword 'explain'. Instead of executing the command, git_remote_branch will simply output the list of commands you need to run to accomplish that goal. Example: grb explain create grb explain create my_branch github All commands also have aliases: create: create, new delete: delete, destroy, kill, remove, rm publish: publish, remotize rename: rename, rn, mv, move track: track, follow, grab, fetch
Use my tool git_remote_branch (you need Ruby installed on your machine). It's built specifically to make remote branch manipulations dead easy.
Each time it does an operation on your behalf, it prints it in red at the console. Over time, they finally stick into your brain :-)
If you don't want grb to run commands on your behalf, just use the 'explain' feature. The commands will be printed to your console instead of executed for you.
Finally, all commands have aliases, to make memorization easier.
Note that this is alpha software ;-)
Here's the help when you run grb help:
git_remote_branch version 0.2.6 Usage: grb create branch_name [origin_server] grb publish branch_name [origin_server] grb rename branch_name [origin_server] grb delete branch_name [origin_server] grb track branch_name [origin_server] Notes: - If origin_server is not specified, the name 'origin' is assumed (git's default) - The rename functionality renames the current branch The explain meta-command: you can also prepend any command with the keyword 'explain'. Instead of executing the command, git_remote_branch will simply output the list of commands you need to run to accomplish that goal. Example: grb explain create grb explain create my_branch github All commands also have aliases: create: create, new delete: delete, destroy, kill, remove, rm publish: publish, remotize rename: rename, rn, mv, move track: track, follow, grab, fetch
A git clone
is supposed to copy the entire repository. Try cloning it, and then run git branch -a
. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo
.
When you do "git clone git://location", all branches and tags are fetched.
In order to work on top of a specific remote branch, assuming it's the origin remote:
git checkout -b branch origin/branchname
This Bash script helped me out:
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
It will create tracking branches for all remote branches, except master (which you probably got from the original clone command). I think you might still need to do a
git fetch --all
git pull --all
to be sure.
One liner:
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
As usual: test in your setup before copying rm -rf universe as we know itCredits for one-liner go to user cfi
Here's a cross-platform PowerShell 7 function adapted from the previous answers.
function Invoke-GitCloneAll($url) {
$repo = $url.Split('/')[-1].Replace('.git', '')
$repo_d = Join-Path $pwd $repo
if (Test-Path $repo_d) {
Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
} else {
Write-Host "`nCloning all branches of $repo..."
git -c fetch.prune=false clone $url -q --progress &&
git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
Write-Host "" #newline
}
}
Note: -c fetch.prune=false
makes it include stale branches that would normally be excluded, remove that if you're not interested in it.
You can make this work with PowerShell 5.1 (the default in Windows 10) by removing &&
from the function, but that makes it try to git pull
even when the previous command failed. So, I strongly recommend just using the cross-platform PowerShell it's always bugging you about trying.
OK, when you clone your repo, you have all branches there...
If you just do git branch
, they are kind of hidden...
So if you'd like to see all branches name, just simply add --all
flag like this:
git branch --all
or git branch -a
If you just checkout to the branch, you get all you need.
But how about if the branch created by someone else after you clone?
In this case, just do:
git fetch
and check all branches again...
If you like to fetch and checkout at the same time, you can do:
git fetch && git checkout your_branch_name
Also created the image below for you to simplify what I said:
Use aliases. Though there aren't any native Git one-liners, you can define your own as
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
and then use it as
git clone-branches
The fetch that you are doing should get all the remote branches, but it won't create local branches for them. If you use gitk, you should see the remote branches described as "remotes/origin/dev" or something similar.
To create a local branch based on a remote branch, do something like:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch. Note that it will fetch all branches, but only pull the one you are on to the top of the tree.
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done
These code will pull all remote branches code to local repo.
all the answers I saw here are valid but there is a much cleaner way to clone a repository and to pull all the branches at once.
When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command
$ git branch -a
you can show all the branches of the repository, and with the command
$ git checkout -b branchname origin/branchname
you can then "download" them manually one at a time.
However, when you want to clone a repo with a lot of branches all the ways illustrated are above are lengthy and tedious in respect to a much cleaner and quicker way that I am going to show, though it's a bit complicated. You need three steps to accomplish this:
create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:
$ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
$ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.
switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:
$ git config --bool core.bare false
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.
$ git reset --hard
So now you can just type the command "git branch" and you can see that all the branches are downloaded.
This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.
Here's an answer that uses awk. This method should suffice if used on a new repo.
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
Existing branches will simply be checked out, or declared as already in it, but filters can be added to avoid the conflicts.
It can also be modified so it calls an explicit git checkout -b <branch> -t <remote>/<branch>
command.
This answer follows Nikos C.'s idea.
Alternatively we can specify the remote branch instead. This is based on murphytalk's answer.
git branch -r | awk '{ system("git checkout -t " $NF) }'
It throws fatal error messages on conflicts but I see them harmless.
Both commands can be aliased.
Using nobody's answer as reference, we can have the following commands to create the aliases:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
Personally I'd use track-all
or track-all-branches
.
I wrote this small Powershell functions to be able to checkout all my git branches, that are on origin remote.
Function git-GetAllRemoteBranches {
iex "git branch -r" <# get all remote branches #> `
| % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> `
| % { Out-Null; $matches['name'] } <# write does names #>
}
Function git-CheckoutAllBranches {
git-GetAllRemoteBranches `
| % { iex "git checkout $_" } <# execute ' git checkout <branch>' #>
}
More git functions can be found on my git settings repo
You only need to use "git clone" to get all branches.
git clone <your_http_url>
Even though you only see master branch, you can use "git branch -a" to see all branches.
git branch -a
And you can switch to any branch which you already have.
git checkout <your_branch_name>
Don't worry that after you "git clone", you don't need to connect with the remote repo, "git branch -a" and "git checkout " can be run successfully when you close your wifi. So it is proved that when you do "git clone", it already has copied all branches from the remote repo. After that, you don't need the remote repo, your local already has all branches' codes.
Looking at one of answers to the question I noticed that it's possible to shorten it:
for branch in `git branch -r | grep -v 'HEAD\|master'`; do
git branch --track ${branch##*/} $branch;
done
But beware, if one of remote branches is named as e.g. admin_master it won't get downloaded!
Thanks to bigfish for original idea
Better late than never, but here is the best way to do this:
mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard
At this point you have a complete copy of the remote repo with all of it's branches (verify with git branch
). You can use --mirror
instead of --bare
if your remote repo has remotes of its own.
I needed to do exactly the same. Here is my Ruby script.
#!/usr/bin/env ruby
local = []
remote = {}
# Prepare
%x[git reset --hard HEAD]
%x[git checkout master] # Makes sure that * is on master.
%x[git branch -a].each_line do |line|
line.strip!
if /origin\//.match(line)
remote[line.gsub(/origin\//, '')] = line
else
local << line
end
end
# Update
remote.each_pair do |loc, rem|
next if local.include?(loc)
%x[git checkout --track -b #{loc} #{rem}]
end
%x[git fetch]
Source: Stackoverflow.com