From Stash to Flash: How Git Worktree Changes the Game

Photo by Praveen Thirumurugan on UnsplashEver been juggling a bunch of branches or changes in Git and wished for a smoother ride? Say hello to Git Worktree.Git Worktree lets you check out multiple branches at once within a single repository. This means…


This content originally appeared on Level Up Coding - Medium and was authored by Deepak Surya

Photo by Praveen Thirumurugan on Unsplash

Ever been juggling a bunch of branches or changes in Git and wished for a smoother ride? Say hello to Git Worktree.

Git Worktree lets you check out multiple branches at once within a single repository. This means you can tinker on different branches simultaneously without switching contexts or stashing your changes.

Here’s how:

git worktree add       # Create a new worktree
git worktree list # List all worktrees
git worktree remove # Remove a worktree

Comparing Git Worktree and Git Stash

True Parallel Development

Git Worktree

  • Lets you have multiple branches checked out at the same time in different directories.
  • This reduces the need for constant context switching.
  • Cleaner mental model as your file system mirrors your Git branching strategy.
project/
├── main/
├── feature-a/
└── bugfix-123/

Git Stash

  • You have to stash your changes, switch branches, and then reapply them, which can be a bit cumbersome.
  • Doesn’t allow true parallel work on multiple branches.

Workflow & Time Efficiency

Git Worktree

  • Perfect for tasks that require working on multiple features or bug fixes concurrently.
  • You can hop between branches without breaking a sweat. (Smooth Operator by Sade)
  • Makes code reviews a breeze by keeping the PR branch right alongside the main branch.

Git Stash

  • Great for quick switches, but you’ve got to stash and pop changes
  • Can throw a spanner in the works. Kills the momentum.

Metrics

Imagine this: you’re knee-deep in feature A but need to fix a bug in feature B.

Git Worktree

  • Checking out a new worktree for the bug fix and switching back to feature A after fixing it takes around 10 seconds. Seriously!
  • Daily time: 10 seconds * 5 = 50 seconds
  • Monthly time (20 working days): 50 seconds * 20 = 16.67 minutes

Git Stash

  • Stashing changes, switching branches, and popping the stash… you know how it goes.
  • Time to stash, switch branches, and pop stash: ~1 minute
  • Daily time: 1 minute * 5 = 5 minutes
  • Monthly time (20 working days): 5 minutes * 20 = 100 minutes

Here’s what a worktree workflow would look like

# Create a new worktree
git worktree add ../feature-branch feature-branch

# Switch to the new worktree directory
cd ../feature-branch

# Work on your changes, commit as usual
git add .
git commit -m "Implement new feature"

# Return to the main worktree
cd -

# List all worktrees
git worktree list

# Remove a worktree when no longer needed
git worktree remove ../feature-branch

And here’s what a stash workflow looks like

# Stash current changes
git stash save "Work in progress on feature X"

# Switch branch or context
git checkout another-branch

# Do some work, then return to original branch
git checkout original-branch

# Apply the stashed changes
git stash apply

# Or pop the stash (apply and remove from stash list)
git stash pop

# List all stashes
git stash list

# Clear all stashes
git stash clear

Conclusion

Git Worktree can save you a ton of time, especially when you’re juggling multiple branches and frequent context switches. While Git Stash is useful for quick changes, Git Worktree offers a more efficient workflow for handling concurrent tasks.

So, the next time you’re bouncing between branches, give Git Worktree a try. It might just be the game-changer you need to boost your productivity.

Happy coding!


From Stash to Flash: How Git Worktree Changes the Game was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Deepak Surya


Print Share Comment Cite Upload Translate Updates
APA

Deepak Surya | Sciencx (2024-08-09T12:57:09+00:00) From Stash to Flash: How Git Worktree Changes the Game. Retrieved from https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/

MLA
" » From Stash to Flash: How Git Worktree Changes the Game." Deepak Surya | Sciencx - Friday August 9, 2024, https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/
HARVARD
Deepak Surya | Sciencx Friday August 9, 2024 » From Stash to Flash: How Git Worktree Changes the Game., viewed ,<https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/>
VANCOUVER
Deepak Surya | Sciencx - » From Stash to Flash: How Git Worktree Changes the Game. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/
CHICAGO
" » From Stash to Flash: How Git Worktree Changes the Game." Deepak Surya | Sciencx - Accessed . https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/
IEEE
" » From Stash to Flash: How Git Worktree Changes the Game." Deepak Surya | Sciencx [Online]. Available: https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/. [Accessed: ]
rf:citation
» From Stash to Flash: How Git Worktree Changes the Game | Deepak Surya | Sciencx | https://www.scien.cx/2024/08/09/from-stash-to-flash-how-git-worktree-changes-the-game/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.