git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug with git worktrees and git init

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Aug 23, 2016, 15:21 UTC
Message-ID
<f55e70fa-5244-66a0-d3a7-c6479528039d@drmicha.warpmail.net>
In-Reply-To
<CALqjkKZO_y0DNcRJjooyZ7Eso7yBMGhvZ6fE92oO4Su7JeCeng@mail.gmail.com>
Max Nordlund venit, vidit, dixit 23.08.2016 14:35:
Show 25 quoted lines
> Hi,
> 
> I've been using multiple worktrees for months without issue (it's a
> great feature, thanks), until recently when I wanted to add hooks to
> them. So, when I added a template for the hooks, everything was fine
> until I did a git reset --hard in the original repo which both applied
> those changes to the other worktrees' working tree (the files on
> disk), and made my master branch kinda lose it's connection to the
> remote/think it was a kinda bare repo.
> 
> To reproduce this:
> 
> ```
> mkdir source-repo
> cd source-repo
> git init
> touch foo
> git add foo
> git commit -m 'Add foo'
> git worktree add ../worktree # which also creates a new branch 'worktree'
> touch bar
> git add bar
> git commit -m 'Add bar'
> cd ../worktree
> git init

This is where the problem is created already: git init does not quite notice that it is in a linked worktree. It treats

source-repo/.git/worktrees/worktree

as a proper gitdir and creates the full gitdir structure in there (branches, hooks, info) which is usually shared among worktrees; also, it adds the config setting

core.worktree = <abspath>/worktree

to the main config (source-repo/.git/config) which is why "git status" thinks that bar is missing - it indeed is in that worktree.

I've cc'ed the master of worktrees.
Show 15 quoted lines
> cd ../source-repo
> git reset --hard master
> cd ../worktree
> git status # Suddenly `bar` has appear the working tree and is not tracked
> ```
> 
> I don't really now what is up with this, but I did notice that it is
> the last worktree in which git init has been run that is affected. I
> only ran git init to copy the hooks from the template, but if that is
> not something you should do in a worktree then a check would have been
> nice.
> 
> Thanks for this awesome tool, and I hope this helps
> Max Nordlund
> 
Previous: Max NordlundNext: Duy Nguyen
Message 2 of 34 in “Bug with git worktrees and git init”
  1. Max NordlundAug 23, 2016
  2. Michael J GruberAug 23, 2016
  3. Duy NguyenAug 24, 2016
  4. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 8, 2016
  5. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 8, 2016
  6. Junio C HamanoSep 8, 2016
  7. Duy NguyenSep 9, 2016
  8. 2/3 t0001: work around the bug that reads config file before repo setupNguyễn Thái Ngọc Duy, Sep 8, 2016
  9. Junio C HamanoSep 8, 2016
  10. Jeff KingSep 8, 2016
  11. Duy NguyenSep 9, 2016
  12. Jeff KingSep 9, 2016
  13. Jacob KellerSep 9, 2016
  14. 3/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 8, 2016
  15. Junio C HamanoSep 8, 2016
  16. Duy NguyenSep 9, 2016
  17. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 21, 2016
  18. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 21, 2016
  19. 2/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 21, 2016
  20. Junio C HamanoSep 21, 2016
  21. Duy NguyenSep 22, 2016
  22. Junio C HamanoSep 22, 2016
  23. 4/3 init: combine set_git_dir_init() and init_db() into oneNguyễn Thái Ngọc Duy, Sep 23, 2016
  24. Junio C HamanoSep 23, 2016
  25. Junio C HamanoSep 23, 2016
  26. Junio C HamanoSep 24, 2016
  27. Duy NguyenSep 25, 2016
  28. 1/5 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  29. 2/5 init: call set_git_dir_init() from within init_db()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  30. 3/5 init: kill set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  31. 4/5 init: do not set unnecessary core.worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  32. 5/5 init: kill git_link variableNguyễn Thái Ngọc Duy, Sep 25, 2016
  33. 3/3 init: reuse original_git_dir in set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 21, 2016
  34. Junio C HamanoSep 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.