Search code examples
gitgit-branchgit-commitgit-worktree

git worktree add [from specific commit hash]


so I have a commit hash from earlier in a project. How can I create a new worktree and have it's branch start from this specific commit?


Solution

  • Use the command described in the git worktree documentation:

    git worktree add [--detach] [-b <new-branch>] <path> [<commit-ish>]

    which in this case might be, e.g.:

    $ cd gitrepo
    $ git worktree add -b workbranch ../gitrepo.workbranch d16c37964
    Preparing ../gitrepo.workbranch (identifier gitrepo.workbranch)
    HEAD is now at d16c37964 Merge branch 'jk/attributes-path-doc'
    

    after which:

    $ cd ../gitrepo.workbranch
    $ git status
    On branch workbranch
    nothing to commit, working tree clean
    $ git rev-parse HEAD
    d16c37964c284fa599251caafb51128c0df925a9