Bird
Raised Fist0
Gitdevops~10 mins

Listing worktrees in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Listing worktrees
Run 'git worktree list'
↓
Git reads .git/worktrees directory
↓
Git gathers info on each worktree
↓
Git outputs list with paths and HEAD info
↓
Display result
The command reads the worktrees info and shows a list of all linked worktrees with their paths and current branch or commit.
Execution Sample
Git
git worktree list
Lists all worktrees linked to the current repository with their paths and HEAD states.
Process Table
StepActionGit Internal ProcessOutput
1User runs 'git worktree list'Git starts reading worktree info
2Git reads .git/worktrees directoryFinds all linked worktrees
3Git reads HEAD info for each worktreeGets current branch or commit for each
4Git formats list outputPrepares path and HEAD info lines
5Git prints list to terminalOutput shows each worktree path and HEAD/path/to/main-worktree 123abc [main] /path/to/feature-worktree 456def [feature-branch]
6Command endsAll worktrees listedDone
💡 All linked worktrees have been listed with their paths and HEAD states.
Status Tracker
VariableStartAfter Step 2After Step 3Final
worktrees_listemptypaths of all worktrees foundpaths + HEAD info for each worktreecomplete list with paths and HEAD info
Key Moments - 2 Insights
Why does the output show commit hashes and branch names?
Because git reads the HEAD of each worktree to show what commit or branch it currently points to, as seen in execution_table step 3 and 5.
What if I have no extra worktrees created?
The list will show only the main worktree path and its HEAD info, or just one entry, as git always includes the main worktree.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 5 output, what does '[feature-branch]' represent?
AThe current branch checked out in that worktree
BThe remote repository URL
CThe last commit message
DThe worktree creation date
💡 Hint
Refer to execution_table step 3 and 5 where HEAD info is read and displayed.
At which step does git gather the commit hashes for each worktree?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Check execution_table where HEAD info is read for each worktree.
If you create a new worktree, how will the variable 'worktrees_list' change after step 2?
AIt will include commit hashes
BIt will remain empty
CIt will include the new worktree path
DIt will be deleted
💡 Hint
See variable_tracker after step 2 where paths of all worktrees are found.
Concept Snapshot
git worktree list
- Lists all worktrees linked to the repo
- Shows path and current HEAD (branch or commit)
- Reads .git/worktrees directory
- Useful to see multiple working copies
- Output includes main and extra worktrees
Full Transcript
The 'git worktree list' command shows all worktrees connected to your git repository. When you run it, git reads the .git/worktrees directory to find all linked worktrees. Then it reads the HEAD information for each worktree to find out which branch or commit is currently checked out. Finally, it prints a list showing each worktree's path and its HEAD state. This helps you see all your working copies and their current branches or commits at a glance.

Practice

(1/5)
1. What does the command git worktree list do?
easy
A. Deletes all existing worktrees
B. Shows the commit history of the current branch
C. Creates a new worktree
D. Shows all the linked worktrees with their paths and branches

Solution

  1. Step 1: Understand the purpose of git worktree list

    This command lists all worktrees connected to the repository, showing their folder paths and branches.
  2. Step 2: Compare with other options

    Options A and C describe deleting or creating worktrees, which are done by different commands. Shows the commit history of the current branch shows commit history, unrelated to worktrees.
  3. Final Answer:

    Shows all the linked worktrees with their paths and branches -> Option D
  4. Quick Check:

    List worktrees = B [OK]
Hint: Remember: 'list' shows all worktrees and their details [OK]
Common Mistakes:
  • Confusing 'list' with 'add' or 'remove' commands
  • Thinking it shows commit history
  • Assuming it deletes worktrees
2. Which of the following is the correct syntax to list all git worktrees?
easy
A. git worktree show
B. git worktree ls
C. git worktree list
D. git worktree status

Solution

  1. Step 1: Recall the exact command for listing worktrees

    The correct command is git worktree list to display all worktrees.
  2. Step 2: Verify other options

    Options A, B, and D are invalid or do not exist in git worktree commands.
  3. Final Answer:

    git worktree list -> Option C
  4. Quick Check:

    Correct syntax for listing = C [OK]
Hint: Use full command 'git worktree list' to avoid errors [OK]
Common Mistakes:
  • Using 'show' or 'status' instead of 'list'
  • Shortening 'list' to 'ls' which is invalid
  • Confusing with other git commands
3. Given the output of git worktree list below, what branch is checked out in the worktree at /projects/myrepo/feature?
/projects/myrepo  123abc [main]
/projects/myrepo/feature  456def [feature-branch]
medium
A. main
B. feature-branch
C. 123abc
D. 456def

Solution

  1. Step 1: Identify the worktree path in the output

    The path /projects/myrepo/feature corresponds to the second line in the output.
  2. Step 2: Read the branch name in brackets

    The branch checked out there is feature-branch, shown in square brackets.
  3. Final Answer:

    feature-branch -> Option B
  4. Quick Check:

    Branch at /feature = feature-branch [OK]
Hint: Branch name is always in brackets after path [OK]
Common Mistakes:
  • Confusing commit hashes with branch names
  • Picking the wrong line for the path
  • Ignoring the brackets around branch names
4. You ran git worktree list but got an error: fatal: 'worktree' is not a git command. What is the most likely cause?
medium
A. Your git version is too old and does not support worktrees
B. You typed 'worktree' instead of 'worktrees'
C. You are not inside a git repository
D. You need to initialize worktrees with a special command first

Solution

  1. Step 1: Understand the error message

    The error means git does not recognize the 'worktree' command, likely due to version.
  2. Step 2: Check git version support

    Worktrees were introduced in git 2.5+. Older versions lack this command.
  3. Final Answer:

    Your git version is too old and does not support worktrees -> Option A
  4. Quick Check:

    Unsupported git version = A [OK]
Hint: Check git version if 'worktree' command not found [OK]
Common Mistakes:
  • Assuming typo in command name
  • Thinking worktrees need special init
  • Not verifying git version
5. You have multiple worktrees for your project. You want to list all worktrees and filter only those on the branch named 'release'. Which command combination will achieve this?
hard
A. git worktree list | grep release
B. git worktree list --branch release
C. git worktree show release
D. git worktree list --filter=branch=release

Solution

  1. Step 1: Understand git worktree list filtering options

    Git worktree list does not have built-in branch filtering options like --branch or --filter.
  2. Step 2: Use shell tools to filter output

    Using grep release after listing worktrees filters lines containing 'release', showing only those branches.
  3. Final Answer:

    git worktree list | grep release -> Option A
  4. Quick Check:

    Filter branches by grep after list = D [OK]
Hint: Use shell grep to filter worktree list output by branch [OK]
Common Mistakes:
  • Assuming git worktree list supports --branch or --filter
  • Using 'show' instead of 'list'
  • Expecting git to filter output internally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard