0

I have recently forked a remote copy of the GIT repository, the remote version has 3 branches development, staging, master.

Now i want to get the latest from the remote's staging branch, hence i tried to switch to the local repo's staging branch to take the updates from remot repo's staging branch.

I used below command to switch to staging branch git checkout staging but it was throws an error pathspec staging does not match any file(s) known to git, however when i try git checkout origin/staging it does checkout but the branch name is some random characters like 1d63d7d

When i tried to list branches in my local repo's copy, it just lists development.

Can anyone tell me how shall i switch to staging branch within the local repo.
Abbas
  • 4,948
  • 31
  • 95
  • 161

1 Answers1

0

From what you've described it sounds like there currently is no staging branch within the local repo. Luckily, it's pretty easy to create one:

git checkout -b staging --track origin/staging

This will create and check out a new branch called staging based on origin/staging, and set it to track the remote branch origin/staging.

You can read more about the -b and --track options in the documentation for git checkout.

It should be noted that, according to the docs, normally git checkout staging should have created a local branch for you automatically. For some reason in this case it didn't. I'm not sure why, but it sounds from the docs like it could happen when if you have more than one remote with a staging branch on it. Relevant quote:

git checkout <branch>

To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

If is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to

$ git checkout -b <branch> --track <remote>/<branch>
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • Thanks for the quick reply, can you tell me what's the difference between upstream and origin, i believe upstream will be the remote repo and origin will be of local repo, is it? – Abbas Nov 03 '15 at 20:24
  • Upstream and origin are both usually used as names for remote repositories. (Not your local checkout.) Typically when people say "upstream" they mean the "master" repository that everyone is pushing their changes to. `origin` is the default name git gives by default to the remote repository you cloned from. It might be the upstream repository, or it might be your own fork of the upstream repository. In both of those cases though it's a remote repository, not your local clone. – Ajedi32 Nov 03 '15 at 20:30
  • Actually i had fork the original repo and the original repo already had those 3 branches, then why those branches didn't appeared in my local clone when i forked it? – Abbas Nov 03 '15 at 20:52
  • `git clone` by default only creates a local branch for `master` when you clone a repo. All the other branches _are_ downloaded from the remote, but they only exist as remote tracking branches until you create a local tracking branch for them. More info: https://git-scm.com/docs/git-clone and https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches – Ajedi32 Nov 03 '15 at 21:05