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
Conclusion
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.