DEV Community

Cover image for πŸš€ Simplify your git workflow with Git Worktree!
Salim Khengui
Salim Khengui

Posted on

πŸš€ Simplify your git workflow with Git Worktree!

Ever felt stuck juggling multiple branches in a single repository ?

If you have then the git command you used to manage and work on these different branches simultaneously was most likely git stash . Unfortunately though, the git stash command can be complicated to keep up with the more branches you have. Sur, if your changes were added to the staging area, using the git stash command will automatically map the changes to the branch so won’t need to remember which change was stashed on which branch, but, what if you forgot to add the changes to the staging area, then the fear is real, the fear of losing your previous changes will eat you alive (i’m just joking, or am i ?). So, isn’t there a better solution ? well there is, git is again here to help. What is this solution you might ask. may i present git worktree

What is git worktree . Well its a git command, obviously, that based on the official git website, allows you to manage multiple working trees that are attached to the same git repository.

You might ask what does that mean. Well, simply put, it allows you to switch to a branch from your repository juste like git switch but unlike it, instead of switching to the branch in your default local repository, it creates a new folder that contains the new branch on a path that you specify, leaving your default local repository untouched.

Still not clear enough. Well, lets take on the example we talked about in the first paragraph or this poste to showcase how git worktree solves the issue.

Imagine that you are an experienced full-stack developper (feels good huh) working on a branch called feat/top-dev, and you have changes that are not yet committed, during this serene coding session, your noob colleague puts on a merge request (or pull request for our Github friends) for his branch called feat/noob-dev and asks you to review it urgently, and like any good self respecting reviewer you want to pull his code to test it locally, but you did not finish you work on your branch yet, so what do you do.

  • Using git stash
    • We add the current changes to the staging area with git add
    • We stash the changes using git stash
    • We switch the review branch using git switch feat/noob-dev
    • We do our review and testing
    • Once done, you switch back to your branch using git switch feat/top-dev
    • And you bring back your previously stashed changes using git stash pop
  • Using git worktree
    • We run the following command git worktree add ../review feat/noob-dev , this will create a new folder outside of our local repository called review and checkout the feat/noob-dev branch into it
    • We open the review folder in a new window or our preferred IDE (or text editor)
    • We do our review and testing
    • Once done, we run the following command git worktree remove ../review

Now, notice that using the git worktree command, our branch and its changes are never touched, meaning you can go back to your work after the review without manipulating any stash or any file, neat right ?

Now, obviously this a simple example, but, this can have many use cases, code reviews, debugging … etc, all without disturbing your primary environment.

If you want to learn more about this git command and its use cases, i would highly recommend you read the official documentation obviously (duh) and try using the command yourself, just be sure to share your favorite use case, and any other tips in the comments for others to learn from your experience.

Top comments (0)