Code Premix

A list of commonly used commands for Git Branches

📅April 14, 2022|🗁Git

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

  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 <name_of_branch></name_of_branch>

7. Checkout a branch

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

git checkout <name_of_branch></name_of_branch>

8. Create and checkout new branch

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

git checkout -b <name_of_branch></name_of_branch>

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 <name_of_branch></name_of_branch>

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 <name_of_branch></name_of_branch>

12. Rename the local branch

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

git checkout <old_branch_name></old_branch_name>

Rename the local branch.

git branch -m <new_branch_name></new_branch_name>

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 <new_branch_name></new_branch_name>

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

git push origin -d <old_branch_name></old_branch_name>

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