FreeKB - Git (Version Control) git branch command
Git (Version Control) - git branch command

This assumes you are already cloned a repository from the source Git repo (typically called "origin") to a directory on your PC, such as the foo.git or bar.git repos.

 

After cloning a repository, by default, you will have a branch called "master" on your PC.

 


Display the branch you are in

The git branch command can be used to view the shorthand name of the branch you are in. 

git branch

 

Something like this will be returned.The wildcard character is used to identify the branch you are currently using (master in this example).

* master

 

The -v or --verbose flag can be used to include the ID and comment of the latest update in the branch.

git branch --verbose

 

Which will return something like this.

* master f882ea7 updated logging output

 


Display all branches

The -a or --all flag can be used to display all of the branches in the repository.

git branch --all

 

Which will return something like this.

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feat/logging
  remotes/origin/master

 


Create a new branch

Here is how you would create a new branch that contains all of the files of the branch you are currently in.

git branch foo

 

Now the --all flag should include the "foo" branch.

  foo
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feat/logging
  remotes/origin/master

 

The git checkout command can be used to switch to the newly created branch.

git checkout foo

 

Which should return something like this.

Switched to a new branch 'foo'

 

Now the --all flag should show that foo is the currently selected branch.

* foo
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feat/logging
  remotes/origin/master

 


Delete a branch

And here is how you would delete a branch.

git branch --delete foo

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 8036e in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |