Skip to content

[BUG] The git_watcher fails to monitor branch changes when started in a subdirectory #515

Description

@samuelfirst

Describe the bug
The git_watcher fails to monitor branch changes when Neovim is started in a subdirectory of a Git repository. While the plugin correctly identifies the branch on startup, the watcher is initialized with a relative path (e.g., .git/HEAD) based on the current cwd. If the cwd is not the project root, the watcher looks for a non-existent file and fails to trigger session switching upon branch changes.

To Reproduce
Steps to reproduce the behavior:

  1. Open a terminal and enter a Git repository.
  2. Navigate to a deep subdirectory: cd src/components/ui.
  3. Launch Neovim: nvim . (or nvim some_file.lua).
  4. In a separate terminal, change the git branch: git checkout -b new-feature.
  5. Return to Neovim. The session does not react, and no branch change is detected by auto-session.

Expected behavior
The plugin should resolve the absolute path to the Git root (toplevel) and start the watcher on the absolute path of the HEAD file (e.g., /absolute/path/to/project/.git/HEAD). This ensures the watcher works regardless of where Neovim was launched within the project tree.

Checkhealth

- ✅ OK setup() called

Config ~
- ✅ OK No config issues detected

Current Config ~
- You have old config names. You can update your config to:
  {
    auto_create = true,
    auto_restore = true,
    auto_save = true,
    enabled = true,
    git_auto_restore_on_branch_change = true,
    git_use_branch_name = true,
    log_level = "error",
    post_restore_cmds = { <function 1> },
    post_save_cmds = { "Neotree show" },
    pre_restore_cmds = { <function 2> },
    pre_save_cmds = { "Neotree close" },
    suppressed_dirs = { "~/", "~/Projects", "~/Downloads", "/" }
  }

  You can also remove any vim global config settings

Baseline (please complete the following information):

  • Result of set sessionoptions?: sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions
  • OS. e.g uname -a: Linux Ubuntu-2404-noble-amd64-base 6.8.0-106-generic #106-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 6 07:58:08 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
  • Neovim version nvim --version :
    NVIM v0.11.6 Build type: Release LuaJIT 2.1.1772619647

Additional context
The issue lies in lua/auto-session/git.lua at M.start_watcher(cwd, towatch)
I can try to fix it. I have a local fix for this. I’ve tested a version that uses git rev-parse --show-toplevel --git-dir to resolve absolute paths for both the root and the HEAD file.

    if git_root then
        require("auto-session.git").start_watcher(git_root, git_root .. "/.git/HEAD")
    else
        -- start watching for branch changes
        require("auto-session.git").start_watcher(vim.fn.getcwd(-1, -1), ".git/HEAD")
    end

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions