Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove a branch and all of its history locally and remotely?

I have a git branch I need to nuke all traces of. I think I need something like

git filter-branch --index-filter 'git -D <branch>' --tag-name-filter cat -- --all

or maybe

git filter-branch --index-filter 'git rm --cached *' --tag-name-filter cat -- <branch>

Then I would

git push -f origin :<branch>

I'm hesitant to try them without knowing for sure what they'll do or if they'll work.

like image 671
drs Avatar asked Jan 07 '16 13:01

drs


People also ask

How do you delete a branch both locally and remotely?

Simply do git push origin --delete to delete your remote branch only, add the name of the branch at the end and this will delete and push it to remote at the same time... Also, git branch -D , which simply delete the local branch only!...

Does deleting a local branch delete the remote branch?

In Git, local and remote branches are separate objects. Deleting a local branch doesn't remove the remote branch.

Does deleting a branch remove it from history?

Like all deletes in svn, the branch is never really deleted, it's just removed from the current tree.

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.

How to delete a remote branch in Git?

The command to delete a remote branch is: Instead of using the git branch command that you use for local branches, you can delete a remote branche with the git push command. Then you specify the name of the remote, which in most cases is origin.

How to delete the main branch from my Test Branch?

Here we will check out our main branch from my test branch. Now in order to delete the test branch locally, we use the command : We will delete my test branch as an example. Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch.

How do I list all the local branches I have?

To list out all the local branches, you use the following command: I have two, branches, master and test2. I am currently on the test2 branch as the (*) shows: I want to delete the test2 branch, butit is not possible to delete a branch you are currently in and viewing. If you try to do so, you'll get an error that will look something like this:


1 Answers

Let's say you want to nuke evilbranch.

You say you want to delete all of a branch's history. This technically includes the initial commit, which is probably more commits then you want. So first, identify which commit you consider to be the first commit of that branch. Let's say it's 666bad. Now we need to find all references to it. Run

git branch -a --contains 666bad
git tag --contains 666bad

Now delete them all. You can either use git commands, or just go into .git/refs.

Do this on every computer that might have the file.

Make sure you are not in detached head.

Now we can kill all the commits, and therefore all the code, that is no longer referenceable (from this gitHub link):

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

Again, this should be done on each computer.

Finally, use this on each computer.

Note: 666bad is the first commit after evilbranch split from wherever it came from, i.e., the first commit that is only evilbranch.

like image 61
PyRulez Avatar answered Sep 26 '22 02:09

PyRulez