5

I have a large number of branches which is getting a bit confusing to work with. Sometimes I dont want to delete the branch completely but I wont be working on it for a while. Ive adopted a convention of prefixing branches with _ when that is the case.

So if i start out with:

branch1
branch2
branch3

When Im done with branch1 but want to save a backup of it just incase then ill rename to:

_branch1
branch2
branch3

This worked well for a while but my workspace is getting a bit cluttered now. Is there anyway of saving a branch somewhere so it can be recovered, but so that its not in the normal workspace (as if its been deleted)?

Evanss
  • 23,390
  • 94
  • 282
  • 505
  • 1
    Relevant: http://stackoverflow.com/questions/25169440/remove-hide-git-branches-without-deleting-commit-histories – Hasturkun Feb 16 '16 at 16:16
  • What do you mean by _"my workspace is getting a big cluttered now"_? Do you just mean that `git branch` shows too much stuff you don't want to see all the time, such as your backup branches which begin with `_`? – Gabriel Staples Mar 11 '21 at 00:16
  • If that's what you mean, here's my answer: https://stackoverflow.com/a/66574807/4561887. – Gabriel Staples Mar 11 '21 at 04:18

4 Answers4

2

You can use tags for many of the same sorts of things as branches:

git tag saved_branch1 branch1
git branch -D branch1

And, to recover the branch:

git branch branch1 saved_branch1
git checkout branch1

The catch is that tags can accidentally get pushed upstream, so you have to be careful to avoid that:

git push --tags         ;# Don't do this!
git push --follow-tags  ;# This will only push annotated tags on some commits

Since the tag command I showed above does not create annotated tags (it creates lightweight tags) those will not be pushed.

ams
  • 24,923
  • 4
  • 54
  • 75
1

Preserve your old branches in a separate repository.

That is, if you're working in $HOME/myproject, clone your repository:

cd $HOME
git clone myproject myproject_archive

Now, set up the _archive repository as a remote on your primary repository:

cd myproject
git remote add archive ../myproject_archive

Now delete all the "old" branches in myproject:

git branch -D _branch1
etc...

And you're all set! Your branches continue to exist in the myproject_archive repository, and you can now trivially archive new branches:

git branch -m branch2 _branch2
git push _branch2 archive
git branch -D branch2
larsks
  • 277,717
  • 41
  • 399
  • 399
0

Your default refspec is:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*

Make sure you have push all your branches, and then clone your repo again, but this time only the master branch

git clone -b master --single-branch git://sub.domain.com/repo.git

Then add the few branches you want to work on:

git config --add remote.origin.fetch refs/heads/my_name/branch1:refs/remotes/origin/branch1
git config --add remote.origin.fetch refs/heads/my_name/branch2:refs/remotes/origin/branch2
git fetch

Then start working on one of the few branches you have fetched:

git checkout -b branch1 origin/branch1
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Quote from the question (emphasis added):

This worked well for a while but my workspace is getting a bit cluttered now. Is there anyway of saving a branch somewhere so it can be recovered, but so that its not in the normal workspace (as if its been deleted)?

What do you mean by "my workspace is getting a big cluttered now"? Do you just mean that git branch shows too much stuff you don't want to see all the time, such as your backup branches which begin with _?

If that's the case, I don't like the other solutions. The cleanest, simplest, and least storage-intensive solution by far I think is to just write your own version of git branch, called git branch_ which wraps around the regular git branch but doesn't show your backup branches beginning with _.

Here's how to do that:

  1. Create git-branch_ in ~/bin:
    mkdir -p ~/bin
    gedit ~/bin/git-branch_
    
  2. Now put this into it
    #!/bin/bash
    
    # Ignore (don't print with `git branch_`) branch names which begin with 
    # these prefix strings
    PREFIX1="z-"
    PREFIX2="_"
    
    git branch --color=always "$@" \
    | grep --color=never -v "^  $PREFIX1" \
    | grep --color=never -v "^  $PREFIX2"
    
  3. Now close and re-open your terminal, and/or log out and log back in if this is the first time creating and using the ~/bin directory (so that Ubuntu can auto-add the ~/bin dir to your path via your ~/.profile file). Then run the following:
    # 1. show ALL branches
    git branch
    # 2. show all branches EXCEPT those whose names begin with `z-` or `_`. 
    git branch_
    

That's it! Note that I like to prefix my backup branch names with z-bak/some_name, hence why I've also excluded branch names which begin with z-.

Since git branch_ is a wrapper around git branch, and passes all arguments to it with "$@", you can use ANY and all parameters/options to git branch_ as you can to the regular git branch.

Example: these commands are the same:

git branch -h
git branch_ -h

Get this git branch_ script in my repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-branch_.sh. See the comments inside of it as well.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265