1

For version control, I've been mostly exposed to Mercurial, but want to get better at using Git as well. There's one workflow I'm used to do that doesn't seem to be shown much in the tutorials and examples for Git beginners, namely removing a branch from your repository.

The scenario is this: The master branch has a bunch of commits, and at some point, I made a feature branch for a feature which might be merged in at some point in time. However; the branch was abandoned, and more commits were added to master. Now it's been decided that the development of the features on the feature branch is not needed, and should not be pushed to the central repository. In mercurial, using TortoiseHg, I'd then do the following:

https://i.stack.imgur.com/aT3Ib.png

Whereby commits numbered 4, 5 and 6 are removed from the repo, leaving me with the desired, final repository:

https://i.stack.imgur.com/KEX4j.png

I've tried looking at using git branch -D <branch-name>, but that doesn't seem to do what I expected it to do. It seems the commits on that branch are retained in the repository, it just removes the branch name.

What's the best/safest/easiest/quickest/name-more-types-here way to achieve that workflow with a Git repository?

Pulkit Goyal
  • 780
  • 4
  • 20
  • In Git, you could just leave the unwanted branch alone and do not push it or merge it to other branches, or `git branch -D ` to delete the branch itself. If you use a Git GUI, there is expected to be an item like `delete the branch` in the menu. – ElpieKay Oct 06 '16 at 16:56
  • Yeah, that could be a solution for the other users of the central repo. The 'cheating' way would then be to clone the central repo again, so that I'd get a new repo without the unwanted branch. I was hoping for a way to salvage the repo with the dirty branch in it. How do I purge it, like I can in my mercurial repo? – Jes Bak Hansen Oct 06 '16 at 17:08

1 Answers1

0

Delete the remote branch

git push <remote name> --delete <branch name>

Now others need to prune their tracking branches

git fetch --prune <remote name>

Delete the local branch

git branch -D <branch name>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167