Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it safe to delete local branch?

The situation is like this:

I created remote branch from other remote develop branch -> fetched it to local -> made some changes -> git add -> git commit -> pushed to remote and made pull request.

Pull request is still there and branch hasn't been merged yet.

Is it safe to delete local branch that hasn't been merged yet?

I don't want to have bunch of local branches that I don't need anymore.

like image 616
Phar0x Avatar asked Jul 14 '15 08:07

Phar0x


People also ask

What does it mean to delete a local branch?

That means you no longer need to keep and use that branch, so it is a common best practice to delete it so it doesn't clutter up your code. Local branches are branches on your local machine and do not affect any remote branches. The command to delete a local branch in Git is: git branch is the command to delete a branch locally.

Why does Git refuse to delete my local branch?

In some cases, Git might refuse to delete your local branch: when it contains commits that haven't been merged into any other local branches or pushed to a remote repository. This is a very sensible rule that protects you from inadvertently losing commit data.

How do I delete a branch without losing history?

You can safely remove a branch with git branch -d yourbranch. If it contains unmerged changes (ie, you would lose commits by deleting the branch), git will tell you and won't delete it. So, deleting a merged branch is cheap and won't make you lose any history.

What is a local branch in Git?

Local branches are branches on your local machine and do not affect any remote branches. The command to delete a local branch in Git is: git branch is the command to delete a branch locally.


3 Answers

It's technically safe to delete a local branch once you've pushed it to a remote branch , as you could always retrieve your changes back from your remote branch, even if the pull request is not merged yet.

However, I'd wait with it until the pull request is actually merged. The whole idea behind having pull requests, as opposed to allowing anyone to just merge what they want, is to allow a feedback loop between the developer and the maintainer of the project. Depends on the project's etiquette and the level of trust the maintainer has towards you, it may take several roundtrips of improvements before your pull requests is merged. I'd avoid the hassle of having to recreate your local branch each time and just leave it there and work on it until the request is finally merged.

like image 94
Mureinik Avatar answered Oct 11 '22 14:10

Mureinik


It is safe to delete your local branch after you pushed your changes to your own remote repository.

The pull request is unrelated to this, because it is simply a request to the maintainers of the original repository to merge your changes back into their code base. For that, they need access your remote repository, but of course not to your local files.

like image 39
Sebastian P. Avatar answered Oct 11 '22 16:10

Sebastian P.


The remote will have all the commit objects that contain your work, so you could remove your local branch, though why bother?

Branches are extremely light weight (being just 40 byte pointers in the repository to the commit object). Your repository and the external will have the same commit objects, and should you be required to do further work before the pull request is accepted, you'll have an easier job moving to your local branch than if you delete it and have to pull down your own change and work off that.

like image 1
Mark Fisher Avatar answered Oct 11 '22 16:10

Mark Fisher