How do I check out a remote Git branch?
Asked Answered
K

43

8621

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r. How do I check out the remote test branch? I've tried:

  • git checkout test, which does nothing
  • git checkout origin/test gives * (no branch)
Kruse answered 23/11, 2009 at 14:23 Comment(10)
I feel like I'm taking crazy pills. I'm trying to checkout a branch from an upstream, not just origin, and every recommended answer doesn't do anything remotely helpful (pun-intended). EDIT - excuse me, the multitude of suggestions contained in the top 2 answers were useless; 3rd one (git branch test origin/test) is what works. Glad the top 2 have 20x the number of votes...Emelia
Maybe useful to someone else: When I used the Atom editor UI to fetch and pull changes, it pulled changes on the "main" branch but did not create a local reference to the second remote branch. Using git fetch on the command line created that reference, then I was able to checkout the branch as per several answers.Manger
From the first answer, if there is a single remote, the following works : git fetch, followed by git switch testWalworth
This question needs renaming, it is poorly aligned with the content and google-bombs other questions on SO that better handle the topic (e.g. every answer here failed for me - but they're all answering different interpretations of the question, so that's no surprise). For future readers: don't do anything in this SO question/answers! Close this tab and re-search SO, find a different question on same topic.Silicium
git fetch then git switchForgave
At least in modern (2022) git context, git fetch origin test would be a necessary command before git checkout origin/test.Poliard
For me that helped: https://mcmap.net/q/11973/-fetch-in-git-doesn-39-t-get-all-branchesFactotum
This answer should be archived. It's not useful to show people answers this old. Stackoverflow should have an updated answer button.Subtonic
What I did today: I have another branch test created from another machine, where I pushed to a remote branch. For some reason I can't access that machine anymore and I tried "git checkout test" from another machine. It didn't work and said "error: pathspec 'test' did not match any file(s) known to git". Then I ran "git pull", then "git checkout test" again, and I was able to switch to that branch now: "Branch 'test' set up to track remote branch 'test' from 'origin'."Cioffred
In my case, I was just spelling the remote branch name WRONG, if none of the methods here worked for you, check you spelling...Cene
A
11635

The answer has been split depending on whether there is one remote repository configured or multiple. The reason for this is that for the single remote case, some of the commands can be simplified as there is less ambiguity.

Updated for Git 2.23: For older versions, see the section at the end.

With One Remote

In both cases, start by fetching from the remote repository to make sure you have all the latest changes downloaded.

$ git fetch

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a

...
remotes/origin/test

