9 useful tricks of git branch

With these tricks, you can be much more productive in git branching

Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.

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.

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.

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.

That’s it for today :)

Hope you learned something. You can show your love by sharing it with others :)

See you next week :) :)