Home > OS >  I can't manage to navigate thought different branches on my GIT project
I can't manage to navigate thought different branches on my GIT project

Time:03-30

I'm working on my first GIT repo, which has several branches. Through the lesson, we have to switch between different branches.

At the moment , I've been cloning on Visual Code Studio the repo, which is working fine. The project opens normally on vscode.

When I try to switch branches, it doesn't work. When I type : git branch it only shows me : * main.

However, there should be more branches on the project! Here is the git link : https://github.com/OpenClassrooms-Student-Center/debuggez-l-interface-de-votre-site/tree/main

CodePudding user response:

If you only see the main branch in your local clone your might want to do a git fetch to get all the other branches that exist in the remote repository.

CodePudding user response:

When I type : git branch it only shows me : * main

By default git branch shows local branches.

When you fetch a repo, its branches start out as remote branches - they're still fetched, but start off only under remotes/repo/branchname.

  • Run git branch [--list] -r|--remote to see these.

When you create a local branch that tracks a remote branch, as is done by default for master -> remotes/origin/master (or in your case for main -> remotes/origin/main), then you have a local branch.

  • Run git branch [--list] to see these.

You want to see all branches:

  • run git branch [--list] -a|--all to see everything.

When you checkout a local branch that doesn't already exist, but matches the name of a remote branch, it will be set up to track the remote correctly even though you couldn't see it when simply running git branch.

When I try to switch branches, it doesn't work

But you didn't try to switch branches. You looked to see what branches existed (without realizing it only showed the local ones) and never actually ran git checkout partie-1/chapitre-1/section-1 or whatever. Right?

You should feel relaxed about trying this even before you knew the above: you can always delete the branch again if something went wrong, and checkout failing has no side-effects whatsoever. If you mis-spell a branch name, it isn't going to ruin your repo or lose your work.

You should also get in the habit of checking the online documentation. Running git help branch shows all these options.


Sample output:

$ git branch
* main

$ git branch -r
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/partie-1/chapitre-1/section-1

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/partie-1/chapitre-1/section-1

PS. The reason for having both local and remote versions of the same branch is that they can diverge: you need to be able to fetch the remote version without altering your local copy, in order to compare them and decide what to do about it (usually merging or rebasing).

  • Related