Git Worktrees and Secret Management
Git Worktrees
Branches
git branch switch <branchname>
Creating a New Worktree
- Create a new worktree for an existing branch at the specified path:
git worktree add <WORKTREE-PATH> <BRANCH> - Create a new branch and its worktree at the specified path:
git worktree add -b <BRANCH> <WORKTREE-PATH>
Listing Worktrees
- List all current worktrees, including the main worktree:
git worktree list - List worktrees in a machine-readable format:
git worktree list --porcelain
Deleting a Worktree
- Delete a worktree:
git worktree remove <WORKTREE-PATH> - Clean up worktree metadata:
git worktree prune
Other Worktree Commands
- Lock a worktree to prevent it from being pruned:
git worktree lock [--reason <string>] <worktree> - Move a worktree to a new location:
Worktrees allow you to check out multiple branches simultaneously in separate directories, avoiding the need to constantly switch between branches or usegit worktree move <worktree> <new-path>git stash.
Merge Worktrees
Identify the worktrees you want to merge: Determine which branches/worktrees you want to combine into a single branch. You can list all your current worktrees using the command
git worktree list.Checkout the target branch: Switch to the branch you want to merge the other worktree(s) into using
git switch <target-branch>. This will be the branch that will contain the final merged changes.Merge the worktree branches: Use the standard
git mergecommand to incorporate the changes from the other worktree branches into the target branch. For example, to merge thefeature-xbranch into themainbranch:git switch main git merge feature-xThis will merge the
feature-xbranch into the currently checked outmainbranch.Resolve any conflicts: If there are any conflicts between the branches, Git will pause the merge process. You'll need to manually resolve the conflicts, stage the resolved files, and then continue the merge:
git merge --continueAlternatively, you can abort the merge and start over if needed:
git merge --abortClean up the worktrees: Once the merge is complete, you can optionally delete the now-merged worktrees using
git worktree remove <worktree-path>. This will remove the linked worktree directory while keeping the main repository intact.
Secrets in Git Repository
Scan for Problems
- Gitleaks (Golang)
- git-secrets (Shell)
- Whispers (Python/Docker)
- Gittyleaks (Python)
- Detect-Secrets (Python)
Remove Secrets Securely
- Rewrite history:
git filter-repo --replace-text replacements.txt --replace-refs delete-no-add - Force push all changes to remote:
git push origin --force --allgit push origin --force --tags
