I Am
Volodymyr Hudyma
<FrontEndDeveloper />
You Are Here: Home/How To Delete And Restore Branches In Git?

How To Delete And Restore Branches In Git?

February 17, 2021

Table Of Contents

    Nowadays, it's hard to imagine product development without using Git branches to deliver a part of the functionality.

    A branch is an independent line of development used in order not to interfere with the main line, which can even be used in production.

    There is no limit to the number of branches you can have, but it is a good practice to delete the unused branches, both locally and on Github.

    Today we'll learn how to delete local and remote Git branches, and also how to undo the delete operation if needed.

    Delete Local Branch

    If the branch has not been pushed to the remote, it can be easily deleted with the following command:

    git branch -d <name>

    Let's create a new branch feat/feature and delete it:

    Git Delete Branch

    However, there are a few cases where Git refuses to delete your local branch:

    • When you are on the branch you want to delete

    Git Delete Branch Fail [1]

    • When it contains commits that have not been merged into another branch or pushed to remote

    Git Delete Branch Fail [2]

    If there was no protection, you could have accidentally deleted the branch and lost all your data.

    But if you are really sure that you want the unmerged branch to be deleted anyway, you can use the following command:

    git branch -D <name>


    Git Delete Unmerged Branch

    Delete Remote Branch

    If the branch has been pushed to remote, you can delete it with the following command:

    git push origin --delete <name>


    Git Delete Remote Branch

    Note that the branch is not deleted locally.

    Undo Branch Delete

    If you have accidentally deleted a local branch with some important changes, you can restore the changes with a combination of the following commands:

    git reflog


    git checkout -b <name> <hash>

    Create a branch and delete it:

    Create And Delete Git Branch

    Use reflog to see what has been done recently:

    Git Reflog Output

    And create a new branch with the deleted commit:

    Git Recover Deleted Commit


    In this article, we learned how to delete local and remote Git branches and also how to undo the deletion if it happened accidentally.

    Be sure to play around with the commands to learn and understand them better.

    Receive all new posts directly to your e-mail
    No spam, only quality content twice a week
    Let me know what you think about this article
    Click here to write response...