-1

I created a branch/tree with commits in a git repo a while ago that I will never use. How can I permanently remove that from my repo? This is all local - no remote repos or other users

before:

--A-----------D---E---F---G  <- master/head
   \---B---C                 <- will never use

what I'd like:

--A---D---E---F---G  <- master/head

Mercurial has a strip command that does what I need and hoped git has something similar

RShepherd
  • 1
  • 1
  • Do nothing. It will be garbage-collected eventually. – mkrieger1 Nov 13 '19 at 22:41
  • 1
    Possible duplicate of [Listing and deleting Git commits that are under no branch (dangling?)](https://stackoverflow.com/questions/3765234/listing-and-deleting-git-commits-that-are-under-no-branch-dangling) – mkrieger1 Nov 13 '19 at 22:42
  • 1
    If there is no branch referring to commit `C`, you will not see `B` and `C` anymore, even if they technically still exist. – mkrieger1 Nov 13 '19 at 22:43
  • I ran git gc but B and C still exist – RShepherd Nov 13 '19 at 23:15
  • Thanks all. Still learning git but now in the right direction on this. And found I use a log alias with "--all" which is part of my problem. Can I close this or does a Mod close questions? – RShepherd Nov 13 '19 at 23:56

2 Answers2

1

I first move to another branch, not the one I want to delete, and then execute:

git branch -D branch_name

To delete a local branch. The -D is an alias for --delete --force, which deletes the branch 'irrespective' of its merged status.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
-1
  1. Checkout master branch
    git checkout master
    
  2. Delete local branch
    git branch -d local_branch_name
    
    Note: -D is the same as -d --force

Additionally, to get remote repo updates for master:

git pull origin master
roasty
  • 172
  • 1
  • 9
  • 1
    **Don't** use `git pull` unless you know exactly what you will get. `git pull` does `git fetch` followed by `git merge`. It is wiser to run `git fetch` and inspect the result then run either `git merge` or, even better `git rebase`. – axiac Nov 13 '19 at 23:04