Skip to content

Instantly share code, notes, and snippets.

@treramey
Forked from GeorgeLyon/Git Worktree Overview.md
Created September 25, 2024 15:08
Show Gist options
  • Save treramey/919f53e9e6f80a26761e1e9f54006e8a to your computer and use it in GitHub Desktop.
Save treramey/919f53e9e6f80a26761e1e9f54006e8a to your computer and use it in GitHub Desktop.

Revisions

  1. @GeorgeLyon GeorgeLyon revised this gist Oct 24, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions Git Worktree Overview.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # What is it?
    # `git worktree`

    ## What is it?

    [Git worktree](https://git-scm.com/docs/git-worktree) it a git feature which allows you to checkout a single repository into multiple locations on your filesystem.
    It has a few rough edges, but if you follow a few simple rules it can be make context switching much easier than git's other mechanisms, such as stashing or switching branches.
    @@ -10,7 +12,7 @@ My folder structure will usually look something like this:
    ` hotfix_branch_1/` ← A quick hotfix I need to make


    # Creating Worktrees
    ## Creating Worktrees
    1. Create the top-level folder: `mkdir MyRepo`
    1. Checkout the repository's master branch as a normal checkout: `cd MyRepo; git clone <repo url> master`
    1. Add a branch: `cd master; git worktree add -b <branch_name> ../<folder_name> HEAD`
    @@ -19,7 +21,7 @@ My folder structure will usually look something like this:
    1. When you need to create a new branch, make sure to create it from the branch you want to base it on (often, this is `master`)

    # Removing Worktrees
    1. `git worktree rm <path_to_worktree>` from a directory containing your worktree
    1. `git worktree remove <path_to_worktree>` from a directory containing your worktree
    1. (Optional) Delete the local branch with `git branch -D branch_name`
    1. (Optional) Delete the remote branch with `git push <remote> :<branch_name>`

  2. @GeorgeLyon GeorgeLyon revised this gist Aug 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Git Worktree Overview.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # What is it?

    [Git worktree]() it a git feature which allows you to checkout a single repository into multiple locations on your filesystem.
    [Git worktree](https://git-scm.com/docs/git-worktree) it a git feature which allows you to checkout a single repository into multiple locations on your filesystem.
    It has a few rough edges, but if you follow a few simple rules it can be make context switching much easier than git's other mechanisms, such as stashing or switching branches.
    My folder structure will usually look something like this:

  3. @GeorgeLyon GeorgeLyon created this gist Aug 23, 2019.
    29 changes: 29 additions & 0 deletions Git Worktree Overview.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # What is it?

    [Git worktree]() it a git feature which allows you to checkout a single repository into multiple locations on your filesystem.
    It has a few rough edges, but if you follow a few simple rules it can be make context switching much easier than git's other mechanisms, such as stashing or switching branches.
    My folder structure will usually look something like this:

    `MyRepo/`
    ` master/` ← The original checkout, using something like `git clone <repo url> master`
    ` feature_branch_1/` ← A feature branch
    ` hotfix_branch_1/` ← A quick hotfix I need to make


    # Creating Worktrees
    1. Create the top-level folder: `mkdir MyRepo`
    1. Checkout the repository's master branch as a normal checkout: `cd MyRepo; git clone <repo url> master`
    1. Add a branch: `cd master; git worktree add -b <branch_name> ../<folder_name> HEAD`
    - `branch_name` and `folder_name` should likely be the same to avoid confusion (in larger projects, I would prepend `branch_name` with my username)
    1. Switch to that branch: `cd ../my_branch`
    1. When you need to create a new branch, make sure to create it from the branch you want to base it on (often, this is `master`)

    # Removing Worktrees
    1. `git worktree rm <path_to_worktree>` from a directory containing your worktree
    1. (Optional) Delete the local branch with `git branch -D branch_name`
    1. (Optional) Delete the remote branch with `git push <remote> :<branch_name>`

    # Gotchas
    - In a worktree, the `.git` folder is just a file, so scripts modifying repo settings like git hooks may fail. The git hooks in the original repository apply to its worktrees.
    - You can actually checkout a worktree _inside_ of the repository by leaving out the `../` before folder name. This is not recommended.
    - You can not have the same branch checked out in multiple worktrees