How To Clean Up Git Branches

Are you quite exhausted with the unused files in your Git Repository? Usually, we collect several distinct branches for the different features when working with Git. The possible way to resolve all these issues is to clean up your untracked git branches.

In this tutorial, we have explained many ways to clean up unused branches and make your Git workspace to be more organized. As a developer, you have to follow up these kinds of stuff up-to-date and make use of Git commands properly for your projects. This tutorial addresses the below available concepts in detail:

Git Clean

git clean is the built-in command used for cleaning up the untracked files. Be careful with this one, it deletes files permanently!

Always add -n or –dry-run options to preview the damage you’ll do!

  • Run git clean -f, to just clean untracked files.
  • Run git clean -f -d, to remove directories.
  • Run git clean -f -X, just removed an ignored files.
  • Run git clean -f -x, for cleaning ignored as well as non-ignored files.

The Git rebase merge

The four rebase commands needed to synchronize all three branches are:

cleanup@git:~$ git rebase feature develop
cleanup@git:~$ git rebase develop master
cleanup@git:~$ git rebase master feature
cleanup@git:~$ git rebase feature develop

Repeated rebase commands assist to clean up commits and flatten branch histories.

Also Check: 

Clean Up Local Git Branches

First of all, you want to check which branches have already been merged with your current branch.

In this case, we are going to imply that you want to delete local branches merged with master.

To check merged branches, use the “git branch” command with the “–merged” option.

$ git checkout master

$ git branch --merged <commit>

  feature
* master

If you omit to provide the commit hash, the command will imply that you are referring to HEAD (also known as the last commit of your current branch).

Now that you have the local branches already merged with master, you will need to delete them.

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option.

$ git branch -d <branch>

The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch.

If your branch is named “feature” for example, to clean up this branch, you would run

$ git branch -d release

Deleted branch feature (was bd6903f).

Force Delete Unmerged Git Branches

The other way of cleaning up local branches on Git is to use the “git branch” command with the “-D” option.

In this case, the “-D” option stands for “–delete -force” and it is used when your local branches are not merged yet with your remote tracking branches.

$ git branch -D <branch>

As you probably already know it, you have a local branch but you also have a remote-tracking which is a branch set to represent the state of your remote branch (also called the upstream branch).

As a consequence, if you perform a commit on your local branch without pushing it to the remote branch, your remote-tracking branch will be behind your local branch, thus unmerged.

To see differences between your local branch and your remote-tracking branch, execute the “git diff” command.

$ git diff <branch>..origin/<branch>

If there are any differences between the branches, you will have to use the “-D” option to delete the branch locally.

$ git branch -d <branch>

error: The branch 'branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch'.

$ git branch -D <branch>
Deleted branch feature (was 022519a).

Now that your local branches are cleaned-up, let’s see how you can delete the remote tracking branches from your Git repository.

One-line command

In some cases, it might be useful to have a one-liner in order to delete local unused branches.

For those who are curious, here is how you can delete unused local branches in one single line.

$ git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Before executing this, let’s have a quick explanation about this command :

  • git branch –merged : first, you are simply listing all the branches currently merged with your current checked out branch;
  • egrep -v “(^*|master|dev)” : you are using the invert matching feature of grep in order to exclude any branches that may be called “master” or “dev”, just in case;
  • xargs git branch -d : you are deleting every single branch listed before.

Note: You can modify the egrep command in order to include your own branches.

Clean Up Remote Tracking Branches

As a reminder, a tracking-branch is a local branch set to track changes done on the remote branch of your Git server.

Those tracking branches are created in order to track changes but they may become obsolete if remote branches were deleted on the server.

In this case, let’s say that you have a local “feature” branch, a remote-tracking branch named “origin/feature”, but the “feature” branch has been deleted on the remote.

Git Remote Prune

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name.

$ git remote prune <remote>

In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.

$ git remote -v

origin  https://gitserver.com/user/repository.git (fetch)
origin  https://gitserver.com/user/repository.git (fetch)

In this example, the remote name is “origin”.

In order to delete remote tracking branches, we would then execute

$ git remote prune origin

Pruning origin
URL: https://gitserver.com/user/repository.git
 * [pruned] origin/feature

Prune while fetching

In some Git workflows, branches are deleted on the remote whenever they are integrated with the master branch.

Instead of prune your remotes periodically, you can also fetch the new references and prune your branches simultaneously.

In order to clean up remote-tracking branches while fetching, use the “git fetch” command with the “–prune” option.

$ git fetch --prune <remote>

Alternatively, you can simply use the “-p” shortcut instead of typing “prune” every time.

$ git fetch -p <remote>

In the case of the origin remote, this would give

$ git fetch --prune origin

From https://gitserver.com/user/repository.git
 - [deleted]         (none)     -> origin/feature

However, specifying the prune option may be a bit tiring.

Luckily for you, you can configure your Git workspace in order to execute the prune operation every time you perform a fetch or a pull operation.

To configure Git to execute prune for every fetch, execute the following command

$ git config --global fetch.prune true

Defining your Gitflow Workflow

When working with remote-tracking and local branches, it is important for you and your team to define a Git flow that you can stick to.

If you are working with somebody in order to review changes and approve them into your codebase, it might also be necessary for the reviewer to delete the branch on the remote afterwards.

This way, you will be able to prune your unused remote-tracking branches and your local branches as seen in the first section.

Defining this step is important as it can become quite messy to deal with dozens of different branches on your local Git repository.

Now that you have a clearer idea on how to delete unused remote-tracking branches, let’s see how you can perform the same trick for remote branches.

Clean Up Remote Branches

In our last chapter, we are going to see how we can delete remote branches when they are not used anymore.

Before performing any deletion operations on your Git repository, make sure that you are up-to-date with recent commits or merges done on your repository.

$ git pull

Now that you are up-to-date, you can starting deleting your old remote branches.

To clean up old remote branches, use the “git branch” command with the “-r” and “–merged” options.

As always, you need to be on the target branch to see branches already merged with this branch.

$ git checkout master

$ git branch -r --merged <commit>

origin/feature
origin/master

Note: if you don’t specify the commit, the command will simply imply that you are referring to HEAD (also known as the last commit on the branch)

Now that you know the remote branches already merged with master, you can use the “git push” command in order to delete remote branches.

$ git push <remote> --delete <branch>

In the example given previously, this command would give

$ git push origin --delete feature

To https://gitserver.com/user/repository.git
 - [deleted]         feature

One-line command

The command provided is actually very similar to the one used in order to delete local Git branches.

To delete unused remote branches, you can use the following one-liner

$ git branch -r --merged | egrep -v "(^\*|master|dev)" | xargs -n 1 git push --delete origin

Again, let’s have a quick explanation of the different parts of this command :

  • git branch -r –merged : in this case, you are listing remote branches that are currently merged with your current checked out branch. As a consequence, make sure that you are on the correct branch, remember that the “git branch –merged” command takes the HEAD when not provided with a commit SHA;
  • egrep -v “(^*|master|dev)” : in this part, you are using the invert-matching feature of grep in order to exclude the dev and the master branches;
  • xargs -n 1 git push –delete origin : in this case, you are taking every single branch listed and deleting it on the remote.

Conclusion

From the above stuff, you will learn all the ways of cleaning up unused branches on Git, whether they are local, remote tracking branches or remote branches. If you want to check more Git related articles then keep visiting our JunosNotes.com website.

Leave a Reply

Your email address will not be published.