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:
Step 2: View All Branch
Then, list all the available branches of the current repository:
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:
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:
Step 5: Verify Changes
Run the provided command to verify whether the desired branch has been archived or not:
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:
It can be observed that the “beta” branch has been restored, and we have switched to it:
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.