Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git worktree prune - what it does?

Tags:

git

I'm using git version 2.14.2.windows.2 and invoked

git worktree add <newpath> <branch>

on a "normal" repository to create a worktree. This created a new working tree structure at <newpath> for the branch <branch> and added .git/worktrees/<newpath-name> with a couple of files.

When invoking

git worktree prune

from the <newpath> it prints nothing and seems to do nothing (all the files in <newpath> and <original-dir>/.git/worktrees are kept). The Git documentation writes:

Prune working tree information in $GIT_DIR/worktrees.

which does not help much. What actually git worktree prune does or should do? Am I wrong in the assumption that it would remove the worktree to undo the git worktree add?

like image 222
Thomas S. Avatar asked Jan 19 '18 17:01

Thomas S.


People also ask

What does git Worktree prune do?

git worktree prune removes information about (non-locked) worktrees which no longer exist. For example, $ mkdir a $ cd a $ git init $ git commit --allow-empty --allow-empty-message -m '' $ git worktree add ../b $ ls .git/worktrees/ b $ rm -rf ../b $ git worktree prune $ ls .git/worktrees/ $

What is the working tree?

Th Working Tree in Git is a directory (and its files and subdirectories) on your file system that is associated with a repository. It's full of the files you edit, where you add new files, and from which you remove unneeded files.

What is git Worktree add?

In its simplest form, git worktree add <path> automatically creates a new branch whose name is the final component of <path> , which is convenient if you plan to work on a new topic. For instance, git worktree add ../hotfix creates new branch hotfix and checks it out at path ../hotfix .

How do I switch between Worktrees in git?

In your case, you can just run git checkout refs/heads/master . It leads to detached HEAD state. refs/heads/master can be replaced with the specific commit. Create as many worktrees for a detached HEAD as you want to.


1 Answers

git worktree prune removes information about (non-locked) worktrees which no longer exist. For example,

$ mkdir a
$ cd a
$ git init
$ git commit --allow-empty --allow-empty-message -m ''
$ git worktree add ../b
$ ls .git/worktrees/
b
$ rm -rf ../b
$ git worktree prune
$ ls .git/worktrees/
$
like image 95
ephemient Avatar answered Oct 07 '22 15:10

ephemient