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.

Table of contents: Branches in Git

  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

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.

3. Get a list of remote branches

To get a list of all remote branches only, run the below commands.

OR

4. Get a list of all branches

To get a list of all remote and local branches, run the below commands.

OR

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+

For Git v1.8+

For Git v1.7+

6. Create a branch

To create a branch we have to execute the following command.

7. Checkout a branch

Run the following command to switch from one branch to another branch.

8. Create and checkout new branch

To create a new branch and checkout it by executing a single command.

9. Fetch all git branches

By running the following command you can fetch all branches from all remotes.

OR

Use the following command to clean outdated remote branches from the local machine.

10. Delete a branch locally

Following command will be used to delete a branch locally.

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.

12. Rename the local branch

Run the following command to rename the local git branch.

Rename the local branch.

13. Rename the remote branch

If you have renamed the local branch then run the following command to rename the remote branch.

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

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *