DEV Community

Cover image for Rename local and remote branch
David Coy
David Coy

Posted on

4 3

Rename local and remote branch

We've all been there (or maybe you haven't, hopefully this helps prevent what's to follow): cloning the project, making changes in our local branch, then commiting those changes to the repository, only to find you didn't follow the contribution guidelines for branch naming. Recently, I opened a merge request against the docs for GitLab, but didn't follow our documentation guidelines for branch naming as I should have.

Initially, I created my branch: add-efs-warning, committed my changes and opened up the merge request. However, this was wrong as the branch name also needs to be prefixed with docs/, docs-, or -docs. Rather than copying the changes I made to a blank text file, changing to the master branch, deleting the old branch, then creating a new branch and applying the changes, I learned how to rename my local branch and remote branch.

Let's say you did the exact same thing I did and you're on your wrongly named local branch -- let's call it bad-branch-name. If you want to rename the branch, you can run:

git branch -m docs/new-branch-name

This will rename the current local branch to docs/new-branch-name and avoid feedback from the technical writers that review your changes. Next, you can run the following command to rename bad-branch-name to docs/new-branch-name and push the new local branch:

git push origin :bad-branch-name docs/new-branch-name

Finally, you can reset the upstream branch for the new local branch (switch to the branch if you're not currently working in it):

git push origin -u docs/new-branch-name

Cover image by Teddy Kelley on Unsplash

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay