

& git commit -m "Add file2"Įcho hello world > file3 & git add. Create a new branch and switch to it:Įcho hello world > file2 & git add. Now let’s try to delete a branch with unmerged changes. It will also display the hash of the commit pointed to by the branch: Let’s delete it right away:Īfter running the command, Git will let you know the branch was successfully deleted. Let’s call it new:Īt this point, you could switch to the new branch, add some commits to it, then go back and merge it into main. Start by creating a repository and adding a commit to it: There’s a way to recover them if you act quickly. What if you delete a branch with unmerged commits by mistake? Are those changes lost for good? Not yet. From the commit that main points to, you can only move backward, so there’s no way for you to reach the commits formerly pointed to by exp. Why?Ī commit has a reference to its parent-or parents-but not to its children. But unless you have the hash of the last commit, you won’t be able to reach them. If you go now and get rid of the exp branch, your repo will look like this:Īs you can see, the commits are still there. Take a look at the diagram the depicts the moment after you’ve added the new commits to exp but haven’t yet merged them back to main: Now let’s explore the scenario where the branch contains unmerged changes. The commits were already integrated into the original branch: Deleting a Branch With Unmerged Changes Let’s do that:Īt this point, if we delete the exp branch, nothing changes. In this situation, merging is just a matter of moving the main reference forward until it catches up to exp. As you can see, the main branch is behind exp. Go back and take a look at the last diagram. When you delete a branch, what happens depends on whether the branch was already merged. That’s right: The commits are still there, and you might be able to recover them. When you delete a branch in Git, you don’t delete the commits themselves. So, what would happen if you deleted the exp branch? What Happens If I Delete a Git Branch? You switch to the new branch and add two more commits to it. Now you create a new branch called “exp.” The branch currently points to the same commit, so the scenario looks like this: Imagine you create a repository and add three commits:

Branches are named, movable references to commits. What Branches Are In Git?īranching in Git is dead simple. Let’s start by examining what branches are in Git and what’s happening when you delete them. With that out of the way, we’ll now explore the deletion of branches in Git in some more depth. It’s like you’re pushing-sending-the order to delete the branch to the remote repository. You use git push, even if that sounds weird. You don’t use the git branch command to delete a remote branch. If you’re sure you want to do it, you’ll have to force the deletion by replacing the -d parameter with an uppercase D: If the branch contains unmerged changes, though, Git will refuse to delete it. To delete a local branch in Git, you simply run: If you just want to learn the correct incantation to delete branches in Git, we’ll start by offering the TL DR version. Also, make sure you have a GitHub account and are logged in. This tutorial assumes you have Git installed and know your way around the command line.

Most importantly, you’ll learn what branches are under the hood to have a more solid understanding of what happens when you delete them.
#BITBUCKET GIT DELETE BRANCH HOW TO#
You’ll learn how to delete branches, both locally and in your remote repositories, and whether it’s possible to recover a deleted branch. This post is all about the Git delete branch operation. Efficient branch management is essential for using Git to its fullest potential.
