[awesome-claude-code] Git Worktree ์ปค๋งจ๋
์ด ์ปค๋งจ๋๋ GitHub CLI์ git worktree๋ฅผ ํ์ฉํ์ฌ ์ด๋ฆฐ PR ์ ์ฒด ๋๋ ์ ๋ธ๋์น์ ๋ํ worktree๋ฅผ ์๋ ์์ฑํ๋ ๋๊ตฌ์ ๋๋ค.
๐ ์ฐธ๊ณ : ์ด ๋ฌธ์๋ awesome Claude Code ๋ฌธ์์ ์๋ฌธ + ๋ ํผ๋ฐ์ค์ ๋๋ค.
- ์๋ฌธ ๋งํฌ๋ฅผ ๋ฐฉ๋ฌธํ์ ์ ๋ณต์ฌํด์ ์ฌ์ฉํ์ ๋ ๋ฉ๋๋ค
- ์ด ํ์ผ์
.claude/skills/ํ์์ ๋ณต์ฌํ์ฌ ์คํฌ๋ก ๋ฑ๋กํ ์๋ ์์ต๋๋ค(26.03.15 ๊ธฐ์ค)
Git Worktree ์ปค๋งจ๋
์ด๋ฆฐ PR ์ ์ฒด์ ๋ํ Worktree ์์ฑ
์ด ์ปค๋งจ๋๋ GitHub CLI๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฆฐ ํ ๋ฆฌํ์คํธ๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์จ ๋ค์, ๊ฐ PR์ ๋ธ๋์น์ ๋ํด ./tree/<BRANCH_NAME> ๋๋ ํ ๋ฆฌ์ git worktree๋ฅผ ์์ฑํฉ๋๋ค.
# GitHub CLI๊ฐ ์ค์น๋์ด ์๊ณ ์ธ์ฆ๋์ด ์๋์ง ํ์ธ
gh auth status || (echo "Please run 'gh auth login' first" && exit 1)
# tree ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด ์์ฑ
mkdir -p ./tree
# ์ด๋ฆฐ PR์ ๋ชจ๋ ๋์ดํ๊ณ ๊ฐ ๋ธ๋์น์ ๋ํด worktree ์์ฑ
gh pr list --json headRefName --jq '.[].headRefName' | while read branch; do
# ์ฌ๋์๊ฐ ํฌํจ๋ ๋ธ๋์น ์ด๋ฆ ์ฒ๋ฆฌ (์: "feature/foo")
branch_path="./tree/${branch}"
# ์ฌ๋์๊ฐ ์๋ ๋ธ๋์น์ ๊ฒฝ์ฐ, ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ ์์ฑ
if [[ "$branch" == */* ]]; then
dir_path=$(dirname "$branch_path")
mkdir -p "$dir_path"
fi
# worktree๊ฐ ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ
if [ ! -d "$branch_path" ]; then
echo "Creating worktree for $branch"
git worktree add "$branch_path" "$branch"
else
echo "Worktree for $branch already exists"
fi
done
# ์์ฑ๋ ๋ชจ๋ worktree ํ์
echo "\nWorktree list:"
git worktree list
์ถ๋ ฅ ์์
Creating worktree for fix-bug-123
HEAD is now at a1b2c3d Fix bug 123
Creating worktree for feature/new-feature
HEAD is now at e4f5g6h Add new feature
Worktree for documentation-update already exists
Worktree list:
/path/to/repo abc1234 [main]
/path/to/repo/tree/fix-bug-123 a1b2c3d [fix-bug-123]
/path/to/repo/tree/feature/new-feature e4f5g6h [feature/new-feature]
/path/to/repo/tree/documentation-update d5e6f7g [documentation-update]
์ค๋๋ Worktree ์ ๋ฆฌ (์ ํ ์ฌํญ)
๋ ์ด์ ์กด์ฌํ์ง ์๋ ๋ธ๋์น์ ์ค๋๋ worktree๋ฅผ ์ ๊ฑฐํ๋ ค๋ฉด ๋ค์์ ์ถ๊ฐํ ์ ์์ต๋๋ค:
# ํ์ฌ ๋ธ๋์น ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
current_branches=$(git branch -a | grep -v HEAD | grep -v main | sed 's/^[ *]*//' | sed 's|remotes/origin/||' | sort | uniq)
# ๊ธฐ์กด worktree ๊ฐ์ ธ์ค๊ธฐ (๋ฉ์ธ worktree ์ ์ธ)
worktree_paths=$(git worktree list | tail -n +2 | awk '{print $1}')
for path in $worktree_paths; do
# ๊ฒฝ๋ก์์ ๋ธ๋์น ์ด๋ฆ ์ถ์ถ
branch_name=$(basename "$path")
# ํน์ ์ผ์ด์ค ๊ฑด๋๋ฐ๊ธฐ
if [[ "$branch_name" == "main" ]]; then
continue
fi
# ๋ธ๋์น๊ฐ ์ฌ์ ํ ์กด์ฌํ๋์ง ํ์ธ
if ! echo "$current_branches" | grep -q "^$branch_name$"; then
echo "Removing stale worktree for deleted branch: $branch_name"
git worktree remove --force "$path"
fi
done
์ ๋ธ๋์น ๋ฐ Worktree ์์ฑ
์ด ์ธํฐ๋ํฐ๋ธ ์ปค๋งจ๋๋ ์ git ๋ธ๋์น๋ฅผ ์์ฑํ๊ณ ํด๋น ๋ธ๋์น์ ๋ํ worktree๋ฅผ ์ค์ ํฉ๋๋ค:
#!/bin/bash
# git ์ ์ฅ์ ์์ ์๋์ง ํ์ธ
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: Not in a git repository"
exit 1
fi
# ์ ์ฅ์ ๋ฃจํธ ๊ฐ์ ธ์ค๊ธฐ
repo_root=$(git rev-parse --show-toplevel)
# ๋ธ๋์น ์ด๋ฆ ์
๋ ฅ ํ๋กฌํํธ
read -p "Enter new branch name: " branch_name
# ๋ธ๋์น ์ด๋ฆ ๊ฒ์ฆ (๊ธฐ๋ณธ ๊ฒ์ฆ)
if [[ -z "$branch_name" ]]; then
echo "Error: Branch name cannot be empty"
exit 1
fi
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
echo "Warning: Branch '$branch_name' already exists"
read -p "Do you want to use the existing branch? (y/n): " use_existing
if [[ "$use_existing" != "y" ]]; then
exit 1
fi
fi
# ๋ธ๋์น ๋๋ ํ ๋ฆฌ ์์ฑ
branch_path="$repo_root/tree/$branch_name"
# ์ฌ๋์๊ฐ ํฌํจ๋ ๋ธ๋์น ์ด๋ฆ ์ฒ๋ฆฌ (์: "feature/foo")
if [[ "$branch_name" == */* ]]; then
dir_path=$(dirname "$branch_path")
mkdir -p "$dir_path"
fi
# ๋ถ๋ชจ ๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ๋์ง ํ์ธ
mkdir -p "$(dirname "$branch_path")"
# worktree๊ฐ ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ
if [ -d "$branch_path" ]; then
echo "Error: Worktree directory already exists: $branch_path"
exit 1
fi
# ๋ธ๋์น ๋ฐ worktree ์์ฑ
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
# ๋ธ๋์น๊ฐ ์กด์ฌํ๋ฉด, worktree ์์ฑ
echo "Creating worktree for existing branch '$branch_name'..."
git worktree add "$branch_path" "$branch_name"
else
# ์ ๋ธ๋์น ๋ฐ worktree ์์ฑ
echo "Creating new branch '$branch_name' and worktree..."
git worktree add -b "$branch_name" "$branch_path"
fi
echo "Success! New worktree created at: $branch_path"
echo "To start working on this branch, run: cd $branch_path"
์ฌ์ฉ ์์
$ ./create-branch-worktree.sh
Enter new branch name: feature/user-authentication
Creating new branch 'feature/user-authentication' and worktree...
Preparing worktree (creating new branch 'feature/user-authentication')
HEAD is now at abc1234 Previous commit message
Success! New worktree created at: /path/to/repo/tree/feature/user-authentication
To start working on this branch, run: cd /path/to/repo/tree/feature/user-authentication
๋ค๋ฅธ ๋ฒ ์ด์ค์์ ์ ๋ธ๋์น ์์ฑ
ํ์ฌ HEAD๊ฐ ์๋ ๋ค๋ฅธ ๋ฒ ์ด์ค์์ ๋ธ๋์น๋ฅผ ์์ํ๋ ค๋ฉด, ์คํฌ๋ฆฝํธ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ ์ ์์ต๋๋ค:
read -p "Enter new branch name: " branch_name
read -p "Enter base branch/commit (default: HEAD): " base_commit
base_commit=${base_commit:-HEAD}
# ์ง์ ๋ ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ worktree ์์ฑ
git worktree add -b "$branch_name" "$branch_path" "$base_commit"
์ด๋ ๊ฒ ํ๋ฉด ์ ๋ธ๋์น์ ์์์ ์ผ๋ก ์ปค๋ฐ, ํ๊ทธ ๋๋ ๋ธ๋์น ์ด๋ฆ์ ์ง์ ํ ์ ์์ต๋๋ค.