Search code examples
gitgit-worktree

git pull doesn't pull latest changes when using git worktree


I like git worktree, but keep getting into situations where the local working directory is not the same as the remote branch, even though git pull says "Already up to date".

Here's a little MRE I did:

  • Created a remote repo in the GitLab web interface.
  • Cloned it using git clone --bare <url>
  • Added the main branch locally using git worktree add main
  • Added a new branch using git worktree add new-branch, cded into it and did a git push.
  • Using the GitLab web interface, I added a file main-branch-change-1 directly in the main branch, then rebased new-branch on top of the new main, then added a second file feature-branch-change-1 in new-branch.
  • git pull on the local new-branch says "Already up to date", while commit hash and git log clearly shows the local is not up to date. The file main-branch-change-1 is present, but not feature-branch-change-1.
  • git fetch --all in the bare repo followed by git pull in new-branch changes nothing.
  • git pull in the main branch followed by git pull in new-branch changes nothing.

What do I have to do to sync these changes?

Some screenshots:

GitLab graph remote graph

Content of remote new-branch remote new-branch content

Locally local state


Solution

  • As torek pointed out in a comment, the issue was caused by cloning with --bare. That's something I picked up from "ThePrimeagen" here (2 mins in). But rather than having a full (non-bare) repo at the root, which I think I don't want, this command seems to fix it all:

    git config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
    

    Thanks TamaMcGlinn.

    I still have to do git branch --set-upstream-to=origin/new-branch new-branch from the new-branch folder, which failed previously with "error: the requested upstream branch 'origin/main' does not exist", but succeeds after the above command.