• Post category:Git
  • Reading time:6 mins read

Today, we’ll go over the basics of branches and show you how to use git commands to play with them.

A list of commonly used commands for Git Branches

  1. What is Branches in Git
  2. Get a list of local branches
  3. Get a list of remote branches
  4. Get a list of all branches
  5. Get the current branch name
  6. Create a branch
  7. Checkout a branch
  8. Create and checkout new branch
  9. Fetch all git branches
  10. Delete a branch locally
  11. Delete a branch remotely
  12. Rename the local branch
  13. Rename the remote branch

1. What is Branches in Git

A branch is a development path that is separate from the main path. It’s just a small, moveable pointer to a particular commit.

To create, retrieve, and remove local and remote branches, use the following command.

2. Get a list of local branches

To retrieve a list of just local branches, use the following command.

git branch

3. Get a list of remote branches

Run the following command to fetch a list of all remote branches.

git branch -r

OR

git branch --remotes

4. Get a list of all branches

Run the following commands to obtain a list of all remote and local branches.

git branch -a

OR

git branch --all

5. Get the current branch name

Based on the git version, the list of commands will be used to retrieve the current branch name.

For Git v2.22+

git branch --show-current

For Git v1.8+

git symbolic-ref --short HEAD

For Git v1.7+

git rev-parse --abbrev-ref HEAD

6. Create a branch

To create a branch, use the following command.

git branch 

7. Checkout a branch

To swap from one branch to another, use the following command.

git checkout 

8. Create and checkout new branch

By using a single command, you may create a new branch and checkout it.

git checkout -b 

9. Fetch all git branches

You will get all branches from all remotes with the following command.

git fetch -a

OR

git fetch origin

To clear off old remote branches from your local system, use the following command.

git fetch origin -p

10. Delete a branch locally

To remove a branch locally, use the following command.

git branch -d 

11. Delete a branch remotely

If you see a list of all branches after deleting from a local environment, that means the remote repository still has it. To remove a branch remotely, use the command below.

git push origin -d 

12. Rename the local branch

To rename the local git branch, use the command below.

git checkout 

Rename the local branch.

git branch -m 

13. Rename the remote branch

If you’ve already renamed the local branch, use the command below to rename the remote branch.

git push origin -u 

Now use the below command to delete the old branch remotely.

git push origin -d 

That’s all I’ve got for today.
Thank you for reading. Happy Coding..!!

Leave a Reply