Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a git working tree branch when its working directory has been removed?

Tags:

git

I've removed its working directory, because this git working tree is no longer useful, then I git branch -D pubsub-sketch-tree at the directory of main repository. An error thrown:

error: Cannot delete branch 'pubsub-sketch-tree' checked out at '/Users/zhouhancheng/编程/github_own/sketch_worktree/pubsub-sketch_tree'  

But '/Users/zhouhancheng/编程/github_own/sketch_worktree/pubsub-sketch_tree' had been removed.

like image 823
周汉成 Avatar asked May 22 '17 09:05

周汉成


People also ask

How do I delete a git working tree?

Though you can directly delete a file or files from a working tree and commit the changes using the command 'git add -A' or 'git commit -a', but Git provides an alternative way to delete the file using the 'git rm' command.

Why can't I delete my git branch?

If the branch contains unmerged changes, though, Git will refuse to delete it. If you're sure you want to do it, you'll have to force the deletion by replacing the -d parameter with an uppercase D: It's like you're pushing—sending—the order to delete the branch to the remote repository.

Can not delete local branch?

Delete a local branch You can't delete a branch if you're checked out that branch. You will see this error: Cannot delete branch 'branch-name' checked out at 'some-location'. To fix this, you will have to switch to a different branch. -d is shortcut for —-delete and it deletes a branch.


2 Answers

You are using git worktree, so the answer is in the git worktree documentation:

When you are done with a linked working tree you can simply delete it. The working tree’s administrative files in the repository (see "DETAILS" below) will eventually be removed automatically (see gc.worktreePruneExpire in git-config(1)), or you can run git worktree prune in the main or any linked working tree to clean up any stale administrative files.

(emphasis mine). Git won't let you delete the branch if it believes it is checked out in a secondary worktree. If the secondary worktree is already removed, but Git has not yet caught on to that fact, just run git worktree prune to tell Git to go check.

like image 197
torek Avatar answered Oct 18 '22 14:10

torek


I've removed its working directory

You will have an easier time with Git 2.17+ (Q2 2018), since "git worktree" has learned 'move' and 'remove' subcommands.

See commit 7f19def (04 Mar 2018) by Eric Sunshine (sunshineco).
See commit ee6763a, commit cc73385, commit 78d986b, commit c64a8d2, commit 9f792bb, commit 9c620fc (12 Feb 2018), and commit 4ddddc1 (24 Jan 2018) by Nguyễn Thái Ngọc Duy (pclouds).
(Merged by Junio C Hamano -- gitster -- in commit bd0f794, 14 Mar 2018)

worktree remove: new command

This command allows to delete a worktree. Like 'move' you cannot remove the main worktree, or one with submodules inside.

For deleting $GIT_WORK_TREE, Untracked files or any staged entries are considered precious and therefore prevent removal by default. Ignored files are not precious.


worktree remove: allow it when $GIT_WORK_TREE is already gone

"git worktree remove" basically consists of two things

  • delete $GIT_WORK_TREE
  • delete $GIT_DIR (which is $SUPER_GIT_DIR/worktrees/something)

If $GIT_WORK_TREE is already gone for some reason, we should be able to finish the job by deleting $GIT_DIR.

like image 13
VonC Avatar answered Oct 18 '22 15:10

VonC