'How do I list branches having a common prefix?
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?
Solution 1:[1]
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"
Solution 2:[2]
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"
Solution 3:[3]
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/*
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | β.εηοιτ.βε |
Solution 3 | Daniel Liuzzi |