Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to a git branch after delete if it was not merged?

Tags:

git

github

I have a small repo with a master and an experimental branch for trying out some new features. I decided not to implement those features on the master.

What happens to the commits of the experimental branch when I delete that branch using git push origin --delete <branchName>? Will git keep some non accessible remnants? If I remember well I have tags on that branch and it is uploaded to github. Will this cause any problem?

edit:

I checked with gitk --all as suggested, the commit graph looks like this:

commit graph

What I want to remove the babel branch with all of its commits, and I don't want it to be restorable. Is this possible?

like image 670
inf3rno Avatar asked Jan 09 '16 14:01

inf3rno


People also ask

What happens when a branch is deleted in git?

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.

What does it mean if a branch is not fully merged?

05-10-2021. #GIT. There are times when you get an “not fully merged” error for a git branch, usually when trying to delete a local branch from your machine. It's a safe bet that something in your local branch has not actually made it to the remote repository and we should do some investigating.

Should git branches be deleted after merge?

When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.


3 Answers

Deleting a branch will only do that: Delete the branch. Since a branch is just a reference to a commit, only that reference is gone. The commit is still there.

Similar to programming languages, Git has a garbage collection which will eventually remove commit objects which are no longer referenced. So if that branch was the only pointer to that commit, you will effectively lose access to it, and it will be deleted “soon”.

If you have other references, that includes other branches, tags, and commits that indirectly reference that commit (i.e. for which that commit is a parent), then the commit is still referenced and it will not be deleted.

So if you want to get rid of a commit, you would need to remove all references to that commit. If you want to delete the branch, and there are other things referencing a commit from that branch, then that is not a problem and will not cause problems: It is desired behavior that the commit is then kept so the other things don’t break.

like image 135
poke Avatar answered Sep 27 '22 22:09

poke


What happens to the commits of the experimental branch when I delete that branch using git push origin --delete <branchName> ?

The specific branch will be deleted in your local and your remote repository. I might not be possible to restore it again.

Will git keep some non accessible remnants?

According to this answer https://stackoverflow.com/a/4674570/1654730, it might be possible to restore the branch with the help of the reflog.

If I remember well I have tags on that branch and it is uploaded to github. Will this cause any problem?

This should not cause any problems. If you have tags on that branch you can still access the tags and create a new branch based on them.

like image 21
florieger Avatar answered Sep 27 '22 22:09

florieger


Yes, git will keep some non-accessible remnants - at least to begin with. Commits that can't be accessed from existing branches or tags are called dangling commits. See this question for example: Git: what is a dangling commit/blob and where do they come from?

If you created tags on the branch and pushed those tags to GitHub, then the tags will still exist in your local repository, so commits accessible from those tags won't ever be deleted.

You could use for example gitk --all to show all branches and tags in your local repository, and if you see tags that were on the old branch that you have now deleted, you could delete each tag as well, using

git tag -d TAG-NAME

to delete the tag locally, and also delete the tag from GitHub using:

git push origin :refs/tags/TAG-NAME

Non-accessible commits are eventually removed when git performs garbage collection in the repository. As mentioned in the other Stack Overflow question, there's more information about this in the Maintenance and Data Recovery section on git-scm.com.

like image 34
Richard Fearn Avatar answered Sep 27 '22 21:09

Richard Fearn