84

In Git, what does "deletion" of a branch mean?

Will it be gone from the repository? Or will it still be navigable to via git branch?

What I really want to do is mark a branch as "dead end", i.e., the branch is so far from master, that nobody should use it as a starting point, though there were some good ideas down that branch, so we'd like to keep it, for reference.

Sam
  • 7,252
  • 16
  • 46
  • 65
bobobobo
  • 64,917
  • 62
  • 258
  • 363

4 Answers4

163

You can delete the branch, but tag it first, so that it's history doesn't disappear. This way, the branch doesn't show up in the branch list, which should hopefully deter people from working on it, but the work won't be permanently erased (even after garbage collection is run). For instance, whenever I have a branch that has become irrelevant, but I'm not prepared to permanently delete it, I tag it as "archive/<branch-name>".

While on master or some other branch:

git tag archive/foo foo
git branch -D foo

This creates a tag named archive/foo from the foo branch before deleting foo. You can also add a message to the tag, that explains what is in the branch, why it existed, why it's now a dead end, etc.

git tag -m 'Foo is deprecated in favor of bar' archive/foo foo

The ability to record why a branch is being deprecated is perhaps an advantage of tagging versus moving branches to an alternate namespace.

If you ever need to resurrect a branch that has been archived this way, it's as simple as:

git branch foo archive/foo
git tag -d archive/foo       # Optional

Now the branch is back as though it was never deleted.

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • 5
    @sardaukar: This can also work with remote repos by pushing the archive tag (using the `--tags` option to `git push`) and then deleting the remote branch (using the `:` refspec while pushing). For example: `git tag archive/foo foo; git branch -D foo; git push --tags origin :foo` – Dan Moulding Sep 09 '11 at 22:04
  • It would be nice if git had a way to automatically inform people "this branch has been archived" and show the commit message, if people tried to pull from an archived branch, but it doesn't. – Robin Green Nov 13 '11 at 17:25
  • 1
    Very nice answer, exactly what I was looking for! Just to be sure, is this usefull if the branch has been merged back to the master before being deleted? – claf Nov 05 '12 at 17:27
  • Another related question (if someone still follow this thread) is how should I "close" (merge in master or archive in a tag or whatever) if there were multiple developpers involved in this branch? removing and tagging the branch (both local and remote) isn't too rude to other developpers of this branch? – claf Nov 05 '12 at 17:29
3

git branch -D <branchName> will delete your branch from repository. You will not be able to see it or navigate anymore. Also you will lose all file changes made in that branch.

https://git-scm.com/docs/git-branch

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Stewie Griffin
  • 9,257
  • 22
  • 70
  • 97
3

Git branches are stored as references to a revision. If you delete the branch, the reference is removed; if nothing else references that revision, it will eventually be garbage collected. Also, if you delete the branch then it's properly gone (from your repository). If you want to mark a branch as deprecated but keep it around for later use, you could move the branch to a subdirectory:

$ git branch
* master
  testing_feature_one
  testing_feature_two
$ git branch -m testing_feature_one deprecated/testing_feature_one
$ git branch
  deprecated/testing_feature_one
* master
  testing_feature_two

Alternatively, you could create a separate repository for deprecated branches, pull them across then delete them from the original. In either case, you're going to affect any users who are following the branches -- the content of their repository won't change (and neither will any of their branch names), but if they try to pull again, they'll have to change their target in their configuration.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
0

It won't be navigable via git branch and until garbage collection is performed, it won't be lost from the repository.

If you want to mark the branch in question as a dead end then simply do so (git may not do this for you but you certainly can)!

Labelling it (in any way you prefer) as a historical reference works.

Fake Code Monkey Rashid
  • 13,731
  • 6
  • 37
  • 41