Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for git workree, quick intro ( simplify your git workflow! )
zahaar
zahaar

Posted on

     

git workree, quick intro ( simplify your git workflow! )

Here is a fast example of how to usegit worktree, it's extremely helpful and at the same time extremely unpopulargit feature.

Surprisingly it has been around for ages, but no one seems to know about, yet fall in love on first try!!!

I, personally discovered it from one of Guido's, tweets:

Guido van Rossum profile image
Guido van Rossum
@gvanrossum
twitter logo
Why did I not know before about "git worktree"? :-(git-scm.com/docs/git-workt…
20:27 PM - 07 Apr 2021
Twitter reply actionTwitter retweet actionTwitter like action

Problematic

to present a solution, we have to understand the problem it tries to solve, before jumping to a solution. Here is a simple scenario:

You are trying to work on your own feature for some time. Meanwhile, a coworker writes to you, with a request to review his work, that he has done on aseparate branch of the same repo.

Most likely, your next steps would be:

1 - save your work instash with command

git stash save <name>
Enter fullscreen modeExit fullscreen mode

2 -checkout to the branch that coworker wants you to review

git checkout feature-coworkers-branch
Enter fullscreen modeExit fullscreen mode

3 - there might be a need to install the dependencies, with

pipinstall-r req.txt// or yarn
Enter fullscreen modeExit fullscreen mode

4 - after browsing though the branch, and finishing with the review, you would want to revert back to the branch that you have worker on

git checkout feature-your-old-branch
Enter fullscreen modeExit fullscreen mode

5 -HIDERANCE 1: you have to find &&unstash your code. You can run,git stash pop/apply, in case the last stash that you have saved, is the one you need

you might need to run:

git stash list
Enter fullscreen modeExit fullscreen mode
git stash pop stash{0} // or git stash apply
Enter fullscreen modeExit fullscreen mode

please note that git stash apply won't delete the stashed item from the stash list

to look up for just the rightstash, saving stash with helps a lot. But still sometimes, in a heat of the moment, while having multiple work processes saved in stash, we get lost, apply the the wrongstash and then another one...

6 -HINDERANCE 2: you might encounter a problem of dependency conflict. The dependencies that were installed infeature-coworkers-branch, might hinder your work, due to requiring different versions, demanding different configuration etc... The solution would be to delete/reinstall them, but that's once again an annoying process.

Note: A different workflow, might be cloning a repo in a different folder, doing the review there and discarding it afterwards. But what about the time it would take to install all the dependencies?

All in all, that's an uncomfortable situation to be in. We have encountered it frequently, it consumes our time and mental power.

Luckily,git have a better way

Solution

worktree is wonderful alternative, let's try to follow the same steps using this feature.

1 - While working on your feature, useworktree command to create a new folder/directory of the same repo, and specify which branch you want to start with

git worktree add ../repo_test feature-42-update-mat-view# git worktree add <path> <optional:starting-branch-name>
Enter fullscreen modeExit fullscreen mode

and BOOM, now you are in a separate folder/directory, on afeature-42-update-mat-view branch.

2 - Install all the dependencies necessary to check the work

pipinstall-r req.txt// or yarn
Enter fullscreen modeExit fullscreen mode

3 - Write to your colleague, removerm -rdf repo_test /git worktree remove ../repo_test ( or keep it there ), and get back to folder containing your own work progress.

If a working tree is deleted without usinggit worktree remove, then its associated administrative files, which reside in the repository (see "DETAILS" below), will eventually be removed automatically ...

That's it!!!

No need tostash the code, revert installed dependencies and dig throughstash list.

This way is not only faster, but also, much less error prone!
Having errorlessgit workflow is especially important to keep the concentration on your work.

Tips && References

1 - Officialgitdocumentation is very good

2 - If you create a worktree with intention to make some experimental changes, pass-d flag.

git worktree add-d ../repo_test
Enter fullscreen modeExit fullscreen mode

this quote fromgit worktreedocs

For instance,git worktree add -d <path> creates a new working tree with a detached HEAD at the same commit as the current branch.

3 - Another good workflow would be is to start off by cloning abare repo. Then useworktree add ... and have a list of yourworktrees in abare repo.

git clone--bare url...
Enter fullscreen modeExit fullscreen mode

git--bare docs

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

“I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.”
  • Location
    Berlin, Germany
  • Work
    Software Developer at NBT
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp