Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT Renaming a branch and keeping all commit history

I have a question regarding renaming a branch in git. I created a local branch dev and pushed branch to remote. I did lots of work on the dev branch and am updating remote branch regularly.

Now I want to change the name of the branch from dev to development. I know how to rename a branch in GIT.

My question is like if I rename the branch, does commit history to the dev-branch will be lost or not? If yes, how do i keep my commit-history?

like image 606
Rahul Avatar asked May 12 '14 09:05

Rahul


People also ask

Can I rename a branch in git?

The git branch command lets you rename a branch. To rename a branch, run git branch -m <old> <new>. “old” is the name of the branch you want to rename and “new” is the new name for the branch.

Can we change branch name after commit?

Branches are essentially pointers to a certain commit. Renaming a local Git Branch is a matter of running a single command. However, you can't directly rename a remote branch; you need to push the renamed local branch and delete the branch with the old name.


2 Answers

Now I want to change the name of the branch from dev to development, I know how to rename branch in GIT. My question is like if I rename the branch, does commit history to the dev-branch will be lost or not? If yes, how do i keep my commit-history?

You can simply create a new branch from your dev branch, and then delete the dev branch. The new branch will be a copy of your existing branch, I often do this to guard against breaking a branch when rebasing, or merging.

Here's a sample output:

# Normal state, for me at least
$ git branch
=> master
# Get into your dev branch.
$ git checkout dev
=> dev
# Now we make a new branch `development' based on `dev'
$ git checkout -b development
=> development
$ git branch -d dev

You can always check the git log before the last step, if you prefer. But all branches in Git are just special tagged references. Creating development from dev doesn't duplicate everything, so you don't waste any space by keeping it.

like image 153
Lee Hambley Avatar answered Oct 11 '22 07:10

Lee Hambley


As per man pages: git branch -move - move/rename a branch and the corresponding reflog.

like image 34
Vitalliuss Avatar answered Oct 11 '22 05:10

Vitalliuss