Git

How Can I Archive Git Branches?

In Git, archiving a branch means creating a copy of the particular branch’s contents and keeping it outside the Git repository. This is done to store the branch that is no longer needed for the current project but may be used in the future. Moreover, archiving branches is helpful to keep the repository organized, store the project code and save storage space.

This write-up will explain:

How to Archive Git Branches?

To archive a particular Git branch, follow the below-mentioned steps:

  • Redirect to a local directory.
  • List all branches and select the specific branch.
  • Archive the desired branch using the “git tag archive/<branch-name> <branch-name>” command.
  • Delete/remove the archived branch from the repository through the “git branch -D <branch-name>” command.
  • Ensure changes.

Step 1: Navigate to Local Directory

First, switch to the particular local repository by entering the following command:

cd "C:\Git"

 

Step 2: View All Branch

Then, list all the available branches of the current repository:

git branch

 

The below screenshot displays the current repository’s branches. Now, choose the desired branch that needs to be archived. For instance, we have selected the “beta” branch:

Step 3: Archive Branch

Now, execute the following command to archive the selected branch:

git tag archive/beta beta

 

The above-stated command will create a backup of the “beta” branch by creating a new tag:

Step 4: Delete Branch

Next, delete the archived branch to remove it from the repository:

git branch -D beta

 

Step 5: Verify Changes

Run the provided command to verify whether the desired branch has been archived or not:

git branch

 

In the below output, the “beta” branch cannot be seen which means it has been archived successfully:

How to Restore Archived Git Branches?

To restore the archived Git branch, check out the tag and recreate the deleted branch with the help of provided command:

git checkout -b beta archive/beta

 

It can be observed that the “beta” branch has been restored, and we have switched to it:

git branch

 

In the below image, the restored “beta” branch can be seen which is also the current branch:

We have explained the easiest way to archive and restore the Git branches.

Conclusion

To archive a particular Git branch, first, choose the desired branch and run the “git tag archive/<branch-name> <branch-name>” command to archive it. Then, execute the “git branch -D <branch-name>” command to delete the archived branch from the repository. Moreover, you can restore the archived branch using the “git checkout -b <branch-name> archive/<branch-name>” command. This write-up described the method to archive and restore the Git branches.

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.