Git basics

Work on two branches at once (git worktree)

Open a second branch in its own folder with git worktree, so you can switch tasks without stashing or committing half-done work.

Fill these in once
  1. Open a branch in a new folder
    Terminal
    git worktree add ../myapp-hotfix hotfix

    Run this inside your project. Git checks out the branch in a folder next to your project, so your current work stays as it is.

  2. Or make a new branch in a new folder
    Terminal
    git worktree add -b hotfix ../myapp-hotfix

    Use this when the branch does not exist yet. It starts from the commit you are on now.

  3. Go to the new folder and work there
    Terminal
    cd ../myapp-hotfix

    Edit, commit and push here like in any project. Both folders share the same history, so commits show up in both.

Check it worked

  1. List every folder and the branch it has
    Terminal
    git worktree list
Undo it
  1. Remove the extra folder when you are done
    Terminal
    git worktree remove ../myapp-hotfix

    Run this from your main project folder. The branch and its commits stay. Git refuses if the folder has changes you have not committed.

  2. Clean up folders you deleted by hand
    Terminal
    git worktree prune
  1. Open a branch in a new folder
    Terminal · Ubuntu / Debian
    git worktree add ../myapp-hotfix hotfix

    Run this inside your project. Git checks out the branch in a folder next to your project, so your current work stays as it is.

  2. Or make a new branch in a new folder
    Terminal · Ubuntu / Debian
    git worktree add -b hotfix ../myapp-hotfix

    Use this when the branch does not exist yet. It starts from the commit you are on now.

  3. Go to the new folder and work there
    Terminal · Ubuntu / Debian
    cd ../myapp-hotfix

    Edit, commit and push here like in any project. Both folders share the same history, so commits show up in both.

Check it worked

  1. List every folder and the branch it has
    Terminal · Ubuntu / Debian
    git worktree list
Undo it
  1. Remove the extra folder when you are done
    Terminal · Ubuntu / Debian
    git worktree remove ../myapp-hotfix

    Run this from your main project folder. The branch and its commits stay. Git refuses if the folder has changes you have not committed.

  2. Clean up folders you deleted by hand
    Terminal · Ubuntu / Debian
    git worktree prune
  1. Open a branch in a new folder
    PowerShell
    git worktree add ../myapp-hotfix hotfix

    Run this inside your project. Git checks out the branch in a folder next to your project, so your current work stays as it is.

  2. Or make a new branch in a new folder
    PowerShell
    git worktree add -b hotfix ../myapp-hotfix

    Use this when the branch does not exist yet. It starts from the commit you are on now.

  3. Go to the new folder and work there
    PowerShell
    cd ../myapp-hotfix

    Edit, commit and push here like in any project. Both folders share the same history, so commits show up in both.

Check it worked

  1. List every folder and the branch it has
    PowerShell
    git worktree list
Undo it
  1. Remove the extra folder when you are done
    PowerShell
    git worktree remove ../myapp-hotfix

    Run this from your main project folder. The branch and its commits stay. Git refuses if the folder has changes you have not committed.

  2. Clean up folders you deleted by hand
    PowerShell
    git worktree prune

New to Branch? I explain it in plain English on Layman Terms.