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
- How to clone Git repository
- Configure username and email in Git
- Git Clone Error: Repository not found
- How to change the commit message in git
- How to save changes temporarily using git stash
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.
git branch
3. Get a list of remote branches
To get a list of all remote branches only, run the below commands.
git branch -r
OR
git branch --remotes
4. Get a list of all branches
To get a list of all remote and local branches, run the below commands.
git branch -a
OR
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+
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 we have to execute the following command.
git branch <name_of_branch>
</name_of_branch>
7. Checkout a branch
Run the following command to switch from one branch to another branch.
git checkout <name_of_branch>
</name_of_branch>
8. Create and checkout new branch
To create a new branch and checkout it by executing a single command.
git checkout -b <name_of_branch>
</name_of_branch>
9. Fetch all git branches
By running the following command you can fetch all branches from all remotes.
git fetch -a
OR
git fetch origin
Use the following command to clean outdated remote branches from the local machine.
git fetch origin -p
10. Delete a branch locally
Following command will be used to delete a branch locally.
git branch -d <name_of_branch>
</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.
git push origin -d <name_of_branch>
</name_of_branch>
12. Rename the local branch
Run the following command to rename the local git branch.
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 have renamed the local branch then run the following command to rename the remote branch.
git push origin -u <new_branch_name>
</new_branch_name>
Now use the below command to remove the old branch remotely.
git push origin -d <old_branch_name>
</old_branch_name>
That’s it for today.
Thank you for reading. Happy Coding..!!