• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

How to rename a branch, delete a branch, and find the author of a branch in Git

How to rename a branch, delete a branch, and find the author of a branch in Git: How to rename a branch, delete a branch, and find the author of a branch in Git Agil Antony Fri, 05/20/2022 – 03:00

Register or Login to like
Register or Login to like

One of Git’s primary strengths is its ability to “fork” work into different branches.

If you’re the only person using a repository, the benefits are modest, but once you start working with many other contributors, branching is essential. Git’s branching mechanism allows multiple people to work on a project, and even on the same file, at the same time. Users can introduce different features, independent of one another, and then merge the changes back to a main branch later. A branch created specifically for one purpose, such as adding a new feature or fixing a known bug, is sometimes called a topic branch.

Once you start working with branches, it’s helpful to know how to manage them. Here are the most common tasks developers do with Git branches in the real world.

More on Git
What is Git?
Git cheat sheet
Markdown cheat sheet
New Git articles

Rename a branch using Git

Renaming a topic branch is useful if you have named a branch incorrectly or you want to use the same branch to switch between different bugs or tasks after merging the content into the main branch.

Rename a local branch

1. Rename the local branch:

 $   git branch -m <old_branch_name> <new_branch_name>

Of course, this only renames your copy of the branch. If the branch exists on the remote Git server, continue to the next steps.

2. Push the new branch to create a new remote branch:

 $   git push origin <new_branch_name>

3. Delete the old remote branch:

 $   git push origin -d -f <old_branch_name>

Rename the current branch

When the branch you want to rename is your current branch, you don’t need to specify the existing branch name.

1. Rename the current branch:

 $   git branch -m <new_branch_name>

2. Push the new branch to create a new remote branch:

 $   git push origin <new_branch_name>

3. Delete the old remote branch:

 $   git push origin -d -f <old_branch_name>

Delete local and remote branches using Git

As part of good repository hygiene, it’s often recommended that you delete a branch after ensuring you have merged the content into the main branch.

Delete a local branch

Deleting a local branch only deletes the copy of that branch that exists on your system. If the branch has already been pushed to the remote repository, it remains available to everyone working with the repo.

1. Checkout the central branch of your repository (such as main or master):

 $   git checkout <central_branch_name>

2. List all the branches (local as well as remote):

 $   git branch -a

3. Delete the local branch:

 $   git branch -d <name_of_the_branch>

To remove all your local topic branches and retain only the main branch:

 $   git branch | grep -v main | xargs git branch -d

Delete a remote branch

Deleting a remote branch only deletes the copy of that branch that exists on the remote server. Should you decide that you didn’t want to delete the branch after all, you can re-push it to the remote, such as GitHub, as long as you still have your local copy.

1. Checkout the central branch of your repository (usually main or master):

 $   git checkout <central_branch_name>

2. List all branches (local as well as remote):

 $   git branch -a

3. Delete the remote branch:

 $   git push origin -d <name_of_the_branch>

Find the author of a remote topic branch using Git

If you are the repository manager, you might need to do this so you can inform the author of an unused branch that it should be deleted.

1. Checkout the central branch of your repository (such as main or master):

 $   git checkout <central_branch_name>

2. Delete branch references to remote branches that do not exist:

 $   git remote prune origin

3. List the author of all the remote topic branches in the repository, using the --format option along with special selectors (in this example, %(authorname) and %(refname) for author and branch name) to print just the information you want:

 $   git for-each-ref --sort=authordate --format='%(authorname) %(refname)' refs/remotes

Example output:

 
tux  refs/remotes/origin/dev
agil refs/remotes/origin/main

You can add further formatting, including color coding and string manipulation, for easier readability:

 
$ git for-each-ref --sort=authordate \
--format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)%(align:25,left)%(color:yellow) %(authorname)%(end)%(color:reset)%(refname:strip=3)' \
refs/remotes

Example output:

 
01/16/2019 03:18 PM tux      dev
05/15/2022 10:35 PM agil     main

You can use grep to get the author of a specific remote topic branch:

 
$ git for-each-ref --sort=authordate \
--format='%(authorname) %(refname)' \
refs/remotes | grep <topic_branch_name>

Get good at branching

There are nuances to how Git branching works depending on the point at which you want to fork the code base, how the repository maintainer manages branches, squashing, rebasing, and so on. Here are three articles for further reading on this topic:

  • Explaining Git branches with a LEGO analogy, by Seth Kenlon
  • My guide to using the Git push command safely, by Noaa Barki
  • A guide to Git branching, by Kedar Vijay Kulkarni

Become an expert at the most common Git tasks for managing local and remote branches.

tree branches
Image by:

Image by Erik Fitzpatrick. CC BY-SA 4.0

Git

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Agil Antony

Supporting Open Source.

Have you tried: Diving with Sharks?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 20 May 2022 by Alan Category: SoftwareTag: author, Branch, delete, Find, rename

Trending Now: Real Madrid

These are the latest trending searches on Google right now: Real Madrid: PSG vs Real Madrid, PSG, …

Read moreTrending Now: Real Madrid

Inside Charlie Heaton’s life from age gap with drummer ex to becoming a dad at 20

Stranger Things actor Charlie Heaton will star in the upcoming Peaky Blinders spin-off – but who is …

Read moreInside Charlie Heaton’s life from age gap with drummer ex to becoming a dad at 20

Trending Now: James Webb Space Telescope

These are the latest trending searches on Google right now: James Webb Space Telescope: James Webb …

Read moreTrending Now: James Webb Space Telescope

About Alan

Previous Post:2014-10-22 V8 Superfest Beach Volleyball 031
Next Post:COVID in South Africa: what to expect in the short and long term

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top