Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I archive git branches?

Tags:

git

I have some old branches in my git repository that are no longer under active development. I would like to archive the branches so that they don't show up by default when running git branch -l -r. I don't want to delete them, because I want to keep the history. How can I do this?

I know that it's possible to create a ref outside of refs/heads. For example, refs/archive/old_branch. Are there any consequences of doing that?

like image 319
dan-manges Avatar asked Aug 20 '09 15:08

dan-manges


People also ask

How do I archive a git repository?

To archive a repository, go to your Repository Settings Page and click Archive this repository. Before archiving your repository, make sure you've changed its settings and consider closing all open issues and pull requests.

Should I delete stale branches?

They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.

What can you do with old branches in git?

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch. $ git branch -d release Deleted branch feature (was bd6903f).

What is git archive command?

Git archive is a helpful utility for creating distributable packages of git repositories. Git archive can target specific refs of a repository and only package the contents of that ref. Git archive has several output formats that can utilize added compression.


2 Answers

I believe the proper way to do this is to tag the branch. If you delete the branch after you have tagged it then you've effectively kept the branch around but it won't clutter your branch list.

If you need to go back to the branch just check out the tag. It will effectively restore the branch from the tag.

To archive and delete the branch:

git tag archive/<branchname> <branchname> git branch -d <branchname> 

To restore the branch some time later:

git checkout -b <branchname> archive/<branchname> 

The history of the branch will be preserved exactly as it was when you tagged it.

like image 109
Jeremy Wall Avatar answered Oct 15 '22 05:10

Jeremy Wall


Jeremy's answer is correct in principle, but IMHO the commands he specifies are not quite right.

Here's how to archive a branch to a tag without having to checkout the branch (and, therefore, without having to checkout to another branch before you can delete that branch):

> git tag archive/<branchname> <branchname> > git branch -D <branchname> 

And here's how to restore a branch:

> git checkout -b <branchname> archive/<branchname> 
like image 25
Steve Avatar answered Oct 15 '22 03:10

Steve