A Hotfix Lands While Your Work Is Half Open
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
- Create a repository with 3 commits at
/root/gitx4/repoand branch off areleasebranch. - Attach
releaseas a working copy at/root/gitx4/rel. - Try attaching
mainonce more at/root/gitx4/dup, and leave the refusal message innotes/dup.txt. - While leaving unsaved changes in
repo, attach at/root/gitx4/hota newly createdhotfixbranch, and commithotfix.txtthere. - Save
git worktree list --porcelaintonotes/list.txt. - Attach
/root/gitx4/tmp, thenrm -rfonly the directory, and leave theprunablemarker and thepruneresult innotes/prune.txt. - Attach
/root/gitx4/usb, put alockon it, leave the fact thatremoverefuses innotes/lock.txt, and then unlock and delete it. - In
notes/report.md, summarize when to use worktree, stash and clone.
Notes
- This image has no global git identity. When you create a repository, you must specify
git config user.emailandgit config user.namein that repository for commits to work. - Fix the commit times with
GIT_AUTHOR_DATEandGIT_COMMITTER_DATE. This is so that grading is not shaken by timestamps, and it is also a trick used in practice to make a reproducible history. - Common mistake: in step 4, clearing away the changes in
repowithgit stashor a commit. The point of this lab is that you do not have to clear them away, so leave them as they are. - Common mistake: using
git worktree removein step 6. Here you need to see the flow in which you first remove the directory withrm -rfandprunethen clears away the leftover administrative files.
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.