Automate Local Git Cleanup
Automate Local Git Cleanup

How to Clean Up Your Local Git Branches with PowerShell ?

Now a days, almost every project (be it a pet project or a professional enterprise) uses Git version control. If you are a Git user, you may be following a general workflow:

  • We generally create a new branch when we start the work. Let’s call it a “feature” branch.
  • Then we push the changes to remote and raise the pull request to merge the branch into main (or master branch).
  • Once the PR is merged, we start the work on new work items assigned to us.
  • OR PR is discarded and we start work on the new work item.

Generally, while merging the PR, there is option to delete the branch. Some people strictly ensure that the checkbox is selected to delete the “feature” branch after merge. But this will only delete the remote branch. It will not delete the branch from the local git repository. That means each time we merge a pull request (or merge request), we need to manually delete the “feature” branch from local as well.

I mostly do not delete local branches immediately. Many other people may also be following the same. And when you keep working like this for few weeks, you may have a bit list of branches in your local Git repository. Then on one fine day, most of us manually delete the local branches.

So, either you have too many branches on local, either due to the remote branch was merged and deleted or the remote branch was just deleted due to some reason. Either way, having too many local branches can make it hard to find the ones you actually need, and can also take up unnecessary disk space.

Fortunately, there is a way to clean up your local git branches with PowerShell. PowerShell can help you automate tasks and interact with various applications, including Git. In this article, we have a look at how to use PowerShell to delete your local git branches that have been merged and no longer exist on the remote.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • A Git repository that you want to clean up
  • A PowerShell terminal (you can use the built-in PowerShell console, or install a third-party terminal like Windows Terminal or Cmder)
  • A basic understanding of Git and PowerShell commands

Step 1: Check out the main branch

The first step is to check out the main branch of your repository. This is the branch that you want to keep as the base for your future work.

To check out the main branch, run the following command in your PowerShell terminal:

git checkout main

For the sake of this discussion, I am going to assume that the name of the main parent branch is “main”. If you have a different name for your main branch, such as master or develop, use that name instead.

Step 2: Fetch the latest changes from the remote

The next step is to fetch the latest changes from the remote repository. And how to do that ? We can either pull or fetch. This will ensure that your local repository is up to date with the remote, and that you have the latest information about which branches have been merged or deleted. To fetch the latest changes from the remote, run the following command in your PowerShell terminal:

git fetch --prune

What does ‘prune’ option does ?

As we know, we can update the git metadata in the local repository by pulling or fetching. But if we just pull or fetch, it will get the new data about the remote branches but if there are any branches which are deleted from remote but are present on local, those references will not be cleared by just pulling or fetching.

That’s why we need to use prune option – to clear references to the branches which are present on local but are deleted in remote repository. This will ensure that you have metadata only for those repositories, which are still active in Git remote.

Understanding git fetch –prune

We can explicitly specify this option, or we can also update git settings file and set this option to enabled by using the command given below.

git config --global fetch.prune true

Step 3: List and delete the merged local branches

We know prune solves half of our problem – by deleting obsolete branch references from local. Now, the final step – to list and delete the local branches that have been merged into the main branch.

For this, we will use a combination of Git and PowerShell commands. The basic idea is to use Git to list all the merged branches, then use PowerShell to filter out any branches that we want to keep (such as main), and finally use Git again to delete the remaining branches.

Here is the command that we will use:

git branch --merged `
| Where-Object { $_ -notmatch "^(\\*|main)" } `
| ForEach-Object { git branch -d $_.Trim() }

Let’s break down this command and see what each part does:

  • git branch --merged lists all the local branches that have been merged into the current branch (which is main in our case).
  • | (you probably know what it is), is a pipe symbol that passes the output of one command as input to another command.
  • Where-Object { $_ -notmatch "^(\\*|main)" } filters out any branches that match the regular expression ^(\\*|main). This expression matches any branch that starts with an asterisk (which indicates the current branch) or main (which is our main branch). You can add more branches to exclude by using the | symbol inside the expression, such as ^(\\*|main|develop).
  • ForEach-Object { git branch -d $_.Trim() } executes a block of code for each object in the input. In this case, the input is each branch name from the previous command. The code inside the block uses git branch -d to delete each branch, and uses $_ to refer to the current object. The .Trim() method removes any leading or trailing whitespace from the branch name.

After running this command, you should see a message for each deleted branch, such as:

Deleted branch feature/feature1(was 1234567).
Deleted branch bugfixes/bug1 (was 7654321).
Deleted branch releases/1.1.0 (was 7654321).

You can verify that your local branches have been cleaned up by running git branch again.

The Complete Script

The complete script can be found in the code snippet given below. You can save this script as file and maybe schedule it to locally run this script at certain time in the week. If you don’t trust automatic execution (like me), then you will at least have to run this script yourself as and when needed.

Conclusion

In this article, we showed you how to use PowerShell to delete your local git branches that have been merged and no longer exist on the remote. This can help you keep your workspace tidy and organized, and save some disk space as well.

I hope you found this article helpful. Let me know your thoughts.

Leave a ReplyCancel reply