Git

How to Close a Branch in Git?

Developers work on various Git branches while dealing with large development projects. After completing the work in a particular branch or merging it into the main branch, they may want to delete or close it. However, deleting a Git branch also deletes the content and all the history of the branch permanently. So, it is a good option to close the branch and retrieve it later whenever required. Moreover, closing an old branch is a good practice for maintaining the project’s workflow as it helps users to ensure that the modifications are appropriately tracked and merged into the main branch.

This article will demonstrate:

How to Close a Particular Git Branch?

To close a branch in Git, look at the following steps:

  • Switch to the local directory.
  • View all available branches.
  • Select the desired branch that needs to be closed.
  • Tag the selected branch by archiving it using the “git tag archive/<branch-name> <branch-name>” command.
  • Delete/remove the branch from the repository by utilizing the “git branch -D <branch-name>” command.
  • Verify changes.

Step 1: Redirect to Local Repository

First, navigate to the desired local repository:

$ cd "C:\Git"

Step 2: View Branches

Then, list all the available repository branches:

$ git branch

The below image displays all the current repository’s branches. Now, choose the desired branch that needs to be closed. For instance, we have selected the “feature” branch:

Step 3: Tag Branch

To store the backup of the branch that needs to be closed, tag the selected branch by archiving it using the below-listed command:

$ git tag archive/feature feature

The above-provided command has created the backup for the “feature” branch:

Step 4: Delete Branch

Now, utilize the following command along with the selected branch name to delete it from the current repository:

$ git branch -D feature

Step 5: Verify Deleted Branch

To ensure that the desired branch has been closed or deleted from the repository or not, run the following command:

$ git branch

It can be observed that the “feature” branch has been closed/deleted successfully:

How to Restore a Closed Branch in Git?

Sometimes, users want to restore the closed branch. Git allows them to retrieve any closed branch with its content using the “git checkout -b <branch-name> archive/<branch-name>” command. To do so, follow the provided steps.

Step 1: Restore Branch

To restore or retrieve the closed Git branch, check out the tag and recreate the deleted branch using the given-below command:

$ git checkout -b feature archive/feature

According to the below image, the “feature” branch has been restored, and we have switched to it:

Step 2: Verify Changes

Write out the following command to ensure that the desired branch has been restored:

$ git branch

In the below output, the “feature” branch can be seen, which is also the current branch:

Step 3: View Git Log

Lastly, view the restored branch history by checking the Git log:

$ git log --oneline

As you can see, the branch has been restored successfully with its content and history:

That was all about closing and restoring a particular branch in Git.

Conclusion

To close a branch in Git, first, switch to the local directory and view all available branches. Then, choose the desired branch that needs to be closed. After that, tag that particular branch by archiving it using the “git tag archive/<branch-name> <branch-name>” command. Next, utilize the “git branch -D <branch-name>” command to delete the desired branch from the repository and verify changes. Moreover, users can retrieve the closed branch with the help of the “git checkout -b <branch-name> archive/<branch-name>” command. This article demonstrated the method to close and restore a specific branch in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.