HomeTime SavingHow to Use Git Prune Command to Clean Local Branches

How to Use Git Prune Command to Clean Local Branches

GIT PRUNE Command to clean local branches

In this article, you will know how to use GIT PRUNE Command to clean local branches.

The concept of branches in git is a good feature as it allows you to work on different parts of the project at the same time. However, the more branches you have, the harder it will be to manage them.

If you create a local branch, most of the time, you could push changes to that branch at the same name branch in the remote storage. And when you are done with all the necessary changes for that branch, you will associate the branch with your production branch (asking).

Upon merging that branch, the branch becomes inactive, and GitHub, for example, offers you the option to delete a branch later. If you do this, you may forget to delete the local branch. Repeating this for many other branches leaves you with many local branches that have expired. How to use GIT PRUNE Command to clean local branches?

For learning how to use GIT PRUNE Command to clean local branches, you need to learn how both the branches work together.

Home and Remote Branches

When you create a local branch, say “header”, a new file in the project directory .git will be created as follows: refs/heads/header. This file will contain all the obligations you perform in that branch.

If you create a similar “title” branch in a remote repository, the local repository will not have information about that branch until you tell the local branch to follow it. You can track with the following command:

GIT checkout header

When you track a remote branch, you find a new file in the .git directory that has the following options: refs/remotes/header that contains the branch’s dedication and helps the local branch view the status of the remote branch.

In all, we have two new branch files.

When you delete a remote branch, nothing happens automatically to the local branch. The local branch and remote reference are not affected. That brings us to the pruning option.

Learn more about how to use GIT PRUNE Command to clean local branches.

GIT PRUNE Command to clean local branches

Prune git option

To delete location references, we need the pruning option. But note that, prune can be used in a variety of ways. Here are some of them:

git prune
git remote prune
git fetch –prune
## e.t.c

Git prune is a different order beyond the scope of this article. We are only concerned with the origin of git remote prune and git fetch –prune for this article. The two commandments have similarities and differences. Let’s look at them.

Learn more about how to use GIT PRUNE Command to clean local branches.

3 Methods for “Steam Hide Recent Activity” | What Is Steam Hide Recent Activity?

Git remote prune origin

This command removes the branch references for missing remote branches. The remote branch can be removed as a result of removal-branch-after merger-operation.

Learn more about how to use GIT PRUNE Command to clean local branches.

Git fetch –prune

This command does the same, but before the pruning, the latest remote data is first downloaded. This method is recommended over the command above, as the latest remote data may contain branches found remotely.

You can also execute this command automatically on every download request. Instead of transferring the option – prune in all git download operations you do, you can set the git to automatically prune when downloading. Here’s how:

git config –global fetch.prune fact

The above command prepares your global git configuration to make a prune in every download. If you download the git now, the outdated remote references will be automatically deleted.

Learn more about how to use GIT PRUNE Command to clean local branches.

Does pruning remove the local branches?

No, it is not. Pruning only removes references to ref/remotes / that do not identify the active branch in the remote. It works like this so you don’t have to delete your local changes. For example, if you make a lot of local branches that you haven’t pushed far so far, you won’t lose one of them.

However, if you do not want to remove the merged branches manually, here are some tips you can apply.

GIT branch – integrated

This order lists all the branches that are grouped to form the main ones. But we need to skip other branches like “dev”, “main”, “staging”, and so on. Optional – merged also lists the main branch because logically, the main branch is merged to become the main branch again. So, you also want to skip this. Use the grep command:

GIT branch – integrated | [any-other-branch-you-want-chase]”

The branch command lists the merged branches and extracts them from the grep command, which filters the result. Now, let’s remove the branches:

git branch – integrated | xargs git branch -d

The xargs command converts grep output into git branch -d arguments. that is, this will be translated as git branch -d [egrep output]. And git branch -d performs a soft removal of branches. The soft removal ensures that the branch is fully covered.

You can also set a git alias for this. In your global git config file. git-config, which should be in the root directory of your system, you can add git nicknames as follows:

And do the following:

Git delete-local-merged

git fetch will prune missing remote references (if you add that to your configuration file), and every command removes the merged local branches.

Hope you all learned to Learn more about how to use GIT PRUNE Command to clean local branches.

This is it for today’s article on “GIT PRUNE Command to clean local branches”, hope it was informative!

For more details and the latest updates, visit Instachronicles.

READ MORE