Skip to content

Instantly share code, notes, and snippets.

@zkiraly
Last active September 27, 2024 13:45
Show Gist options
  • Save zkiraly/c378a1a43d8be9c9a8f9 to your computer and use it in GitHub Desktop.
Save zkiraly/c378a1a43d8be9c9a8f9 to your computer and use it in GitHub Desktop.
Procedure to archive git branches.

How to prune unwanted git branches

Discover unwanted branchs

From http://www.darkcoding.net/software/cleaning-up-old-git-branches/

  1. Switch to the main branch, usually 'develop':

     git checkout develop
    
  2. Get a list of fully merged branches:

     git branch -a --merged
    

Prune unwanted branches

From http://www.aaronwest.net/blog/index.cfm/2011/6/7/Git-Workflows-Archiving-Old-Branches

  1. Tag the unwanted branch:

     git tag archive/sprintjuly2010 sprintjuly2010
    
  2. Delete the branch:

     git branch -d sprintjuly2010
    
  3. Push the branch deletion to origin:

     git push origin :sprintjuly2010
    
  4. Push the new tag to origin:

     git push --tags
    
  5. Restore a deleted branch from a tag:

     git checkout -b sprintjuly2010 archive/sprintjuly2010
    
@calebickler
Copy link

Thanks!

@mwikya
Copy link

mwikya commented Jun 7, 2019

Thanks

@kattatzu
Copy link

kattatzu commented Jan 8, 2020

Hi, to make this process faster I created this function in my .zshrc file (or .bashrc).

function archive_branch() {
  date=$(date '+%Y-%m-%d')

  git checkout master
  git tag archive/$date-$1 $1
  git branch -D $1
  git branch -d -r origin/$1
  git push --tags
  git push origin :$1
}

In console runs like this:

archive_branch sprintjuly2010

This will remove the branch you want to archive and create a tag with its content in the following format:

archive/2020-01-08-sprintjuly2010

regards

@augustogiles
Copy link

Hi,
I made a script to prune all unwanted branches. You can check at:
https://gist.github.com/augustogiles/528176e0eac6b066195ff25e10f52849

@coder-candace
Copy link

This is exactly what I needed thank you!

@GhaniyKie
Copy link

Mantul, Bro!

@hustc12
Copy link

hustc12 commented Sep 11, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment