Git pull, merge changes is worktrees?
Asked Answered
P

2

7

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?

Pontificate answered 7/2, 2019 at 21:26 Comment(0)
H
6

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.)

Halfcock answered 7/2, 2019 at 21:50 Comment(0)
U
0

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).

Notes

  1. See 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.

  1. git version 2.40.0
Unitarian answered 15/4, 2023 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.