You need to use Git Worktrees
This YouTube video on “git worktrees” explains how to manage multiple branches and code changes simultaneously without needing to constantly commit, stash, or clone repositories. Here are the key points:
-
Problem: Working on a complex code change with many uncommitted changes makes it difficult to switch context (e.g., for hotfixes, code reviews, or testing different versions).
-
Solution:
git worktreeThis built-in Git command creates multiple working directories (“worktrees”) for a single repository. Each worktree is a separate checkout of the repo, allowing you to work on different branches concurrently. It’s like having multiple clones but with integrated management. -
Creating a Worktree: Use
git worktree add <new_path> <branch_name>. This creates a new directory with a checkout of the specified branch. If the branch doesn’t exist, add-bto create it. -
Independent Changes: Changes made in one worktree don’t affect others. Each worktree maintains its own state, including changes and node_modules.
-
Branch Management: Git tracks which worktree is checked out for each branch. You can’t check out a branch already open in another worktree without first switching that other worktree. Branches can be shared among worktrees.
-
Rebasing: You can easily rebase changes between worktrees, keeping them synchronized.
-
Removing a Worktree: Use
rm -rf <worktree_path>to delete the directory.git worktree prunecleans up references to removed worktrees. -
Efficiency:
git worktreesprovides a streamlined way to manage multiple development tasks, avoiding the complexities of stashing or managing multiple clones.