I cloned a Git repository containing many branches. However, git branch
only shows one:
$ git branch
* master
How would I pull all the branches locally so when I do git branch
, it shows the following?
$ git branch
* master
* staging
* etc...
I cloned a Git repository containing many branches. However, git branch
only shows one:
$ git branch
* master
How would I pull all the branches locally so when I do git branch
, it shows the following?
$ git branch
* master
* staging
* etc...
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
(It seems that pull fetches all branches from all remotes, but I always fetch first just to be sure.)
Run the first command only if there are remote branches on the server that aren't tracked by your local branches.
You can fetch all branches from all remotes like this:
git fetch --all
It's basically a power move.
fetch
updates local copies of remote branches so this is always safe for your local branches BUT:
fetch
will not update local branches (which track remote branches); if you want to update your local branches you still need to pull every branch.
fetch
will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches:
git branch -a
To update local branches which track remote branches:
git pull --all
However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all
:
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
P.S. AFAIK git fetch --all
and git remote update
are equivalent.
Kamil Szot's comment, which folks have found useful.
I had to use:
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
because your code created local branches named
origin/branchname
and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.
git checkout -b localname remotename/remotebranch
–
Alialia for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
because your code created local branches named origin/branchname and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it. –
Salinometer git pull --all; for remote in `git branch -r | grep -v \>`; do git branch --track ${remote#origin/} $remote; done
. The change strips out HEAD. –
Brooder for /F %remote in ('git branch -r') do ( git branch --track %remote) && git fetch --all && git pull --all
–
Jaella git fetch --all
and git remote update
First is that git fetch --all was added in v1.6.6. Second fetch --all will pull from all remote repos. see the commit 9c4a036b3 on github. github.com/git/git/commit/… –
Moneybags while read
trick as this git branch -r | while read remote; do git branch --track "${remote#origin/}" "$remote"; d
and quote the variable expansions! –
Introduce for /F %r in ('git branch -r') do ( git branch --track %r) && git fetch --all && git pull --all
–
Albin origin/
–
Apollyon git branch -r
commands to find all the various branches. Use git branch -r --no-color
to prevent not valid branch names on terminals supporting color codes. –
Tanya git pull --all
just does nothing: "You asked to pull from the remote '--all', but did not specify a branch" –
Basically fetch --all
meant to fetch all remotes? at least it does not work for me (does not fetch new remote branches) –
Threap git pull
only merges into the current branch, even with --all
. So it won't update other tracking branches besides the current. –
Epode git branch -r | grep -v "\->" | foreach {git branch --track $_.Substring($_.IndexOf("/") + 1)}; git pull --all
–
Sculpture for remote in $(git branch -r | grep -v -e "\->" -e master); do git branch --track ${remote#origin/} $remote; done
–
Cryoscopy git fetch
followed by git pull` https://mcmap.net/q/12376/-what-is-the-difference-between-39-git-pull-39-and-39-git-fetch-39 –
Ineligible git fetch --all
and git remote update
are almost the same. However, git remote update
by default fetches the remote specified by remotes.default
. If this is not set (which I believe by default it is not) then it will fetch all. Best to just use git fetch --all
as its behavior is consistent. –
Watch git config --global alias.fa 'branch -r | grep -v ">" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done'
does just display git branch command definition –
Potshot git fetch
explains the background information really well, so now this answer makes sense to me. +1! –
Hildahildagard git fetch origin master:master
, for example. This allows to update index without checking branches out. –
Eighteenth git fetch
? (Serious question, I don't understand how it is one...) –
Vanzant fatal: '?[31morigin/dev?[m' is not a valid branch name
–
Preprandial git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do shopt -s extglob && git branch --track "${remote##!(/)/}" "${remote##+( )}"; done
worked for me –
Wheeled git fetch -a
is actually short for git fetch --append
rather then git fetch --all
, meaning there is no short flag for fetching from all remotes. (The append flag adds fetched content to the FETCH_HEAD, rather then replacing it). I used to run git fetch -taf
which didn't do what I wanted. –
Porett git branch -r
and hard reset to, merge or rebase wrt eg origin/somebranch directly. Creating all of these local branches may not make sense. In my workflow, I rarely use more then two local branches at the same time. –
Porett "s,\x1B\[[0-9;]*[a-zA-Z],,g"
mean? –
Equate s/foo/bar/
replaces "foo" with "bar". This one uses ,
instead of /
. The final g
means replace all rather than just one. \x1B\[[0-9;]*[a-zA-Z]
is a regular expression. It matches the Escape character (\x1B
), followed by an open square bracket (\[
), followed by zero or more (*
) digits or semicolons ([0-9;]
), followed by a letter ([a-zA-Z]
). The whole RE matches sequences that make the terminal change the colour of following text. The command replaces these with empty string (,,
), i.e. deletes them. –
Comprador To list remote branches:
git branch -r
To checkout a remote branch as a local branch:
git checkout -b local_branch_name origin/remote_branch_name
git checkout remotebranchname
and it works. what's the difference with your solution? –
Fallingout git checkout remotebranchname
used to just create a new unrelated branch that is named remotebranchname. –
Alialia fatal: Cannot update paths and switch to branch 'develop' at the same time. Did you intend to checkout 'origin/temp' which can not be resolved as commit?
.That's result when I run answer script, what's the problem? –
Loveinidleness git checkout -b LocalName origin/remotebranchname
–
Warenne git remote update
and then list the remote branches with git branch -r
–
Stokowski git switch -c local_branch_name origin/remote_branch_name
–
Deniable 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.
git pull --all
will update all local tracking branches? As far as I can tell it only updates the current branch from all remotes. –
Epode origin
? See this answer which will work on all remote names. –
Nora If you do:
git fetch origin
then they will be all there locally. If you then perform:
git branch -a
you'll see them listed as remotes/origin/branch-name. Since they are there locally you can do whatever you please with them. For example:
git diff origin/branch-name
or
git merge origin/branch-name
or
git checkout -b some-branch origin/branch-name
--all
) –
Pic git fetch -all
fetches all branches of all remotes. git fetch origin
fetches all branches of the remote origin
. The later is what the OP was asking. –
Pic --all
means "all remotes", not "all branches of a given remote". The latter is implied by any fetch from a remote. –
Jonna git branch -r
displays them, however. –
Warenne $ git remote update
$ git pull --all
This assumes all branches are tracked.
If they aren't you can fire this in Bash:
for remote in `git branch -r `; do git branch --track $remote; done
Then run the command.
Caution: mind the warnings below (all remotes will track the same local branch main
).
->
which will likely exist in the output of git branch -r
as ` origin/HEAD -> origin/master` –
Nora Branch 'origin/quote-filenames' set up to track local branch 'master'.
The desired output is: Branch 'quote-filenames' set up to track remote branch 'quote-filenames' from 'origin'.
This is backwards, setting the origin to track the remote. See this answer for a fix. –
Nora git fetch
wouldn't work. So the lesson here is that you need to track the remote branches. Gracias! –
Brentbrenton git branch -r | grep -v HEAD
; do git branch --track ${REMOTE}; done . –
Dodgson Branch 'origin/app1' set up to track local branch 'master'.
[..] Branch 'origin/app10' set up to track local branch 'master'.
–
Dinger The Bash for
loop wasn't working for me, but this did exactly what I wanted. All the branches from my origin mirrored as the same name locally.
git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'
See Mike DuPont's comment below. I think I was trying to do this on a Jenkins Server which leaves it in detached head mode.
fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository
after a simple clone. Have to detach head first. I did this with git checkout <SHA>
–
Syringomyelia git checkout --detach # detach the head
and then git fetch origin \'+refs/heads/*:refs/heads/*
–
Monocyclic git clone <repo_URI> .git
to just setup a bare repo without content. Then execute the OPs command: git fetch origin '+refs/heads/*:refs/heads/*
. –
Warranty remote.<remote>.fetch
is setting to something restrictive. –
Ulysses .git\refs\heads
as well as .git\refs\remotes\origin
and everything is fine. If you want to get the latest of a branch, you need to switch to that branch via git checkout branchname
. You then do git pull origin branchname
to get the latest for the checked-out branch from origin. –
Zuniga Use git fetch && git checkout RemoteBranchName
.
It works very well for me...
origin/branch
; it suffices to say only branch
). –
Educe 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, there is a much cleaner and quicker way, though it's a bit complicated. You need three steps to accomplish this:
First step
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.
Second step
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
Third Step
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.
You can fetch all the branches by:
git fetch --all
or:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.
To pull all the branches, use:
git pull --all
If above won't work, then precede the above command with:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
as the remote.origin.fetch
could support only a specific branch while fetching, especially when you cloned your repo with --single-branch
. Check this by: git config remote.origin.fetch
.
After that you should be able to checkout any branch.
See also:
To push all the branches to the remote, use:
git push --all
eventually --mirror
to mirror all refs.
If your goal is to duplicate a repository, see: Duplicating a repository article at GitHub.
depth=1
) and the config also specified one specific branch for fetch
- the depth=1000
parameter was the fix that helped me to checkout a specific remote branch –
Lorsung pull --all
doesn't pull all the branches, but all the remotes –
Cordiacordial I usually use nothing else but commands like this:
git fetch origin
git checkout --track origin/remote-branch
A little shorter version:
git fetch origin
git checkout -t origin/remote-branch
If you are here seeking a solution to get all branches and then migrate everything to another Git server, I put together the below process. If you just want to get all the branches updated locally, stop at the first empty line.
git clone <ORIGINAL_ORIGIN>
git branch -r | awk -F'origin/' '!/HEAD|master|main/{print $2 " " $1"origin/"$2}' | xargs -L 1 git branch -f --track
git fetch --all --prune --tags
git pull --all
git remote set-url origin <NEW_ORIGIN>
git pull
<resolve_any_merge_conflicts>
git push --all
git push --tags
<check_NEW_ORIGIN_to_ensure_it_matches_ORIGINAL_ORIGIN>
git fetch git pull
https://mcmap.net/q/12376/-what-is-the-difference-between-39-git-pull-39-and-39-git-fetch-39 –
Ineligible pull
does indeed do a fetch
first but it's easier to tell if the problem is from the fetch
part of pull
or the subsequent merge
part of pull
when fetch
is executed independently. –
Warranty git branch -r | awk -F'origin/' '!/HEAD|master|main/{print $2 " " $1"origin/"$2}' | xargs -L 1 git branch -f --track
–
Warranty 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
Lazy way (this can create a mess due to merge conflicts, be careful):
git checkout master
git pull
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 remoteBranch in $(git branch -r | grep -vE "HEAD|master"); do git branch --track ${remoteBranch#*/} $remoteBranch; done
–
Oarfish xargs
instead of a for
loop you won't need any variable at all. –
Dumpy git branch -r | grep -vE "HEAD|master" | xargs -I remoteBranch -n 1 echo ...remoteBranch...
with echo
replaced by the git branch
command but I see now that the removal of the remote in remoteBranch will require some sort of sed
command maybe making it more complicated. –
Dumpy I believe you have cloned the repository by:
git clone https://github.com/pathOfrepository
Now go to that folder using cd:
cd pathOfrepository
If you type git status
you can see all:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
To see all hidden branch types:
git branch -a
It will list all the remote branchs.
Now if you want to checkout on any particular branch just type:
git checkout -b localBranchName origin/RemteBranchName
Make sure all the remote branches are fetchable in .git/config
file.
In this example, only the origin/production
branch is fetchable, even if you try to do git fetch --all
nothing will happen but fetching the production
branch:
[origin]
fetch = +refs/heads/production:refs/remotes/origin/production
This line should be replaced by:
[origin]
fetch = +refs/heads/*:refs/remotes/origin/*
Then run git fetch
etc...
git config --get remote.origin.fetch
and then to (destructively) set it: git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
–
Schlep After you clone the master repository, you just can execute
git fetch && git checkout <branchname>
Just these three commands will get all the branches:
git clone --mirror repo.git .git (gets just .git - bare repository)
git config --bool core.bare false
git reset --hard
pull --all
. But if still needed, then the other answer bellow, by @Johnno Nola, assuming all branches are tracked, mixed with this answer, is the way to go. –
Karb This has been tested and functions on Red Hat and Git Bash on Windows 10.
TLDR:
for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;
Explanation:
The one liner checks out and then fetches all branches except HEAD.
List the remote-tracking branches.
git branch -r
Ignore HEAD.
grep -v ' -> '
Take branch name off of remote(s).
cut -d"/" -f2
Checkout all branches tracking a single remote.
git checkout $branch
Fetch for checked out branch.
git fetch
Technically the fetch is not needed for new local branches.
This may be used to either fetch
or pull
branches that are both new and have changes in remote(s).
Just make sure that you only pull if you are ready to merge.
Check out a repository with SSH URL.
git clone [email protected]
Before
Check branches in local.
$ git branch
* master
Execute Commands
Execute the one liner.
for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;
After
Check local branches include remote(s) branches.
$ git branch
cicd
master
* preprod
Looping didn't seem to work for me and I wanted to ignore origin/master. Here's what worked for me.
git branch -r | grep -v HEAD | awk -F'/' '{print $2 " " $1"/"$2}' | xargs -L 1 git branch -f --track
After that:
git fetch --all
git pull --all
grep | awk
antipattern: git branch -r | awk -F 'origin/' '!/HEAD|master/{
... –
Sternberg 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
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" } }
–
Inharmonious Here's something I'd consider robust:
HEAD
to track origin/HEAD
origin
for b in $(git branch -r --format='%(refname:short)'); do
[[ "${b#*/}" = HEAD ]] && continue
git show-ref -q --heads "${b#*/}" || git branch --track "${b#*/}" "$b";
done
git pull --all
It's not necessary to git fetch --all
as passing -all
to git pull
passes this option to the internal fetch
.
Credit to this answer.
${b#*/}
- can someone explain the meaning please? –
Consueloconsuetude ${STR#*/}
expands to the full path of STR (i.e. b in the above code). –
Consueloconsuetude git branch -r --format='%(refname:short)'
will output quote marks ('), so use double-quotes : git branch -r --format="%(refname:short)"
–
Consueloconsuetude Be careful while playing with git, go step by step.
$ git remote update //This will update your local
$ git branch -a //This will list all the branches(After updating you can now
see the new branch in the list)
$ git checkout your_branch_name
Set alias: (based on the top answer)
git config --global alias.track-all-branches '!git fetch --all && for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done && git fetch --all'
Now to track all the branches:
git track-all-branches
|‾‾‾‾‾‾‾‾‾‾‾‾‾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
you can fetch all braches by this one line command like this:
git fetch --all && git pull --all && git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
I wrote a little script to manage cloning a new repo and making local branches for all the remote branches.
You can find the latest version here:
#!/bin/bash
# Clones as usual but creates local tracking branches for all remote branches.
# To use, copy this file into the same directory your git binaries are (git, git-flow, git-subtree, etc)
clone_output=$((git clone "$@" ) 2>&1)
retval=$?
echo $clone_output
if [[ $retval != 0 ]] ; then
exit 1
fi
pushd $(echo $clone_output | head -1 | sed 's/Cloning into .\(.*\).\.\.\./\1/') > /dev/null 2>&1
this_branch=$(git branch | sed 's/^..//')
for i in $(git branch -r | grep -v HEAD); do
branch=$(echo $i | perl -pe 's/^.*?\///')
# this doesn't have to be done for each branch, but that's how I did it.
remote=$(echo $i | sed 's/\/.*//')
if [[ "$this_branch" != "$branch" ]]; then
git branch -t $branch $remote/$branch
fi
done
popd > /dev/null 2>&1
To use it, just copy it into your git bin directory (for me, that’s C:\Program Files (x86)\Git\bin\git-cloneall
), then, on the command line:
git cloneall [standard-clone-options] <url>
It clones as usual, but creates local tracking branches for all remote branches.
If you have problems with fetch --all
then track your remote branch:
git checkout --track origin/%branchname%
TLDR
It is likely a bad idea to create a local branch for each remote tracking branch. These different types of branches serve different purposes and local branches are normally created based on their need. git branch
shows local branches git branch -r
shows the remote tracking branches. git branch -a
shows both. You can update all remote tracking branches with an appropriate fetch command. That should usually be all you need.
After scrolling through the existing answers I see two kinds: those simply answering the question asked, rather than suggesting a different approach. And those suggesting a different approach without explaining why. Here is my attempt to explain a bit more.
There are actually three kinds of branches a usual git repository needs to deal with. These three kinds of branches serve different purposes. In short:
remote branches: These are the branches as they exist in the remote repository. You will never read from remote branches directly. All reading of remote branches happens through gits so called "remote tracking branches"
remote tracking branches: git keeps local snapshots of the remote branches which are most accurately called "remote tracking branches". They are updated when you call git fetch
or git pull
(which does a fetch). You can often get away with using the remote tracking branches without creating local branches out of them.
For example:
git merge origin/master
will merge the remote tracking branch origin/master
into the current local branch without requiring you to create a local copy of it first. This means: there is a remote tracking branch called origin/master
which is a snapshot of the branch called master
as it exists on the remote repository called origin
. And this command will merge it into the current checked-out local branch. You may want to do a fetch before performing operations like this.
local branches: these are manually created snapshots of certain points in history (often based on remote tracking branches, at least at first). They are much more static than the other kinds and will really only change when you manually change them. One by one. You will likely want a local branch if you want to view its contents in your working-tree (project directory) or when you want to add commits to it.
Once you are done with a local branch (and published its contents) you may consider deleting it. That way you do not need to keep it up to date. After all, a fetch will not update the current local branch, and a pull will only update the current checked-out local branch. In other words: you should only create local branches when you need them, and you should probably delete them when you no longer do.
It is probably not accurate to say that 'some branches are hidden by default'. Rather the git branch
command was created to show you the "local branches". You can use git branch -r
to list the remote tracking branches and git branch -a
to show both the local branches and the remote tracking branches. These two different types of branches serve a different purpose and you are not likely to need a local branch for each remote tracking one.
Also note that is is generally a bad idea to create local branches whose names begin with the name of a remote followed by a slash (eg creating a local branch called "origin/master" tends to be a bad idea since its name conflicts with the name of the remote tracking branch).
In the context of updating branches, it does make sense to discuss different flavours of fetch commands:
git fetch
: updates only the remote tracking branches whose remote matches the one used in the "upstream" of the current checked out branch. If the checked out branch does not have an upstream set, this falls back to fetching from a remote called "origin" if it exists. This command is the easiest, and is sufficient most of the time.git fetch --all
: updates all remote tracking branches regardless of the remote they belong are snapshotting from.I am especially fond of
git fetch -tf --all
Which will also always update (and override if needed) all tags, including tags not reachable from the remote branches.
We can put all branch or tag names in a temporary file, then do git pull for each name/tag:
git branch -r | grep origin | grep -v HEAD| awk -F/ '{print $NF}' > /tmp/all.txt
git tag -l >> /tmp/all.txt
for tag_or_branch in `cat /tmp/all.txt`; do git checkout $tag_or_branch; git pull origin $tag_or_branch; done
To avoid the error message 'fatal: A branch named 'origin/master' already exists.', you may try my solution:
git branch -r | grep -v '\->' | grep -v `git branch | awk '/\*/ { print $2; }'`| while read remote; do git branch --track "${remote#origin/}" "$remote"; done
Have tried many ways, only this one is simple and works for me.
for branch in $(git ls-remote -h git@<your_repository>.git | awk '{print $2}' | sed 's:refs/heads/::')
do
git checkout "$branch"
git pull
done
Here's a Perl version of the one-liner provided in the accepted answer:
git branch -r | perl -e 'while(<>) {chop; my $remote = $_; my ($local) = ($remote =~ /origin\/(.*)/); print "git branch --track $local $remote\n";}' > some-output-file
You can run the output file as a Shell script if you'd like.
We deleted our Stash project repository by accident. Fortunately someone had created a fork right before the accidental loss. I cloned the fork to my local (will omit the details of how I did that). Once I had the fork fully in my local, I ran one one-liner. I modified the remote's URL (origin in my case) to point to the target repository we were recovering to:
git remote set-url origin <remote-url>
And finally pushed all branches to origin like so:
git push --all origin
and we were back in business.
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.
git remote add origin https://yourBitbucketLink
git fetch origin
git checkout -b yourNewLocalBranchName origin/requiredRemoteBranch (use tab :D)
Now locally your yourNewLocalBranchName
is your requiredRemoteBranch
.
checkout
only creates one local branch, not all. –
Comprador for branch in $(git branch -r); do
echo "BRANCH: $branch --> ${branch#*/}";
git fetch origin ${branch#*/};
done
For Visual Studio Users, On Package Manager console:
git branch | %{ git fetch upstream; git merge upstream/master}
© 2022 - 2024 — McMap. All rights reserved.
--single-branch
setting when cloning: stackoverflow.com/questions/17714159/… (git fetch --all
will never work if you've specified only one branch!) – Torrellgit checkout -b <branch>
seems like the most likely answer. – Pantiegit clone --bare <repo url> .git
(notice you need to add "--bare" and ".git" at the end to clone the repo as a "bare" repo), thengit config --bool core.bare false
(sets the "bare" flag to false), thengit reset --hard
(moves the HEAD to current HEAD on the repo). Now if yougit branch
you should see all branches from the repo you cloned. – Grovergrovesgit fetch origin
will do. – Grinderygit pull origin '*:*'
– Kensell