Search code examples
gitjenkinsgit-configgit-fetchgit-worktree

Running <git worktree add> checks out older commit hash


Im running a jenkins pipeline, where:

[Pipeline] { (Prepare)
[Pipeline] sh
+ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

[Pipeline] sh
+ git fetch origin master
From https://github.com/<company>/ltlwrk-python
 * branch            master     -> FETCH_HEAD
   94b92f5..22c1a07  master     -> origin/master

[Pipeline] sh
+ git fetch origin test
From https://github.com/<company>/ltlwrk-python
 * branch            test       -> FETCH_HEAD
   223cb5f..7b13d20  test       -> origin/test

[Pipeline] sh
+ git fetch origin production
From https://github.com/<company>/ltlwrk-python
 * branch            production -> FETCH_HEAD
   54aad77..2e8a174  production -> origin/production

I run a config remote and fetch 3 branches, as seen. After I call on git worktree, because I want to build a single container of the 3 env., the problem is, it doesnt checks out the latest commit and when the image is build it has the old code in it:(master: 94b92f5 - old hash. 22c1a07 - new commit hash which I want to build upon)

git worktree add ./all/src_master master
git worktree add ./all/src_test test
git worktree add ./all/src_production production

I'm might be using the wrong git config, fetch, worktree command, i cant figure out which one, anyone knows what am I missing?


Solution

  • From the log, we can see that branches test, master, production are not updated. Instead, their remote tracking branches origin/test, origin/master, origin/production are updated. So use the updated remote tracking branches to create worktrees.

    git worktree add ./all/src_master origin/master
    git worktree add ./all/src_test origin/test
    git worktree add ./all/src_production origin/production
    

    You could checkout test first and then update it. But it's not necessary in your case. Besides, Git does not allow to create more than one worktree from the same branch. The second try would give an error fatal: 'master' is already checked out at xxxx, if the first worktree has not been removed. But it allows to create multiple worktrees from the same commit or ref like origin/master or even refs/heads/master.