Using branches in Git
Today we’ll give you an understanding of the branches and show you a list of git commands to play with branches.
Using branches in Git, git create branch from master, git branch checkout, git merge branch to another branch, git switch to another branch, git push branch, git create branch from another branch, git checkout remote branch, Create a new branch with git and manage branches, git branch – Creating, deleting and showing branches, Create and delete a branch in your Git repository.
Checkout more articles on Git
Table of contents: Branches in Git
- 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
Branch is an independent line of development. It’s nothing but a lightweight movable pointer to a specific commit.
Following command will be used to create/fetch/delete local and remote branches.
2. Get a list of local branches
Run the following command to get a list of local branches only.
1 | git branch |
3. Get a list of remote branches
To get a list of all remote branches only, run the below commands.
1 | git branch -r |
OR
1 | git branch --remotes |
4. Get a list of all branches
To get a list of all remote and local branches, run the below commands.
1 | git branch -a |
OR
1 | git branch --all |
5. Get the current branch name
The list of commands will be used to get the current branch name based on the git version.
For Git v2.22+
1 | git branch --show-current |
For Git v1.8+
1 | git symbolic-ref --short HEAD |
For Git v1.7+
1 | git rev-parse --abbrev-ref HEAD |
6. Create a branch
To create a branch we have to execute the following command.
1 | git branch <name_of_branch> |
7. Checkout a branch
Run the following command to switch from one branch to another branch.
1 | git checkout <name_of_branch> |
8. Create and checkout new branch
To create a new branch and checkout it by executing a single command.
1 | git checkout -b <name_of_branch> |
9. Fetch all git branches
By running the following command you can fetch all branches from all remotes.
1 | git fetch -a |
OR
1 | git fetch origin |
Use the following command to clean outdated remote branches from the local machine.
1 | git fetch origin -p |
10. Delete a branch locally
Following command will be used to delete a branch locally.
1 | git branch -d <name_of_branch> |
11. Delete a branch remotely
If you get a list of all branches after removing the local branch then it still exists on the remote repository.
So use the below command to delete a branch remotely.
1 | git push origin -d <name_of_branch> |
12. Rename the local branch
Run the following command to rename the local git branch.
1 | git checkout <old_branch_name> |
Rename the local branch.
1 | git branch -m <new_branch_name> |
13. Rename the remote branch
If you have renamed the local branch then run the following command to rename the remote branch.
1 | git push origin -u <new_branch_name> |
Now use the below command to remove the old branch remotely.
1 | git push origin -d <old_branch_name> |
That’s it for today.
Thank you for reading. Happy Coding..!!