Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove/hide git branches without deleting commit histories

Situation:

I have a main repo with a main dev branch and lots of "experiment" branches sprouting off from it (e.g., exp1 and exp2). The purpose of these experiment branches is to serve as placeholders for experiments that generate numerical results. I record the branch name (and commit ID) of the experiment branches so I can return to the commits to see precisely the code and history behind the results.

But, now, there are so many experiment branches that it's getting hard to see the main tree. So, I'm rethinking my strategy for keeping placeholders to the code behind each set of results (i.e., each experiment). Obviously I could just save the working dir at each branch, but it would be nice to keep the commit history also.

Possible solution:

One way to deal with this is to move the experiment branches into their own independent repos, each being rooted at the child node of the appropriate node in the commit history of the dev branch. Here's an illustration of what I mean:

enter image description here

Click here for larger version of image (on imgur.com).

So, for example, for branch exp1, I would like to export commits A->B->C to a separate repo rooted at commit A. Then, I can just record the hash of commit P1 so that I know where the exp1 branch descended from.

Question:

How can I do that?

Better question:

On the other hand, I strongly suspect there is a much better strategy for doing what I want to do---namely, unclutter the tree for visual inspection but keep placeholders to prior branches so I can return to them if needed. So, can anyone recommend a strategy for this?

like image 651
synaptik Avatar asked Aug 06 '14 20:08

synaptik


People also ask

Does deleting a branch delete commit history?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.

How remove branch from previous commit?

In order to create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit to create your branch from. Alternatively, you can use the “git branch” command with the branch name and the commit SHA for the new branch.

Can you archive branches git?

Well, there is no such option like archiving branches in git. But we can create a tag with a prefix like “archive”. In other words, we move entry about the feature from branch list to tags list. To restore the branch, checkout it by the tag.

How do I remove all branches from a repository?

Newer Git repositories have renamed the master branch to main. To delete all branches in Git except main, simply replace the grep for master with a grep for main: git branch | grep -v "main" | xargs git branch -D. git branch | grep -v " main$" | xargs git branch -D.


1 Answers

Here's one alternative: use non-branch references to save the branch-tips before deleting the branch names.

Since these are non-branch references, they won't show up in git branch output, nor in stuff shown by git log --branches and gitk --branches, for instance. However, they will show up in --all listings, and will retain repository objects.

To create or update a non-branch reference, use git update-ref. Choose a name-space within refs/ that you think will not collide with some future use (current uses are refs/heads/ for branches, refs/tags/ for tags, refs/remotes/ for remote branches, refs/notes/ for notes, and refs/stash for the stash). [Edit, July 2022: refs/namespaces/ is now reserved as well, and refs/replace/ is used by git replace. refs/bisect/, refs/rewritten/, and refs/worktree/ are reserved; refs/original/ is reserved if you will use git filter-branch. Gerrit, if you use it, has more reserved names. The list never seems to stop growing, so use care here.]

like image 157
torek Avatar answered Oct 09 '22 03:10

torek