Git Worktree Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 22: Line 22:
cd ../playground-temp
cd ../playground-temp
</syntaxhighlight>
</syntaxhighlight>
After operating changes, commit as usual, and possibly remove the working tree:
<syntaxhighlight lang='bash'>
git commit -m "some work"
</syntaxhighlight>
<syntaxhighlight lang='bash'>
cd ../playground
git worktree remove ../playground-temp
</syntaxhighlight>
=Work Tree Info=
<code>git branch</code> shows the branches active in different work tree in different colors: green in the current working tree, blue in other trees.
==List Work Trees==
<syntaxhighlight lang='bash'>
git worktree list
</syntaxhighlight>
=Create a Working Tree=
<syntaxhighlight lang='bash'>
git worktree add <absolute-or-relative-local-path> <branch|commit|tag>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
cd playground
git worktree add ../playground-kubernetes-experiments /task/kubernetes-experiments
</syntaxhighlight>
Note that <absolute-or-relative-local-path> must be outside of the main repository directory.
A remote branch for which there is no local tracking branch can be initialized in a work tree as follows. Note that the remote tracked branch is <remote>/<branch-name>.
<syntaxhighlight lang='bash'>
git worktree add --track -b <branch-name> <absolute-or-relative-local-path> <remote>/<branch-name>
</syntaxhighlight>
=Remove a Working Tree=
[[Git_Concepts#Linked_Working_Tree|Linked working trees]] can be removed with:
<syntaxhighlight lang='bash'>
git worktree remove <path>
</syntaxhighlight>
The full path can be used, or if the last segment of the path uniquely identifies the worktree, only that last segment can be used:
<syntaxhighlight lang='bash'>
git worktree remove ../playground-wk-1
</syntaxhighlight>
<syntaxhighlight lang='bash'>
git worktree remove playground-wk-1
</syntaxhighlight>
The [[Git_Concepts#Main_Working_Tree|main working tree]] cannot be removed.
It is possible to remove the work tree local filesystem directory without calling <code>git worktree remove</code>. The associated administrative files will be eventually removed automatically. Alternatively,
<syntaxhighlight lang='bash'>
git worktree prune
</syntaxhighlight>
can be execute to clean up state.
If a branch was checked out in the working tree that was removed, the branch remains in the local repository, it is just not checked out.

Latest revision as of 01:10, 16 July 2020

Internal

Typical Working Tree Workflow

Assuming that a developer is in the middle of a major refactoring on a branch and it is impractical to commit a stable checkpoint, or even stash, switching to a different branch while leaving the current branch in the state it is in involves:

git worktree add ../playground-temp /existing/task/branch/I/need/to/work/on

It is also possible to create a new branch on-the-fly. The branch is based on the branch specified in command line:

git worktree add -b temp-emergency-branch  ../playground-temp /existing/task/branch/I/need/to/work/on

To interact with the newly checked out branch in the new working tree, simply go to the working tree directory:

cd ../playground-temp

After operating changes, commit as usual, and possibly remove the working tree:

git commit -m "some work"
cd ../playground
git worktree remove ../playground-temp

Work Tree Info

git branch shows the branches active in different work tree in different colors: green in the current working tree, blue in other trees.

List Work Trees

git worktree list

Create a Working Tree

git worktree add <absolute-or-relative-local-path> <branch|commit|tag>
cd playground
git worktree add ../playground-kubernetes-experiments /task/kubernetes-experiments

Note that <absolute-or-relative-local-path> must be outside of the main repository directory.

A remote branch for which there is no local tracking branch can be initialized in a work tree as follows. Note that the remote tracked branch is <remote>/<branch-name>.

git worktree add --track -b <branch-name> <absolute-or-relative-local-path> <remote>/<branch-name>

Remove a Working Tree

Linked working trees can be removed with:

git worktree remove <path>

The full path can be used, or if the last segment of the path uniquely identifies the worktree, only that last segment can be used:

git worktree remove ../playground-wk-1
git worktree remove playground-wk-1

The main working tree cannot be removed.

It is possible to remove the work tree local filesystem directory without calling git worktree remove. The associated administrative files will be eventually removed automatically. Alternatively,

git worktree prune

can be execute to clean up state.

If a branch was checked out in the working tree that was removed, the branch remains in the local repository, it is just not checked out.