9 useful tricks of git branch you might not know
Tags: git, beginners, tutorial, productivity
Published: 2020-08-14 07:09:22

---
title: 9 useful tricks of git branch you might not know
published: true
tags: git, beginners, tutorial, productivity
canonical_url: https://gitbetter.substack.com/p/9-useful-tricks-of-git-branch-you
---
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F63a7764f-1370-472f-b106-65e76a57eef9_343x480.gif)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.


### List all branches that are already merged into a particular branch

```
git branch --merged <respective_branch_name>

Example
git branch --merged master
```

The above command will help you to find all branches that are already merged with the **master** branch.

----
### Quickly switch to the previous branch

```
git checkout -
```

This will switch to the previous branch you checked out.

**Alternatives**

```
git checkout @{-1} // checks out to previous branch

git checkout @{-2} // checks out to second previous branch
```

---
### List all branches ordered by most recent commits

```
git branch --sort=committerdate
```

The above command will list branches in order of the most recent commits. So you can see branches that you are using it most in recent times.

Very helpful command and it comes in handy when you are working with multiple branches.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6cd625f-e41d-4ed2-ac83-033c762d0768_534x207.png)

In the above example, **develop** has the most recent commit. And **master** has the second most recent commit and so on.

---
### Delete local and remote branch

```
git branch -d <branch_name>
```

This command will delete the branch only if it is merged with the parent.

```
git branch -D <branch_name>
```

This command will delete the branch irrespective of whether it is merged or not.

To delete the branch in remote you can use

```
git push origin <branch_name> -d
```

**-d** attribute will delete the branch in remote too.

---
### Showing diff between branches

We have already seen this command in two of our previous issues.

```
git log master..develop
```

This command will help you show all the commits from **develop** but that are not present in the master branch. In this way, you can know that how many new commits are added to the develop branch that is not present in the master branch. And make sure you have the updated changes in the local before comparing.

---
### Rename a branch name

```
git branch -m <old_name> <new_name>

Example
git branch -m development develop
```

The above command will rename the branch name in local.

To push the updated branch name to remote,

```
git push origin :old_name new_name

Example
git push origin :development develop
```

---
### List remote branches

```
git branch -a
```

This command will list all the local and remote branches.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0fd962cd-e540-4d32-bb69-c3e4e131b1c0_504x266.png)

To list only the remote branches

```
git branch -r
```

These commands will be useful to see which remote branches are still present in local so that you can delete them.

I mean even after the branch is deleted in remote, there will be a reference to it in the local machine.

To delete all remote references in local, you can use

```
git remote prune origin
```

---
### List branches containing a particular commit hash

```
git branch --contains <commit-hash>

Example
git branch --contains f9456ba
```

The above command will list down all branches that contain a provided commit hash.

### Show all commits in the current branch yet to be merged to the parent

```
git cherry -v <parent_branch>

Example
git cherry -v master
```

The above command will list all commits in the current branch that are yet to be merged to the respective branch.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7599fac-b812-4d2b-962d-12ffc76a100f_807x286.png)

Thank you for reading :)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.