TT Lab
Get started
Learn Learning paths Courses

Git in Practice

A Hotfix Lands While Your Work Is Half Open

Continue in TT Lab

Goal

You attach up to three working directories to a single repository and then clean them up. When you finish, you will have the skill of switching branches without stash, and you will know how to clear away the administrative files that a working copy leaves behind.

Why it matters

If you think about why switching branches is so cumbersome, it is because there is only one working tree. A repository holds the objects and branch references, and a working tree is a copy in which one of those commits is laid out. If you could increase the places where it is laid out, switching itself would become unnecessary. git worktree does exactly that. But the repository holds the branch references as one, so if you try to open the same branch in two places, git stops you. Understanding this constraint is half of this lab. The other half is cleanup — if you delete only the directory, the administrative files remain, and that place keeps showing in the list as prunable.

Steps

  1. Create a repository with 3 commits at /root/gitx4/repo and branch off a release branch.
  2. Attach release as a working copy at /root/gitx4/rel.
  3. Try attaching main once more at /root/gitx4/dup, and leave the refusal message in notes/dup.txt.
  4. While leaving unsaved changes in repo, attach at /root/gitx4/hot a newly created hotfix branch, and commit hotfix.txt there.
  5. Save git worktree list --porcelain to notes/list.txt.
  6. Attach /root/gitx4/tmp, then rm -rf only the directory, and leave the prunable marker and the prune result in notes/prune.txt.
  7. Attach /root/gitx4/usb, put a lock on it, leave the fact that remove refuses in notes/lock.txt, and then unlock and delete it.
  8. In notes/report.md, summarize when to use worktree, stash and clone.

Notes

Create the repository and a release branch

Create a repository with 3 commits at /root/gitx4/repo and branch off a release branch.

Start with git init -b main. This image has no global identity, so you must set git config user.email and user.name for each repository for commits to work. Fix the commit times with the two environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE.

Attach the release branch to a second place

Attach release as a working copy at /root/gitx4/rel.

It is git worktree add <경로> <브랜치>. After attaching, confirm that the .git in the new directory is a file, not a directory, using cat. That single line points to the administrative directory of the original repository.

Why you cannot open the same branch in two places

Try attaching main once more at /root/gitx4/dup, and leave the refusal message in notes/dup.txt.

If you run git worktree add /root/gitx4/dup main as it is, a line starting with fatal: comes out. Put that line into the file as it is without summarizing it, and add one line on why it blocks — the branch reference is held by the repository as one.

A hotfix while leaving unsaved changes as they are

While leaving unsaved changes in repo, attach at /root/gitx4/hot a newly created hotfix branch, and commit hotfix.txt there.

In repo, create draft.txt — do not commit it and do not stash it. In that state, make one more place with git worktree add -b <새브랜치> <경로> <기준>, and commit inside that directory. When you look at repo afterward, draft.txt is still there.

How many places there are now, in a machine-readable format

Save git worktree list --porcelain to notes/list.txt.

--porcelain is output made to be read by scripts, not people. Instead of an aligned table, worktree, HEAD and branch come out one per line. Put it into the file as it is with redirection.

If you delete only the directory, the administrative files remain

Attach /root/gitx4/tmp, then rm -rf only the directory, and leave the prunable marker and the prune result in notes/prune.txt.

After making a place with git worktree add -b tmpwork /root/gitx4/tmp, rm -rf that directory. In that state, if you look at git worktree list, prunable is attached to one line. Check what git worktree prune -v says it deleted, and whether the branch remains, and write it down. The explanation from prune goes to standard error, so to put it into a file you need 2>&1.

A locked place is refused by remove

Attach /root/gitx4/usb, put a lock on it, leave the fact that remove refuses in notes/lock.txt, and then unlock and delete it.

Lock with git worktree lock --reason '<사유>' <경로>. If you run git worktree remove on a locked place, a refusal message comes out — keep that line as it is. Then unlock and remove, and confirm that both the list entry and the directory disappear.

When to add places and when to push aside

In notes/report.md, summarize when to use worktree, stash and clone.

Separate the three methods by what each saves and what each loses. The two things you saw for yourself this time — that you cannot open the same branch in two places, and that administrative files remain even if you delete the directory — must also be included.