Jun
7

Git Workflows: Archiving Old Branches

Posted by Aaron West at 9:57 PM in ColdFusion, Git

I decided to take some time today to clean up old sprint branches that have been resting in Git unused. Some for well over a year. I had two goals I wanted to accomplish when trying to come up with a workflow. First, I wanted the list of branches that display with the command git branch -a (which I have conveniently aliased to just gb) to be shortened. Second, I didn't want to lose any of the commits part of old branches. Sure the commits were already merged into the master branch, but I didn't want to lose commit chain that was stored as branches.

My solution was to archive each branch as a tag then remove the branches from Git on my local machine as well as the Git origin server.

Creating a tag from a branch

Git tags aren't too different from branches. They are a point-in-time backup of your repository at a specific commit level or in the case of this workflow a specific branch. Since I was archiving a dozen or so branches I decided to create a naming convention to make finding the tags easier. I chose the name archive/[branchname]. This is the name of the tags that will display when the command git tag is issued. To create a tag that's associated with a branch I used the following Git command from my Mac's Terminal.

git tag archive/sprintjuly2010 sprintjuly2010

Deleting a branch from the local Git working copy

Deleting a branch from Git is really simple but there are two prerequisites. You must have already merged the branches commits to another branch such as master, and if the local branch is configured to track against a remote Git server you must have pushed all local commits to the origin. With these two steps out of the way you can delete the local Git branch with git branch -d [branchname].

git branch -d sprintjuly2010

Deleting a branch from the remote Git origin

After deleting a branch from the local Git working copy the branch will no longer display when you issue the git branch -a command. But, the remote origin version of the branch - if you are using a remote Git server and branch tracking - will still display in the format remotes/origin/[branchname]. To get rid of these elements of the display you need to delete the branch from the origin server using the somewhat funky command git push origin :[branchname]. If you're worried about doing this, don't be. You will have merged the branches commits to another branch such as master, and you have the branch backed up as a tag too. You can restore the branch commits from the tag which I'll discuss in a bit.

git push origin :sprintjuly2010

Pushing your new Git tags to the origin server

If you use an origin Git server (I highly recommend this) you'll want to push your local Git tags to the origin server. This is useful if you need to clone your repository again, or if you have other developers who also use Git. You push your new Git tags using the command git push --tags.

git push --tags

Displaying a list of local Git tags

To display a list of all the tags you have in your local Git repository you use the git tag command. If you want additional information about a tag you can use the git show command passing it the name of an existing tag.

# Display all tags
git tag

# Display details about a specific tag
git show archive/sprintjuly2010

Restoring a tagged/archived branch

If you ever need to restore an archived branch you have tagged and deleted you can do so with the Git checkout command. The -b flag tells Git to create a new branch and check it out in the same step. The first branch name is the name of the new branch, and the second is the name of the tag to use as the source for the new branch.

git checkout -b sprintjuly2010 archive/sprintjuly2010

Conclusions

I believe this Git workflow for archiving and restoring old, unused branches is a great way to clean up your repository without losing valuable work. If you have a better way to do things, or ways to improve this workflow, please post them in the comments.

Aaron West's Gravatar
About this post:

This entry was posted by Aaron West on June 7, 2011 at 9:57 PM. It was filed in the following categories: ColdFusion, Git. It has been viewed 76282 times and has 4 comments.

2 related blog entries

4 Responses to Git Workflows: Archiving Old Branches

  1. Nice approach Aaron! We have about 40 or so branches from our current build that have been merged and are going to hit production today. This came just in time since I will be archiving all those branches tomorrow to help clean up the repo some.

    Thanks for the post.

  2. wwwald

    Following this approach, what happens to the affected branches in other contributors' local repositories?

    Are they deleted in their repositories too, as soon as they pull or fetch?
    Or do they remain present, with the risk that they re-appear as soon as someone pushes the old branch to origin again?

  3. @wwwald - the answer to your question about the impact to the origin server was covered in the post itself.

    "My solution was to archive each branch as a tag then remove the branches from Git on my local machine as well as the Git origin server."

    Note: This post discusses tagging the state of the branch using "git tag" before archiving/deleting the branch. So developers can also restore the state of the branch by resetting/reverting to the SHA of the tag.

  4. wwwald

    Hi Aaron,

    I'm not wondering about the origin server, but rather the local repositories of other contributors, that also connect to the origin server.
    If the branches are archived on the origin server, they still may have the "unarchived" branches lingering around in their local repositories, right?
    Isn't there a risk of "archived" branches re-appearing as normal branches, as soon as one of the other contributors pushes it to the origin server again?