Ali Gamal
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    <style> :root { --neon-cyan: #0ff; --neon-magenta: #f0f; --neon-yellow: #ff0; --neon-green: #0f0; --bg-color: #0d0d0d; --text-color: #e0e0e0; } body { background-color: var(--bg-color) !important; color: var(--text-color) !important; font-family: 'Fira Code', 'Courier New', Courier, monospace !important; } #doc.markdown-body { background-color: var(--bg-color) !important; color: var(--text-color) !important; } h1, h2, h3 { color: var(--neon-cyan) !important; text-shadow: 0 0 5px var(--neon-cyan), 0 0 10px var(--neon-cyan) !important; border-bottom: 1px solid var(--neon-magenta) !important; } a { color: var(--neon-magenta) !important; text-decoration: none !important; } a:hover { text-shadow: 0 0 5px var(--neon-magenta) !important; } code { background-color: #1a1a1a !important; color: var(--neon-yellow) !important; border: 1px solid var(--neon-yellow) !important; } pre { background-color: #121212 !important; border-left: 5px solid var(--neon-green) !important; box-shadow: 0 0 10px rgba(0, 255, 0, 0.2) !important; } blockquote { border-left: 5px solid var(--neon-cyan) !important; background-color: rgba(0, 255, 255, 0.05) !important; color: var(--neon-cyan) !important; } strong { color: var(--neon-magenta) !important; } hr { border-top: 1px solid var(--neon-cyan) !important; opacity: 0.5; } table { border-collapse: collapse !important; width: 100% !important; background-color:var(--bg-color) !important; border: 1px solid var(--neon-cyan) !important; } th { background-color:var(--bg-color) !important; color: var(--neon-cyan) !important; } td, th { border: 1px solid var(--neon-cyan) !important; background-color:var(--bg-color) !important; padding: 8px !important; } /* Scrollbar */ ::-webkit-scrollbar { width: 10px; } ::-webkit-scrollbar-track { background: #000; } ::-webkit-scrollbar-thumb { background: var(--neon-cyan); border-radius: 5px; } </style> # Git Worktree Complete Guide ## What is Git Worktree? Git worktree lets you check out **multiple branches** from the same repository into **separate directories** simultaneously. Each worktree is an independent working copy with its own branch, staging area, and working files. ### Why Use It? - Work on a **bugfix** while keeping your feature branch intact - Review someone **else's** PR without stashing your changes - Run two branches side-by-side for comparison - Build/test different branches in parallel - No need to stash or commit incomplete work to switch branches --- ## How It Works ``` .git/ ← Single shared repository (objects, refs, config) ├── worktrees/ ← Metadata for each linked worktree │ ├── branch-a/ │ └── branch-b/ ├── HEAD ← Points to the MAIN worktree's branch ├── refs/ └── config ../project-main/ ← Main worktree (on branch: main) ../project-feature/ ← Linked worktree (on branch: feature-x) ../project-hotfix/ ← Linked worktree (on branch: hotfix-y) ``` All worktrees share the same `.git/objects` database — no duplication of history. --- ## Commands ### 1. Create a Worktree ```bash # Create and check out a new branch git worktree add <path> <branch> # Create and check out an existing branch git worktree add <path> <existing-branch> # Create a new branch and check it out git worktree add -b <new-branch> <path> # Create with detached HEAD git worktree add --detach <path> # Create with specific commit git worktree add <path> <commit-hash> ``` **Examples:** ```bash # New branch "feature/auth" at ../project-feature git worktree add ../project-feature -b feature/auth # Check out existing branch "main" at ../project-main-review git worktree add ../project-main-review main # Detached HEAD at a specific commit git worktree add --detach ../project-review abc1234 ``` ### 2. List Worktrees ```bash git worktree list ``` **Output:** ``` /path/to/project-main a1b2c3d [main] /path/to/project-feature e4f5g6h [feature/auth] /path/to/project-hotfix i7j8k9l [hotfix/login] ``` ### 3. Remove a Worktree ```bash # Remove (must be clean — no uncommitted changes) git worktree remove <path> # Force remove (discards uncommitted changes) git worktree remove --force <path> # Prune stale worktree entries (if directory was deleted manually) git worktree prune ``` **Examples:** ```bash git worktree remove ../project-feature git worktree remove -f ../project-hotfix ``` ### 4. Lock / Unlock a Worktree Locking prevents a worktree from being pruned or accidentally removed. ```bash # Lock a worktree git worktree lock <path> # Lock with a reason git worktree lock --reason "In use for production debugging" <path> # Unlock a worktree git worktree unlock <path> ``` ### 5. Move a Worktree ```bash # Move to a new location git worktree move <path> <new-path> ``` **Example:** ```bash git worktree move ../project-feature ../new-location/project-feature ``` ### 6. Repair a Worktree If the link between a worktree and the main `.git` directory is broken: ```bash git worktree repair ``` ### 7. Clean Up Stale Entries If you deleted a worktree directory manually (without `git worktree remove`): ```bash git worktree prune ``` --- ## Common Workflows ### Workflow 1: Quick Bugfix While Working on a Feature ```bash # You're on feature/auth, need to fix a production bug git worktree add ../project-hotfix main cd ../project-hotfix git checkout -b hotfix/critical-bug # ... make fix, commit, push ... cd ../project-main git worktree remove ../project-hotfix ``` ### Workflow 2: Review a Pull Request ```bash # Fetch the PR branch git fetch origin pull/123/head:pr-123 # Create a worktree to review it git worktree add ../project-pr-review pr-123 cd ../project-pr-review # Review, test, etc. cd ../project-main git worktree remove ../project-pr-review git branch -D pr-123 ``` ### Workflow 3: Parallel Builds / Tests ```bash # Main worktree on main branch git worktree add ../project-bench -b benchmark/experiment cd ../project-bench # Run benchmarks while continuing development in main worktree ``` ### Workflow 4: Bisect Without Losing Your Place ```bash # Start bisect in a separate worktree git worktree add ../project-bisect cd ../project-bisect git bisect start git bisect bad <commit> git bisect good <commit> # ... test commits ... git bisect reset cd ../project-main git worktree remove ../project-bisect ``` --- ## Rules and Limitations 1. **Same branch cannot be checked out in multiple worktrees** (unless using `--force`) 2. **Bare repositories** are recommended as the main repo when using many worktrees 3. **Git submodules** have limited support in worktrees 4. **Ignored files**: Each worktree has its own `.git/info/exclude` but shares `.gitignore` 5. **Hooks**: All worktrees share the same hooks from the main `.git/hooks/` 6. **Config**: Shared across all worktrees (use `git worktree config` for per-worktree settings) --- ## Per-Worktree Configuration ```bash # Set a config value for a specific worktree git worktree config set <key> <value> # Example: different user.email per worktree git worktree config set user.email "review@company.com" ``` --- ## Bare Repository Pattern (Recommended for Heavy Use) If you use worktrees frequently, convert your repo to bare: ```bash # Clone as bare git clone --bare git@github.com:user/repo.git ~/projects/repo.git # Create worktrees from bare repo cd ~/projects/repo.git git worktree add ../main main git worktree add ../feature -b feature/new-ui # Structure: # ~/projects/repo.git/ ← bare repo (no working files) # ~/projects/main/ ← worktree on main # ~/projects/feature/ ← worktree on feature/new-ui ``` --- ## Troubleshooting ### "branch is already checked out" Another worktree has this branch. Use a different branch or: ```bash git worktree add --force <path> <branch> ``` ### Stale worktree entries after manual deletion ```bash git worktree prune ``` ### Broken worktree links ```bash git worktree repair ``` ### Cannot remove worktree with uncommitted changes Commit or stash first, or force remove: ```bash git worktree remove --force <path> ``` --- ## Quick Reference Card | Command | Description | | :--- | :--- | | `git worktree add` | Create a new worktree | | `git worktree add -b` | Create + new branch | | `git worktree add --detach` | Create with detached HEAD | | `git worktree list` | Show all worktrees | | `git worktree remove` | Delete a worktree | | `git worktree remove -f` | Force delete a worktree | | `git worktree lock` | Lock a worktree | | `git worktree unlock` | Unlock a worktree | | `git worktree move` | Move a worktree | | `git worktree prune` | Clean stale entries | | `git worktree repair` | Fix broken links | --- ## Tips - Use **absolute paths** or paths **outside** the main repo directory - Name worktree directories clearly (e.g., `../project-hotfix`, not `../wt1`) - Combine with `git stash` if you need to share uncommitted changes between worktrees - IDE support: most IDEs (VS Code, WebStorm) handle worktrees well — just open the worktree directory as a separate window

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully