Today, we’ll go over the basics of branches and show you how to use git commands to play with them.
Checkout more articles on Git
A list of commonly used commands for Git Branches
- What is Branches in Git
- Get a list of local branches
- Get a list of remote branches
- Get a list of all branches
- Get the current branch name
- Create a branch
- Checkout a branch
- Create and checkout new branch
- Fetch all git branches
- Delete a branch locally
- Delete a branch remotely
- Rename the local branch
- 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.
3. Get a list of remote branches
Run the following command to fetch a list of all remote branches.
git branch -r
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
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.
7. Checkout a branch
To swap from one branch to another, use the following command.
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
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.
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..!!