worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Any way to auto-fetch/pull before a new worktree?

Open jdb8 opened this issue 9 months ago • 5 comments

I may have missed this in the docs/open issues, but my usecase is as follows:

  • I have a checkout of ~/my-repo which is designed to stay up to date with main
  • I want to be able to spin up lightweight worktrees based on this repo
  • Since I'm working/pulling main mostly inside worktrees, ~/my-repo can often become out of date
  • Ideally, I want to be able to have wt switch --create my-new-branch automatically run a git pull or otherwise base the new worktree on latest main, not necessarily the version that's currently checked out locally

I know I could probably create a new alias like new-worktree and have that essentially run cd ~/my-repo && git pull origin main && wt switch --create $1, but:

  • I'm wondering if this can be made to work via hooks (there doesn't seem to exist a pre-start?)
  • I'm also wondering whether the absence of this functionality means that I've misunderstood or overlooked something important with worktrees in general (new to me) or with worktrunk's philosophy (I saw another comment in an issue thread explaining that git fetch is considered a mutable operation, so I'm assuming that's probably why pull-by-default isn't a thing?)

Curious on your thoughts and if you have any tips for my desired workflow - thank you!

jdb8 avatar Feb 17 '26 22:02 jdb8

I'm wondering if this can be made to work via hooks (there doesn't seem to exist a pre-start?)

yes we could add this!

what would this do — run before checking branches etc, on any wt switch?

I think that's probably the best place; I'm generally wary of adding any network requests inside hot paths. (are you sure you would want to wait for git pull every time?)

max-sixty avatar Feb 17 '26 22:02 max-sixty

what would this do — run before checking branches etc, on any wt switch?

Yeah, I was imagining that it'd work similar to post-{start,create} to match the blocking/non-blocking behaviour of those (although not sure there'd be much benefit to a non-blocking pre-create since presumably you could just do it in post-create instead).

I think that's probably the best place; I'm generally wary of adding any network requests inside hot paths. (are you sure you would want to wait for git pull every time?)

Would also be fine if there was some way to tell wt to create a new worktree based on the most up to date SHA of origin/main, though I'm not sure if that's a limitation of git worktrees themselves? I basically just want to guarantee that new worktrees are always up to date with the remote HEAD sha, so that I don't have to think about manually fetching or pulling when spinning up new branches. A plan wt switch --create on ~/my-repo could leave me accidentally far behind main otherwise (so then would have to pull and rebuild).

jdb8 avatar Feb 18 '26 00:02 jdb8

@max-sixty finally got around to testing out this version, thanks again for adding the pre-switch hook!

I was able to get the behaviour I wanted via

[pre-switch]
pull = "git pull origin HEAD"

However, I noticed that the docs show this recipe:

[pre-switch]
# Fetch if last fetch was more than 6 hours ago
fetch = """
FETCH_HEAD="$(git rev-parse --git-common-dir)/FETCH_HEAD"
if [ "$(find "$FETCH_HEAD" -mmin +360 2>/dev/null)" ] || [ ! -f "$FETCH_HEAD" ]; then
    git fetch
fi
"""

I tried this out but I'm not sure it works the way I'd expect - the worktree created after a fresh git fetch is still based on whichever SHA is locally present in the source git repo, rather than the latest. Is there something I'm missing or does this example instead need to use git pull? Or is there another config option to have the new worktree based off origin/main instead (which would respect the latest fetched head branch)?

jdb8 avatar Mar 07 '26 00:03 jdb8

erm, yes, sorry! and thank you for spotting that @jdb8 !

@worktrunk-bot could we make that change?

max-sixty avatar Mar 07 '26 02:03 max-sixty

Good catch @jdb8! The pre-switch example was using git fetch, which only updates remote tracking refs (origin/main) without advancing the local branch. So a new worktree created afterward would still be based on the old local main.

Fixed in #1323 — changed the example to use git pull instead.

worktrunk-bot avatar Mar 07 '26 02:03 worktrunk-bot