31

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?

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Simply use git format-patch origin -o {output_folder}.

After getting all the patches out, go to the master and create a new branch.

Then apply those patches as so: git am {output_folder}/{patch_name}.patch.

Then after verifying you didn't loose any info, you can delete the old branch with git Branch -D {name_old_branch}

1 Comment

Between branches that's not necessary, just creating another branch tag would do the trick. But I've just used this method successfully to apply patches to a moved repo. Changesets were created based on the old repo (location) but needed to be pushed to the other, and git format-patch was a great way to preserve the timestamps and change distribution between commits.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.