Skip to main content

One post tagged with "git"

View All Tags

· 2 min read

image

git worktree

worktree 指令可以將內容複製一份出來至指定位置,當需要同時處理多個 branch 的時候很方便,可以避免頻繁 switch branch 。

Documentation: https://git-scm.com/docs/git-worktree

Example

gwt alias 參考: ohmyzsh git alias

使用 worktree

# 列出 worktree
gwtls
# {REPO} 3dd0c75 [master]

# 建立 worktree
gwta -b my_wtb ./my_wt # or `-B` to reset
# Preparing worktree (new branch 'my_wtb')
# HEAD is now at 3dd0c75 {MSG}

gwtls
# {REPO} 3dd0c75 [master]
# {REPO}/my_wt 3dd0c75 [my_wtb]
ls
# my_wt
ls .git/worktrees
# my_wt

切換目錄後就會發現 branch 會自動變換

gb --show-current
# master

cd ./my_wt
gb --show-current
# my_wtb

清除 worktree

# 移除 worktree
gwtrm ./my_wt
gwtls
# {REPO} 3dd0c75 [master]

# branch 還在
gb -v | \cat
# * master 3dd0c75 {MSG}
# my_wtb 3dd0c75 {MSG}

gb -d my_wtb # or `-D` if not merged
# Deleted branch my_wtb (was 3dd0c75).

Note

git worktree prune -v