Search code examples
gitgithubgit-stash

git checkout branchname is not working


I have created new project into stash repository and also i have created few branches in stash for this new project. I am able to clone the project from git bash but when i try to checkout particular branch i was getting below error

git checkout feature/Project_branch

error: pathspec 'feature/Project_branch' did not match any file(s) known to git.

but if i use as below then its working

git fetch origin

git checkout feature/Project_branch

There are other projects which i used to checkout it was working without using git fetch origin command, but in my new repository case its throwing above error.

Can any one please help me to unsderstand, is there any configuration i have to do to directly checkout the branch with git checkout without using git fetch command?


Solution

  • All git fetch does is download information into your repository that you don't already have. This means that when you were first trying to switch to feature/Project_branch, you hadn't downloaded it yet. You can get it in two ways

    1 - Upon the original git clone, all branches currently associated with the remote repository will be downloaded and made usable on your local git repo.

    2 - If a new branch is added to the remote repository after your git clone, you must perform a git fetch, or a similar action such as git pull, in order to gain access to the branch.

    What must have happened is that the branch you wanted to switch to was not created and pushed when you originally cloned the repository, that is why you had to do a git fetch

    However, from now on, you should not have to run git fetch in order to switch between master and feature/Project_branch.

    Note: git branch will list all branches you have at your disposal in your local repo.