Skip to content

Instantly share code, notes, and snippets.

@amjadjibon
Created July 10, 2024 01:53
Show Gist options
  • Select an option

  • Save amjadjibon/83d1b7f01914265f2ead9380c268cc24 to your computer and use it in GitHub Desktop.

Select an option

Save amjadjibon/83d1b7f01914265f2ead9380c268cc24 to your computer and use it in GitHub Desktop.

Using Git for Windows within WSL2 for Windows File Paths

WSL2 can be slow when accessing Windows files, but you can speed things up by using Git for Windows when working with Windows file paths. Follow these steps:

  1. Install Git for Windows

  2. Install Git on WSL

    • Install Git on your WSL2 environment if you haven't already:
      sudo apt update
      sudo apt install git
  3. Add a Custom Git Script to Your WSL

    • Create a script to call the Windows-installed Git when working within Windows file paths. Save this script in the /usr/local/bin directory and name it git.
    sudo nano /usr/local/bin/git
    • Add the following script content:
    #!/bin/bash
    
    GIT_WINDOWS="/mnt/c/Program Files/Git/cmd/git.exe"
    GIT_LINUX="/usr/bin/git"
    
    case "$(pwd -P)" in
    /mnt/?/*)
      case "$@" in
      # Needed to fix prompt, but it breaks things like paging, colours, etc
      rev-parse*)
        # running linux git for rev-parse seems faster, even without translating paths
        exec "$GIT_LINUX" "$@"
        ;;
      *)
        exec "$GIT_WINDOWS" "$@"
        ;;
      esac
      ;;
    *)
      exec "$GIT_LINUX" "$@"
      ;;
    esac
    • Save the file and make it executable:
      sudo chmod +x /usr/local/bin/git

Now, when you use git within your WSL2 terminal, the script will automatically detect if you are in a Windows file path and call the Windows-installed Git. This should help improve performance when working with repositories located in Windows directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment