I don't want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.
How can I rename a local branch which hasn't been pushed to a remote branch?
I don't want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories. How can I rename a local branch which hasn't been pushed to a remote branch? |
||||
|
If you want to rename a branch while pointed to any branch, do:
If you want to rename the current branch, you can do:
A way to remember this, is |
|||||||||||||||||||||
|
The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any. If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):
For more details, see "How to rename your local branch name in Git." |
||||
|
To rename your current branch:
|
|||||
|
Here are the steps to rename the branch:
EDIT(12/01/2017) : Make sure you run command
|
||||
|
The answers so far have been correct but here is some additional info: One can rename a branch with '-m' (move), but one has to be careful, because '-M' forces the rename, even if there is an existing branch with the same name already. Here is the excerpt from the 'git-branch' man page:
|
|||||||||
|
I foolishly named a branch starting with a hyphen, and then checked out master. I didn't want to delete my branch, I had work in it. Neither of these worked:
Here's how I finally fixed it. Go into your working copy's .git/refs/heads, find the filename "-dumb-name", get the hash of the branch. Then this will check it out, make a new branch with a sane name, and delete the old one.
|
|||||||||||||||||||||
|
To rename a branch locally:
Now you'll have to propagate these changes on your remote server as well. To push changes of the deleted old branch:
To push changes of creation of new branch:
|
|||
|
|
||||
|
Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one. From my experience, to rename a local and remote branch in Git you should do the following steps.
1. Rename your local branchIf you are on the branch you want to rename:
If you are on a different branch:
2. Delete the old-name remote branch and push the new-name local branch
3. Reset the upstream branch for the new-name local branch
|
|||||
|
Rename the branch using this command:
If there is already a branch and you want to rename with that branch, use:
For more information about help, use this command in the terminal:
or
|
||||
|
Advanced Git users can rename manually:
|
||||
|
Trying to answer specifically to the question (at least the title). You can also rename local branch, but keeps tracking the old name on the remote.
Now, when you run You have to know and remember this configuration. But it can be useful if you don't have the choice for the remote branch name, but you don't like it (oh, I mean, you've got a very good reason not to like it !) and prefer a clearer name for your local branch. Playing with the fetch configuration, you can even rename the local remote-reference. i.e, having a |
|||
|
Another option is not to use the command line at all. Git GUI clients such as SourceTree take away much of the syntactical learning curve / pain that causes questions such as this one to be amongst the most viewed on Stack Overflow. In SourceTree, right click on any local branch in the "Branches" pane on the left and select "Rename ...". |
|||||||||
|
Probably as mentioned by others, this will be a case mismatch in branch naming. If you have such a situation, I can guess that you're on Windows which will also lead you to:
Then you have to do an intermediate step:
Nothing more. |
|||||
|
To rename current branch (except for detached HEAD state) you can also use this alias:
|
|||
|
Here are three steps: A command that you can call inside your terminal and change branch name.
If you need more: step-by-step, How To Change Git Branch Name is a good article about that. |
||||
|
Since you do not want to push the branch to a remote server, this example will be useful: Let's say you have an existing branch called "my-hot-feature," and you want to rename it to "feature-15." First, you want to change your local branch. This couldn't be easier:
For more information, you can visit Locally and Remotely Renaming a Branch in Git. |
||||
|
git version 2.9.2 If you want to change the name of the local branch you are on:
If you want to change the name of a different branch:
If you want to change the name of a different branch to a name that already exists:
Note: The last command is destructive and will rename your branch, but you will lose the old branch with that name and those commits because branch names must be unique. |
|||
|
If you want to change the name of the current branch, run:
If you want to delete the old remote branch, run:
If you want to delete the old remote branch and create a new remote branch, run:
|
||||
|