How to Clean Up Git Branches?
While working on a project managed by Git, you will be creating a new branch for almost everything that you want to do and later merge them to the master branch. By the time you are done with your project, there would be a lot of branches in your project. These unwanted branches can create confusion among the developers and also make our project history look messy. Cleaning and getting rid of such branches is very important. Let's learn some techniques to clean up the branches.
Types of Branches
Our local repository can have two different types of branches - Local Branches and Remote-Tracking Branches. Local Branches are simple branches that you create so that you have an independent workspace where you can work on new features. Remote-Tracking Branches are branches that track changes made to the branches of the remote repository. We also have Local-Tracking Branches in our local repository which are used to track changes to other local branches or remote-tracking branches.
Any branches present in the remote repository are known as Remote Branches. These include the branches that are pushed by developers to the remote repository.
Cleaning Branches
We know the types of branches we are dealing with. Now let's learn how to get delete branches and clean our repository.
Cleaning Local Branches
- The motive of creating local branches is to create an independent workspace that doesn't affect our master branch. When we have completed our work on that branch and have merged it into our local master branch, then that branch is no longer needed.
- To check which branches have been merged into master we can use the Git Branch command with the --merged option. Make sure you are currently checked out on the master branch.
$ git branch --merged
We can delete these merged branches by using the Git Branch with the -d flag.
$ git branch -d <branch-name>
But merged branches are not the only local branches that we want to delete. There may be cases where we created a branch to develop a feature but later decided to drop that feature. In this case, we don't need to merge this branch into the master branch. By default, Git will block us from deleting these unmerged branches.
To view these unmerged branches we can run the Git Branch command with the --no-merged option. Make sure you are currently on the master branch while running this command because it will return the names of the branches which are not merged on your current working branch(master in this case).
$ git branch --no-merged
To delete unmerged branches we can use the -D flag instead of the -d flag.
$ git branch -D <branch-name>
Cleaning Remote-Tracking Branches
- Whenever we clone or fetch from a repository then Remote-Tracking Branches are set up to track the changes to the remote branches. But these remote-tracking branches are left in our local repository even if the remote branches that they are tracking no longer exist. This makes the remote-tracking branches useless as the branches they were meant to track have been deleted.
- To delete these remote-tracking branches we use the Git Remote Prune command. This will remove all the remote-tracking branches for which the remote branches do not exist.
$ git remote prune <remote-name>
We can also use the Git Fetch command to remove the remote-tracking branches that do not have a remote branch to track. Git Fetch will fetch the new changes and at the same time prune or remove the stale remote-tracking branches.
$ git fetch --prune <remote-name>
Cleaning Remote Branches
We know that the branches that are already merged with our master branch are no longer of any use. This also applies to remote branches. To check which remote branches have been merged we can use the Git Branch command. Make sure that you are currently checked out on the master branch.
$ git branch -r --merged
Make sure that your local repository is up to date with the remote repository. If not then first run the Git Pull Command.
Now that we know the names of the branches which have been merged and are no longer needed, we can delete them by using the Git Push command with the --delete option.
$ git push <remote-name> --delete <branch-name>
Summary
Branching is a very important part of any Git workflow. Every time something new has to be added to a project, it will first be created on a separate branch and then merged with the rest of the project. But keeping unwanted branches in our repository can make the project history look messy and it will be difficult to understand. We can clean our local repository by deleting unwanted local branches that have already been merged into master. This is done by using the Git Branch command. We also need to delete the stale remote-tracking branches that are tracking an already removed remote branch. Git Remote or Git Fetch command can be used for this purpose. Remote branches can be deleted with the help of the Git Push command.