The branches that start with remotes/* can be thought of as read only copies of the remote branches. To work on a branch you need to create a local branch from it. This is done with the Git command switch (since Git 2.23) by giving it the name of the remote branch (minus the remote name):

$ git switch test

In this case Git is guessing (can be disabled with --no-guess) that you are trying to checkout and track the remote branch with the same name.

With Multiple Remotes

In the case where multiple remote repositories exist, the remote repository needs to be explicitly named.

As before, start by fetching the latest remote changes:

$ git fetch origin

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in with -c to create a new local branch:

$ git switch -c test origin/test

For more information about using git switch:

$ man git-switch

Prior to Git 2.23

git switch was added in Git 2.23, prior to this git checkout was used to switch branches.

To checkout out with only a single remote repository:

git checkout test

if there are multiple remote repositories configured then it becomes a bit longer

git checkout -b test <name of remote>/test
Ascribe answered 23/11, 2009 at 14:26 Comment(11)
To expand on this: git doesn't allow you to work on someone else's branches. You can only work on your own. So if you want to add to someone else's branch, you need to create your own "copy" of that branch, which is what the above command does (well, it creates your branch and checks it out, too).Ingraham
If it's a new remote branch you may need to git fetch before doing this so that git is aware of origin/testForman
...and you would do this with git fetch origin testSakti
Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?"Maples
git checkout test will NOT work in modern git if you have multiple remotes which have the same branch name. It can't know which one to use.Lalonde
important note - making sure you have no changes is obvious, but also make sure that you don't have any untracked files changes. For regular checkouts it didn't bother me, but in this case, it did. git clean -nfd to see what would be removed and then git clean -fd to remove it.Broca
git fetch origin fetches only a subset of the branches on the remote, not all of them. I have no idea how it chooses the branches it picks or what causes it to ignore a remote branch, but it only fetched about half the branches from my remote. To get the one I needed I had to specify it by name: git fetch origin my-branch. And then it wouldn't let me check the branch out.Luthanen
Here, in the post-2019 future, this has become as simple as git switch remoteBranch. More on switch vs checkout.Younker
Thanks a lot @BradTurek, I have updated the answer to use git switch instead of git checkout.Ascribe
For me that helped: https://mcmap.net/q/11973/-fetch-in-git-doesn-39-t-get-all-branchesFactotum
The docs for git-switch on version 2.42.0 still have the all-caps warning "THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE." Seems worth mentioning in any answer that advocates using it.Matriarchy
P
1526

Sidenote: With modern Git (>= 1.6.6), you are able to use just

git checkout test

(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.


The * (no branch) in git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:

git checkout -b test HEAD

A more modern approach as suggested in the comments:

@Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) – Jakub Narębski Jan 9 '14 at 8:17

emphasis on git checkout origin/test

Paulitapaulk answered 24/11, 2009 at 0:17 Comment(6)
Unsurprising, but this version has been released in the last few years - knowing this syntax can save a lot of time since there's still a lot of old documentation and comment threads floating around that suggest the older method for doing this.Deuterogamy
"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros.Thwart
@aidan If you get a response like error: pathspec 'branch_name' did not match any file(s) known to git. then you should do a git fetch first.Albuminuria
Using git version 1.8.3.msysgit.0 and this doesn't work for me - did not match any file(s) known to git - I've done many git fetchesEurasian
@Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream)Cirrocumulus
git checkout -b test FETCH_HEAD worked for me.Gile
S
796

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.

To create the local branch and switch to it, use:

$ git checkout -b test origin/test
Sideswipe answered 23/11, 2009 at 14:27 Comment(3)
This will create a local branch without switching to it.Pisci
In your bash git checkout -b test/origin/test suggestion above, you made want to precede it with a bash git fetch upstream to insure that your local repository knows the remote branch test is there. Otherwise, thanks, everything works as advertised.Colonize
This is what I was actually trying to accomplish. When I used the other commands I wound up in a detached HEAD state which is not what I was looking for. Thanks!Allowance
S
576

Accepted answer not working for you?

While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

$ git checkout -b remote_branch origin/remote_branch

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

Solution

If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.

Savagism answered 7/12, 2012 at 20:52 Comment(0)
W
405

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.

Wardlaw answered 18/10, 2013 at 5:55 Comment(5)
This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why.Providence
This was the most direct way to accomplish what I needed which was to use a remote branch (not master) to create a new branch.Ovaritis
this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0Roping
I had a branch of that form: "xx/xx" and This was the solution for me.Freddyfredek
Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0)Armstrong
S
126

You basically see the branch, but you don't have a local copy of that yet!...

You need to fetch the branch...

You can simply fetch and then checkout to the branch, use the one line command below to do that:

git fetch && git checkout test

I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:

git fetch

Sacred answered 5/9, 2017 at 14:20 Comment(0)
O
125

This will DWIM for a remote not named origin (documentation):

$ git checkout -t remote_name/remote_branch

To add a new remote, you will need to do the following first:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

The first tells Git the remote exists, the second gets the commits.

Outright answered 27/4, 2012 at 22:37 Comment(0)
D
125

Use:

git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>

Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.

Dement answered 21/2, 2016 at 10:58 Comment(2)
Looking at it now, they do overlap. Only mine is succinct and tells you what to do rather than tell a story. I assume it can be more useful therefore, especially for nowadays git versions. You can downvote it if you think it is a bad answer.Dement
After reading the other answers and getting more and more confused, this is the one that actually worked for me :)Emelinaemeline
B
78

To clone a Git repository, do:

git clone <either ssh url /http url>

The above command checks out all of the branches, but only the master branch will be initialized. If you want to checkout the other branches, do:

git checkout -t origin/future_branch (for example)

This command checks out the remote branch, and your local branch name will be same as the remote branch.

If you want to override your local branch name on checkout:

git checkout -t -b enhancement origin/future_branch

Now your local branch name is enhancement, but your remote branch name is future_branch.

Behn answered 21/1, 2013 at 10:4 Comment(0)
R
71

I always do:

git fetch origin && git checkout --track origin/branch_name

Referee answered 13/1, 2021 at 11:59 Comment(0)
K
49

I was stuck in a situation seeing error: pathspec 'desired-branch' did not match any file(s) known to git. for all of the suggestions above. I'm on Git version 1.8.3.1.

So this worked for me:

git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD

The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:

git fetch origin desired-branch

From github.com:MYTEAM/my-repo
    * branch            desired-branch -> FETCH_HEAD
Karyotin answered 10/10, 2018 at 21:52 Comment(3)
I had the same problem when trying to check out the remote branch in a submodule in a worktree. Anyone knows what's the reason for that?Lovegrass
This is very useful if some of the branches are very big, and you don't necessarily want to download them all.Reichstag
This fixed it for me when I was trying to locally checkout a branch in the upstream repo, but the situation was so strange. Normally I can fetch all the upstream branches with a simple git fetch upstream, but this wasn't working. So I specifically named the branch I wanted, which resulted in it writing to FETCH_HEAD. I've never seen this before. I'm using Git 2.43.0. I did clone the repo using gh-cli, so maybe that affects the behavior of git fetch??Blearyeyed
M
48

You can try

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

or

git fetch
git checkout -b local_branch_name origin/branch_name
Meridith answered 24/3, 2014 at 13:11 Comment(2)
FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer.Palinode
This comment worked for me, thank you! git checkout -b local_branch_name origin/branch_nameTompkins
M
40

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.

Meneau answered 18/5, 2017 at 13:55 Comment(1)
This has never worked for me. I getting an error telling me <remote branch name> is not a commit and <local branch name> cannot be created from it.Towel
B
36

I use the following command:

git checkout --track origin/other_remote_branch
Binder answered 6/9, 2017 at 14:41 Comment(1)
This answer would be a lot more useful if you explain why you are using it this way. i.e. why someone should use '--track' and so on...Bucher
M
34

The git remote show <origin name> command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.

Example:

git remote show origin

Use these steps to fetch remote branches:

git fetch <origin name> <remote branch name>:<local branch name>
git checkout <local branch name > (local branch name should the name that you given fetching)

Example:

git fetch origin test:test
git checkout test
Mark answered 7/1, 2018 at 13:12 Comment(3)
Good lord, thank you. Been using git for a decade but was on a giant repo drowning all the sudden, just trying to check out a branch... This got me going!Indented
Best answer for meArticulate
I prefer this because I don't have to fetch all branches from the remote, just the one I want.Apivorous
C
31

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

Crisper answered 21/4, 2016 at 19:10 Comment(0)
B
31

Simply run git checkout with the name of the remote branch. Git will automatically create a local branch that tracks the remote one:

git fetch
git checkout test

However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use. In that case you can use either:

git checkout --track origin/test

or

git checkout -b test origin/test

In 2.19, Git learned the checkout.defaultRemote configuration, which specifies a remote to default to when resolving such an ambiguity.

Boole answered 13/9, 2018 at 12:21 Comment(0)
B
26

There are many alternatives, for example:

  • Alternative 1:

    git fetch && git checkout test
    

    It's the simplest way.

  • Alternative 2:

    git fetch
    git checkout test
    

    It's the same, but in two steps.

Butene answered 30/10, 2019 at 10:56 Comment(1)
This one worked for me easily.Ezra
M
25

None of these answers worked for me. This worked:

git checkout -b feature/branch remotes/origin/feature/branch
Macaw answered 10/7, 2018 at 2:11 Comment(0)
T
24

If the branch is on something other than the origin remote I like to do the following:

$ git fetch
$ git checkout -b second/next upstream/next

This will checkout the next branch on the upstream remote in to a local branch called second/next. Which means if you already have a local branch named next it will not conflict.

$ git branch -a
* second/next
  remotes/origin/next
  remotes/upstream/next
Tight answered 1/3, 2013 at 10:0 Comment(0)
R
22

git fetch && git checkout your-branch-name

Ravid answered 26/3, 2014 at 7:0 Comment(2)
How does this magic work?Smearcase
@PeterMortensen There are two commands, git fetch fetches all information from remove and it also fetches the latest branches available. Then git checkout your-branch-name checkouts the branchRavid
S
17

git branch -r says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:

git remote update

And then try checking out your remote branch again.

This worked for me.

I believe git fetch pulls in all remote branches, which is not what the original poster wanted.

Shumate answered 6/6, 2013 at 12:21 Comment(1)
FYI, git remote update will also fetch all remote branches.Palinode
M
17

TL;DR

Using git switch rather than git checkout. More details are on this page.

I think the answer is obsolete. Git split some functions of checkout to switch and restore now.

The following is my summary:

If you want to update something for a remote branch, you should create a local branch to "track" the remote branch. You can update anything you want in local and finally push to remote. If you check out to the remote branch directly after cloning your repository, you may see the "detached HEAD" status and the following message from Git:

Note: switching to 'origin/asd'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d3e1083 Update a

So how can we create a local branch to track a remote branch?

To create a local branch to track a remote branch, you can use git checkout <remote branch name> or git switch <remote branch name>. If you have a file or folder has same name as your remote branch name, git checkout would output some error message, but git switch can work normally!

Example:

  1. See all branches, and we want to create a local branch to track the remote branch remotes/origin/asd, and we also have the file name asd:

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/asd
      remotes/origin/ereres
      remotes/origin/master
      remotes/origin/zxc
    $ ls
    a  asd
    
  2. The filename is same as remote branch, and Git should output some error messages if we are using the git checkout command to create a local branch to track a remote branch

    $ git checkout asd
    fatal: 'asd' could be both a local file and a tracking branch.
    Please use -- (and optionally --no-guess) to disambiguate
    
  3. It works if we are using git switch!

    $ git switch ereres
    Branch 'ereres' set up to track remote branch 'ereres' from 'origin'.
    Switched to a new branch 'ereres'
    
    $ git branch -vv
    * ereres 3895036 [origin/ereres] Update a
      master f9e24a9 [origin/master] Merge branch 'master' of
    
Mussulman answered 16/9, 2021 at 3:4 Comment(4)
not work with 2.17.1, no command found for "git switch" . @Ascribe 's answer works for me!Dactylogram
@Dactylogram No command since your Git version < 2.23. You can refer to this link to see more details.Mussulman
Re "the answer is obsolete": What answer? The accepted answer? Several answers? Which ones?Smearcase
The accepted answer, but the accepted answer already updated for Git 2.23 at Dec 19, 2021.Mussulman
H
15

Fetch from the remote and checkout the branch.

git fetch <remote_name> && git checkout <branch_name> 

E.g.:

git fetch origin && git checkout feature/XYZ-1234-Add-alerts

Heartstrings answered 21/5, 2018 at 11:33 Comment(0)
T
14

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,

  1. Git looks on xxx as a branch name at first, but there isn't any branch named test.
  2. Then Git thinks 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.
  3. If there isn't file named 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.
Tribune answered 10/7, 2017 at 7:45 Comment(0)
L
14

To get newly created branches

git fetch

To switch into another branch

git checkout BranchName
Lalitta answered 15/3, 2018 at 8:49 Comment(0)
P
14

To get all remote branches, use this:

git fetch --all

Then check out to the branch:

git checkout test
Pin answered 23/9, 2019 at 6:46 Comment(0)
H
14

git checkout -b "Branch_name" [ B means Create local branch]

git branch --all

git checkout -b "Your Branch name"

git branch

git pull origin "Your Branch name"

successfully checkout from the master branch to dev branch

enter image description here

Hoagland answered 10/12, 2019 at 6:29 Comment(0)
C
13

It seems to my that no one suggested the simplest way (or maybe I'm too dumb to think this is "a way"). But anyway, try this:

git pull origin remoteBranchName
git switch remoteBranchName

This worked for me in the same case (a branch created on the remote after my last pull request).

Chamkis answered 13/1, 2022 at 20:5 Comment(0)
C
12

For us, it seems the remote.origin.fetch configuration gave a problem. Therefore, we could not see any other remote branches than master, so git fetch [--all] did not help. Neither git checkout mybranch nor git checkout -b mybranch --track origin/mybranch did work, although it certainly was at remote.

The previous configuration only allowed master to be fetched:

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

Fix it by using * and fetch the new information from origin:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

Now we could git checkout the remote branch locally.

I don't have any idea how this configuration ended up in our local repository.

Continuance answered 20/9, 2019 at 10:20 Comment(0)
C
11

I used that one:

git clean -fxd                         # removes untracked (new added plus ignored files)

git fetch
git checkout {branchname}

git reset --hard origin/{branchname}   # removes staged and working directory changes
Chemism answered 19/8, 2020 at 7:28 Comment(0)
H
9

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 ;
Homograph answered 11/8, 2017 at 12:6 Comment(0)
B
9

For some reason, I couldn't do:

git checkout -b branch-name origin/branch-name

It was throwing the error:

fatal: 'origin/branch-name' is not a commit and a branch 'branch-name' cannot be created from it

I had to do:

git checkout -b branch-name commit-sha
Beal answered 9/9, 2021 at 9:1 Comment(2)
Maybe you did not git fetch all branches from origin? Note that git switch is preferred to git checkout nowadays: https://mcmap.net/q/12203/-39-git-checkout-39-docs-claim-working-tree-will-change-why-are-edits-not-discardedEngedus
git fetch --all; for branch in git branch -r --format="%(refname:short)" | sed 's/origin\///'; do git branch --track "$branch" "origin/$branch" ; done ;Languish
C
8

git fetch --all

would fetch all the remote branches to your local

git checkout test

would switch you to the test branch

Contrast answered 12/6, 2021 at 11:17 Comment(0)
G
7

If the remote branch name begins with special characters you need to use single quotes around it in the checkout command, or else Git won't know which branch you are talking about.

For example, I tried to checkout a remote branch named #9773, but the command didn't work properly, as shown in the picture below:

Enter image description here

For some reason, I wondered if the sharp symbol (#) could have something to do with it, and then I tried surrounding the branch name with single quotes, like '#9773' rather than just #9773, and fortunately it worked fine.

git checkout -b '#9773' origin/'#9773'
Gravestone answered 7/11, 2018 at 11:17 Comment(1)
Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and do the right thing (it covers answers and program output as well). Thanks in advance.Smearcase
S
7

Just run these two commands and you should be good to go.

git checkout <branch-name>
git pull <remote> <branch-name>
Secessionist answered 26/6, 2020 at 5:15 Comment(1)
I think this will fail if the remote branch was created after the previous git pull.Steamy
J
7

Working Commands

  1. git fetch origin 'remote_branch':'local_branch_name'

  2. git switch 'local_branch_name'

  3. git pull origin 'remote_branch':'local_branch_name'

The first one is for fetching the branch and creating a local branch from a remote branch.

The second one is for switching to the local branch.

The third is for pulling the latest changes of remote to the local branch.

Jittery answered 3/8, 2022 at 7:16 Comment(0)
B
4

I tried many answers here but still couldn't checkout to a remote branch, turns out that when I first cloned the repo, I had added --single-branch flag which could be the reason why I was encountering the error when trying to create a new branch from a remote branch, to resolve this problem we can update the repository's configuration to fetch all branches from the remote repository:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch
git branch -r

and now we can continue with

git checkout -b feature/FS origin/feature/FS
Bream answered 28/7, 2023 at 7:14 Comment(0)
P
3

Please follow the command to create an empty folder. Enter that and use this command:

saifurs-Mini:YO-iOS saifurrahman$ git clone your_project_url
Cloning into 'iPhoneV1'...
remote: Counting objects: 34230, done.
remote: Compressing objects: 100% (24028/24028), done.
remote: Total 34230 (delta 22212), reused 15340 (delta 9324)
Receiving objects: 100% (34230/34230), 202.53 MiB | 294.00 KiB/s, done.
Resolving deltas: 100% (22212/22212), done.
Checking connectivity... done.
saifurs-Mini:YO-iOS saifurrahman$ cd iPhoneV1/
saifurs-Mini:iPhoneV1 saifurrahman$ git checkout 1_4_0_content_discovery
Branch 1_4_0_content_discovery set up to track remote branch 1_4_0_content_discovery from origin.
Switched to a new branch '1_4_0_content_discovery'
Pilewort answered 1/1, 2016 at 10:31 Comment(0)
H
3

Use fetch to pull all your remote

   git fetch --all

To list remote branches:

   git branch -r

For list all your branches

   git branch -l
   >>outpots like-
     * develop
       test
       master

To checkout/change a branch

   git checkout master
Heirdom answered 24/5, 2019 at 8:51 Comment(5)
Tried git branch -l and no remote branch was shown.Lyndseylyndsie
use git pull --all or git fetch --allHeirdom
Yes, and after that, git branch -l still shows only local branches. It seems to be working exactly as git branch without -l, so what is the reason for -l?Lyndseylyndsie
Also your answer does not answer the question - master is not a remote branch.Lyndseylyndsie
Re "outpots": Do you mean "outputs"?Smearcase
A
1

You can add a new branch test on local and then use:

git branch --set-upstream-to=origin/test test
Anglesite answered 19/9, 2020 at 2:57 Comment(0)
S
0

I am not sure. I keep doing this day in and day out. The following command works like gem.

dev being the branch you want to checkout.

git fetch && git checkout dev
Sternpost answered 6/3, 2022 at 19:11 Comment(0)
M
-4

In your case, you can use this command:

git checkout -b test origin/test

Manard answered 13/7, 2022 at 7:14 Comment(1)
Hi ! How your answer expands from previous answers?Hite

© 2022 - 2024 — McMap. All rights reserved.