I cloned a Git repository, which contains about five branches. However, when I do git branch
I only see one of them:
$ git branch
* master
|‾‾‾‾‾‾‾‾‾‾‾‾‾fetch/clone‾‾‾‾‾‾‾‾‾‾‾‾↓ |‾‾‾‾‾‾‾‾‾‾‾‾checkout‾‾‾‾‾‾‾‾‾‾↓
|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾pull‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾↓
Remote repository (`origin`) <=> Local repository <=> Index <=> Workspace
↑_________________push_______________| ↑____commit____| ↑____add_____|
# 拉取远程仓库所有分支信息 → 本地仓库
# fetch all remote repository branch meta → local repository
git remote set-branches origin '*'
git fetch -v
# 把所有远程分支数据搞到本地
# fetch all remote repository branch data → local repository
git branch -r | grep -v '\->' | while read remote; do git branch "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
Based on the answer by Learath2, here's what I did after doing git clone [...]
and cd
-ing into the created directory:
git branch -r | grep -v master | awk {print\$1} | sed 's/^origin\/\(.*\)$/\1 &/' | xargs -n2 git checkout -b
Worked for me but I can't know it'll work for you. Be careful.
To list remote branches:
git branch -r
You can check them out as local branches with:
git checkout -b LocalName origin/remotebranchname
You will need to create local branches tracking remote branches.
Assuming that you've got only one remote called origin
, this snippet will create local branches for all remote tracking ones:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
After that, git fetch --all
will update all local copies of remote branches.
Also, git pull --all
will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.
Track all branches that exist in the remote repo.
Manually do it:
You would replace <branch>
with a branch that is displayed from the output of git branch -r
.
git branch -r
git branch --track <branch>
Do it with a bash script
for i in $(git branch -r | grep -vE "HEAD|master"); do git branch --track ${i#*/} $i; done
This fetches updates on branches from the remote repo which you are tracking in your local repo. This does not alter your local branches. Your local git repo is now aware of things that have happened on the remote repo branches. An example would be that a new commit has been pushed to the remote master, doing a fetch will now alert you that your local master is behind by 1 commit.
git fetch --all
Does a fetch followed by a merge for all branches from the remote to the local branch. An example would be that a new commit has been pushed to the remote master, doing a pull will update your local repo about the changes in the remote branch and then it will merge those changes into your local branch. This can create quite a mess due to merge conflicts.
git pull --all
For Windows users using PowerShell:
git branch -r | ForEach-Object {
# Skip default branch, this script assumes
# you already checked-out that branch when cloned the repo
if (-not ($_ -match " -> ")) {
$localBranch = ($_ -replace "^.*?/", "")
$remoteBranch = $_.Trim()
git branch --track "$localBranch" "$remoteBranch"
}
}
git fetch --all
git pull --all