Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Deleting A Git Branch From The Command Line

11/26/2016

0 Comments

 
When you start working on a project with multiple people who are all making multiple branches it can get to the point where you have a humongous list of branches, sometimes to the point where it's totally unmanageable. If your project is on github, you will see a huge list of possible branches in the dropdown box. This is something that really bothers me. Luckily, there's a pretty way to get rid of the unused ones, and in this post I'll show you show to do just that. 

Create a Test Branch To Play With

If you would like to follow along with me and branch deleting a branch, you can! Let's just create a new test branch so that we can mess round with it. You can create a new branch named "testes-branch" like this:
git checkout -tb testes-branch

Push Your New Branch

With the above code you've just created a new branch locally, but you haven't let your repo know about it yet. To do that you can push your new branch like so:
git push origin
Note that you don't need to make any commits or changes to the code itself in order to push your new branch here. After you've pushed the branch, you should be able to see it in the dropdown box on Github:
Picture

Switch Back To Master

Ok, you've created the new branch and pushed it up to the repo. Now, you can't delete a branch that you are currently on so let's switch back to master like so:
git checkout master

Deleting The Branch Remotely

This is how you delete a branch remotely:
git push origin :testes-branch
Simply run the git push command and prefix the name of the branch with a colon. This instructs git to delete the branch remotely. After running that you should no longer see the branch listed in the dropdown on Github. Booyah!

Deleting Remotely Is The Key

If you only delete locally, even if you push your changes back up, it won't actually delete the branch for good (and it won't be removed from the dropdown box on github). The key to really cleaning up after yourself is then to remotely delete your branches when you are finished with them. 

Deleting the Branch Locally

Although the above code deletes the branch remotely, you may notice that locally you still have a bunch of old branches. You can view your local branches like so:
git branch
You can then locally delete a branch like this:
git branch -d branch-name
Note that you can use the capital D flash (-D instead of -d) to force the deletion of the branch.

Deleting A Branch On PR Merge In Github 

It should also be noted that another really easy way to delete a branch remotely is through the "delete branch" button that appears on the github.com web interface after you accept a pull request and merge the branch into it's upstream parent branch. This is a good practice since in general you want to keep your branches focus on a certain thing and then open a pull request when that thing is finished. Then you can start working on a different feature in another branch while someone else reviews your pull request. This is just a nifty little shortcut for deleting your no-longer-needed branches. Ultimately, it's up to you to keep your repo clean! 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021