Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github switch from one branch to another after cloning repository

Tags:

git

github

I'm new to Github. I just cloned a repository, and I believe that when I clone it, all of its branches are copied as well. All I want to do is switch from one branch to another, in the order of the branches. I mainly just want to be able to run my code at each branch, and then switch to the next branch soon after. Basically what I'm asking is, how do I open all of the files associated with each branch so that I can run the code?

like image 276
theevader Avatar asked Aug 12 '15 17:08

theevader


People also ask

Which command is used to switch to other branches?

The git checkout command allows you to switch branches by updating the files in your working tree to match the version stored in the branch that you wish to switch to. You can think of it as a way of switching between different workspaces.

Does git clone Get all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.


2 Answers

Git is an awesome tool, here are some tips that should help you.

This will list all branches that exist. Any that are prefixed with origin/ are on the server and you will need to fetch them.

git branch -a

Run the following to get a remote branch

git checkout BRANCH_NAME
git pull origin BRANCH_NAME

Checkout is what allows you to swap between branches. You can even checkout commits and enter detached head mode, but that is a more complex topic.

When you are done with the work in one branch, you should merge your code back into your master branch or dev branch or whatever you happen to use. Once you have pulled down a branch and have checked it out, your local git repo will contain all the files for that branch. If you checkout another branch, the code will be replaced by the code of the other branch ect...

like image 53
Unome Avatar answered Oct 23 '22 19:10

Unome


When you clone you are pulling down a specific branch. Probably master. Your code base on your file system is that of the branch you are on. Think of branches as features. You have your master branch and want to add a new feature so you create a new feature branch. Once that feature is finished you merge it back into your master branch.

like image 29
koga73 Avatar answered Oct 23 '22 17:10

koga73