Jes Bak Hansen Jes Bak Hansen - 1 month ago 15
Git Question

How do I strip branches from a git repository?

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:

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

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?


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>