Git Create Branch-Switch Branch-List Branches-Local and Remote-Git Branch Command

Git Create Branch-Switch Branch-List Branches-Local and Remote

in the following examples i will show you how to create a branch locally and remotely and how to push the newly created branch to remote. and how to list the branches.

List the local branches in git

To list the branches in a git we use git branch command

git branch

it will list all the local(working directory) branches in git.

git branch command

you can see in the image that I have one branch name called master.

list all the local and remote branches

git branch command only list the local branches in git not remote branches so to list all local and remote branches in a git we use git branch -a command

git branch -a

it will list all the local and remote branches

git branch create command

you can see in the image that i have one local branch master and one remote branch master

the red color are remote branches.

create a branch in a git or create a local branch

To create a branch in a git use below command.

git branch <branchname>

create a branch in git

git branch devops

So it will create a branch name called devops, and now you can check whether the branch is created or not by listing all branches with git branch command.

Switch to branch

To switch from one branch to another branch we use git checkout command

git checkout  <branchname>

git switch to branch

git checkout devops

I executed this command in master branch

so it will switch from master to devops branch and by using this command we can switch to already existed branch only. In the above step I created devops branch and I switched to it.

create a new branch and switch to that branch

Instead of creating a new branch and then doing a checkout to switch to that branch

We can use below command

git checkout –b <branchname>

So this command will do both the two things one is it will create a new branch and it will switch to that branch.

git create and switch to branch

You can see in the image that I created branch name called sony and I switched to that branch with this single command. So this command will useful only when you are creating a new branch. But to switch to any existed branch we use  normal git checkout command.

Push git branch to remote or creating remote branch

NOTE: if you you want to work in a git , you must have the same branch in locally and remotely(github or bitbucket) then only you can push to your branch and you can pull from your branch and you can work on your branch. so we have to push  the same branch to remote(github or bitbucket).

By using previous commands we created a branch in locally not in remote(github or bitbucket).

To push the branch to remote we use below command. this command will push our branch to remote(github or bitbucket).

So the same branch will be created in remote(github or bitbucket).

   git   push   --set-upstream   origin  <branch_name>

create a branch in git

git   push    – -set-upstream   origin   devops

you can see in the image , the devops branch is pushed to github or bitbucket , so now you can work in this branch.

now you can check wether remote branch created or not by using git branch -a command. it will list all local remote branches.

git list remote branches

in the image you can see remote/origin/devops that means remote devops branch is created. and you can see that sony branch is created locally and it is not pushed to remote. you can push sony branch as mentioned in above step.

  • git create branch from master
  • branch create in git
  • git create branch from another branch
  • branch in git
  • create a branch in git
  • git create remote branch
  • git create new branch from current

 

Leave a Reply

Your email address will not be published. Required fields are marked *