I clone a repository from GitHub and I create a few worktrees with:
git worktree add -path- -branch-
When I pull the changes from master
, do these worktrees update too?
I clone a repository from GitHub and I create a few worktrees with:
git worktree add -path- -branch-
When I pull the changes from master
, do these worktrees update too?
Depending on what commands you use to "pull changes", the answer is either "no' or "not exactly".
The simplest way to get updates from the remote is git fetch
. This updates the database (by adding objects from the remote if they weren't already in the local repo) and may update refs. (Usually it updates remote tracking refs specific to that remote. It can be told to directly update local branches, but this is rarely a good idea.) In any event, fetch
does not perform merges; either it leaves the local branches alone, or it updates them to match the remote and clobbers any local changes. (Again, the default is to leave them alone.)
Having fetched, you could then cd
into the worktree where you have a given local branch checked out and merge
the corresponding remote tracking ref into that branch (or do a rebase that has a similar approach).
git pull
is a shorthand for doing a fetch followed by a merge. (Well... again, that's the default. You can change the configuration so that it becomes "fetch and then do a rebase"...) So if you're in a worktree where you have a given branch checked out, you can do a git pull
and that could initiate a merge into that branch (using that worktree and its staging area).
But if you're looking for a single command that would initiate merges into all of the worktrees (which is kind of what it sounds like you're asking), you would have to probably script something like that. (And honestly, I don't recommend such a thing.)
Only the working tree[1] of the current worktree (where you are
pulling from) can get changed by git pull
. This would be relevant if
git pull
tried to do a merge, got a conflict, and then left the mess
on your floor (your worktree) for you to deal with.
The refs in your repository are trivially updated for each worktree
because all of your worktrees share the same repository (typically the
.git
directory; all “linked” worktrees simply point to the .git
directory in your “main” worktree).
man gitglossary
:[2]The tree of actual checked out files. The working tree normally contains the contents of the
HEAD
commit’s tree, plus any local changes that you have made but not yet committed.
git version 2.40.0
© 2022 - 2024 — McMap. All rights reserved.