3

I am using Git to manage the development and releases of some software used internally by my company, and having not used Git much prior to joining this company, am still learning how to use it/ best practice when using it for version management, feature development, etc.

I have reached the point where my local Git repository is now quite cluttered with several branches- I have been creating a new branch for the development of each feature/ fixing of each bug raised, merging that branch with master once the feature is complete/ bug is fixed, and pushing master to the server. The server only has the one branch, master running on it.

I want to 'clean up' my local repository, so that old development branches that I am no longer using no longer show up when I run git branch, or other such commands. But still want to have access to the old branches in case I need to revert/ checkout any old working versions/ compare them to the current version if something breaks, etc

I came across this question on SO: How can I archive git branches?, which, as I understand, indicates that I can 'clean up' my local repository by archiving and deleting the old branches. But I'm a bit hesitant to do this before clearing up a few things that I'm unsure about:

If, when I run git branch, I have the following branches on my repository:

master
testFeature
brokenTestFeature
FixedTestFeature

and I want to 'archive' the testFeature & brokenTestFeature branches, as I understand, I would run:

git tag archive/testFeature testFeature
git tag archive/brokenTestFeature brokenTestFeature
git branch -d testFeature
git branch -d brokenTestFeature

After doing this, if I then run git branch again, the output should display:

master
FixedTestFeature

and if I wanted to checkout testFeature again, I could run:

git checkout -b testFeature archive/testFeature

and the testFeature branch would then be displayed again the next time I ran git branch, with all of its commit history, etc still intact?

If I archived a number of branches, how would I display all of the branches that I have archived, so that I know what branches I have in the archive to checkout if/ when I decide that I need to?

Community
  • 1
  • 1
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

3 Answers3

4

git branch --list accepts an optional <pattern>. For example:

git branch --list 'archive/*'

Note that --list isn't usually needed, but it's required when using the optional <pattern>; otherwise it'll assume you want to create a branch named archive/*.

Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57
1

To preview the branches with a common prefix or regexp

git branch | grep "<pattern>"

Replace the <pattern> with a regular expression to match your branch names.

To delete all branches starting with a prefix Jira, just type

git branch | grep "Jira"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
0

Just think of Git tags and branches as post-it notes, because that is waht they essentially are. They are just post-it notes that stick on commits. The only difference is, that if you checked out a specific branch (not only the commit the post-it points to, but really the branch) and create a new commit, the branch post-it is automatically moved to the new commit.

So what you do in this archiving process is just creating a tag post-it and peeling off the branch post-it, so that the branch is not listed in the list of branches anymore. The commits are not changed in any way and you still have the full history available as the tag is still pointing to the commit. To just check out the archived branch, you can also simple check out that tag with git checkout archive/testFeature. Only if you want to unarchive the branch, you have to do the git checkout -b testFeature archive/testFeature and maybe you should delete the tag after unarchiving with git tag -d archive/testFeature.

You can also create Git aliases that do the archiving and unarchiving with one command instead of having to remember multiple commands, so you could then do git archiveBranch testFeature, git unarchiveBranch testFeature and git listArchivedBranches for example.

