DEV Community 👩‍💻👨‍💻

Camilo
Camilo

Posted on

Archiving git branches

This is a quick way to archive a git branch. Maybe you do not use the code anymore and the branch is cluttering the repo but for backup or historical reasons you need to keep it somewhere.

The first thing is to checkout to the branch

$ git checkout my-branch
Enter fullscreen mode Exit fullscreen mode

Then create a new tag. I use the format archive/branch-{name}

$ git tag archive/branch-my-branch
Enter fullscreen mode Exit fullscreen mode

Now we can upload the tags to Github or Gitlab

$ git push origin --tags
Enter fullscreen mode Exit fullscreen mode

And delete the branch from local and origin repos.

Be sure to be in another branch like master

$ git checkout master
Enter fullscreen mode Exit fullscreen mode

Then

$ git branch -d my-branch
$ git push origin :my-branch
Enter fullscreen mode Exit fullscreen mode

If you need to delete the tag (an all the branch backup) from local

$ git tag -d archive/branch-my-branch
Enter fullscreen mode Exit fullscreen mode

Or from origin

$ git push --delete origin archive/branch-my-branch
Enter fullscreen mode Exit fullscreen mode

This will erase all tagged branch data though. Please be careful.

Top comments (2)

Collapse
 
hugo__df profile image
Hugo Di Francesco

The title is quite confusing because if I recall, git has an 'archive' command which exports to a zip 😂.

Collapse
 
clsource profile image
Camilo Author

lol xd. well git happens, I don't know if I should update the title since the url would maintain I guess.

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.