Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens in the child branch if I delete a parent branch in git

Tags:

I am planning to rename a branch in my git repository. I found out that the easy way to do that is to make a new branch from that branch and give it the desired name.

After that I want to delete the old branch (the parent). But I'm afraid that I will lose data in my new branch if I do that.

What happens with the commits originally made to the parent branch if I delete that branch?

like image 428
Willem de Jong Avatar asked Oct 15 '13 12:10

Willem de Jong


People also ask

Does deleting git branch delete history?

The commits will still be retained in the repository and it is possible to recover them immediately after the delete, but eventually they will be garbage collected. Thanks for the answer.

Is it okay to delete a branch in your remote repository?

It is safe to delete your local branch after you pushed your changes to your own remote repository. The pull request is unrelated to this, because it is simply a request to the maintainers of the original repository to merge your changes back into their code base.

Does deleting local branch delete remote branch?

In Git, local and remote branches are separate objects. Deleting a local branch doesn't remove the remote branch.

Does deleting a branch delete sub branches?

Mikkel Rasmussen. It would not delete the bar_feature branch. From the article: If the branch was merged into another branch before it was deleted then all of the commits will still be reachable from the other branch when the first branch is deleted. They remain exactly as they were.


1 Answers

What happens? nothing.

If you create a branch where another is, you can "delete" that other branch without losing anything. A branch (HEAD) is just a pointer to a commit.
As long as those commits are referenced by a branch HEAD (or are part of the branch HEAD ancestors), they aren't lost.
And even if they are no longer referenced by any branch or tag, they are still in the local reflog for (by default) 90 days.

But, looking at the man page for git branch, this seems easier:

 git branch (-m | -M) [<oldbranch>] <newbranch> 

With:

-m --move 

Move/rename a branch and the corresponding reflog.

-M 

Move/rename a branch even if the new branch name already exists.

like image 198
VonC Avatar answered Sep 19 '22 11:09

VonC