To list the archived branches, just list the tags with the archive prefix by doing git tag -l archive/*.

Here you have an example of how to define aliases for your actions:

git config alias.archiveBranch '!archiveBranch() { [ $# -ne 1 ] && echo Exactly one argument is expected, which is the branch to archive && exit 1; git tag archive/$1 && git branch -qD $1 && echo archived branch $1; }; archiveBranch'
git config alias.unarchiveBranch '!unarchiveBranch() { [ $# -ne 1 ] && echo Exactly one argument is expected, which is the branch to unarchive && exit 1; git branch $1 archive/$1 && git tag -d archive/$1 && echo unarchived branch $1; }; unarchiveBranch'
git config alias.listArchivedBranches '!listArchivedBranches() { [ $# -gt 1 ] && echo None or one argument expected, which is the pattern to list && exit 1; git tag -l archive/${1-*}; }; listArchivedBranches'

With these you can do stuff like

git archiveBranch foo
git unarchiveBranch foo
git listArchivedBranches
git listArchivedBranches foo # to list an archived branch called "foo"
git listArchivedBranches 'f*' # to list all archived branches starting with "f"
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • Thanks for your answer. So if I run `git tag archive/testFeature testFeature`, this would move the `testFeature` branch to a 'folder' called `archive`, and `testFeature` would no longer be displayed in the list of branches when I run `git branch` from my repository? I wouldn't need to delete the branch at all? Does this command create the `archive` 'folder', and place the branch there, or does `archive` already exist? – Noble-Surfer Jan 18 '17 at 10:53
  • No, why should `tag` remove the branch? The `archive/` prefix is arbitrarily chosen by you and has absolutely no special meaning. It just creates a tag named `archive/testFeature` that points to the same commit the `testFeature` branch is pointing to. There is not really a folder structure, the tags name acutally is `archive/testFeature`. Internally it is stored in a folder, yes, but that is just an internal detail. Some tools could use the slash as separator and display a tree with folders, but that would only be a viewing sugar. – Vampire Jan 18 '17 at 11:08
  • What I'm looking to do, is declutter the output displayed when I run `git branch`- I have quite a lot of branches there, which were created for different features/ bug fixes, and I want to clear up this list, but don't want to remove the branches that were created for each feature/ bug fix completely, as they usually just have changes to one part of the software- but it is possible that another part of the software has a dependency on the part that I've changed, but the impact that the change has on that other part won't always be apparent for some time... so I want to be able to `checkout` – Noble-Surfer Jan 18 '17 at 11:18
  • other older branches, to compare how a particular change I've made has impacted this other part of the software... I was not the original developer, but have taken over the development of this project since the original developer left the company, and there is not much documentation regarding the structure of the code, etc. So I'm having to learn the structure, and what dependencies various parts of the code have on other parts as I'm working on it. – Noble-Surfer Jan 18 '17 at 11:21
  • I'm sorry if you misunderstood my first sentence in my last comment. This was a rhetorical question, not the question for your intentions. I absolutely understand your request. That first sentence just meant "No, it does not". – Vampire Jan 18 '17 at 11:56
  • I updated my answer with example alias definitions which combine your commands into one each. – Vampire Jan 18 '17 at 12:51
  • Apologies- I meant that after I had run `git tag archive/testFeature testFeature`, would the `testFeature` branch still be displayed when I run `git branch` from my root directory, or would I need to manually remove it, knowing that it now also exists in `archive/testFeature`? – Noble-Surfer Jan 18 '17 at 12:59
  • As I said, of course it will still be displayed. You just create a tag, so an additional post-it note on the commit. Why should it not be listed in the list of branches. Again, `archive/` is just an arbitrary prefix you chose, you can also call your tag `foo/testFeature`. – Vampire Jan 18 '17 at 13:03
  • Ah, so it's not quite what I want then... what I want to do, is move some old branches that I'm not currently working on to a separate 'folder' (don't know if Git has folders, or what the equivalent is...) so that they don't show up in the output when I run `git branch`, but that I can still access them if I ever want to check one of them out, to see how an older version differs to the given branch that I'm currently working on. I'm aware that I can use the `commit` history to look up previous `commits`, but was just wondering if there's a way to 'store' branches elsewhere? – Noble-Surfer Jan 18 '17 at 13:20
  • What is wrong with your commands or with my aliases I built for you out of them? Don't they exactly do what you want? – Vampire Jan 18 '17 at 13:42
  • Well you said, "of course it will still be displayed. You just create a tag, so an additional post-it note on the commit. Why should it not be listed in the list of branches."... But I don't want the branches that I move to the `Archive` to be displayed- that's what I'm looking for, is a way to reduce the number of branches that are displayed by running the `git branch` command, without actually removing/ deleting them. i.e I don't want to run `git branch -d brokenTestFeature`, but want to 'store' this branch somewhere so that it won't be displayed in the list of branches that are displayed – Noble-Surfer Jan 18 '17 at 14:37
  • when I run `git branch` – Noble-Surfer Jan 18 '17 at 14:37
  • Is this possible, or not? Would I have to delete a branch to stop it being displayed when I run `git branch`? – Noble-Surfer Jan 18 '17 at 14:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133466/discussion-between-vampire-and-someone2088). – Vampire Jan 18 '17 at 15:12
  • I haven't had a chance to look into it any further yet, as something else a bit more urgent has come up. Once I get the time, I'll give it a go and let you know. Thanks again for your help. – Noble-Surfer Jan 19 '17 at 15:39