4 minute read

Working with GitHub from WSL and Windows Folders

Problem Statement:

I have project in D:\github-blog\dasarpai-websitetest. This is also a github repo. It’s remote repo is https://github.com/dasarpai/dasarpai-websitetest. I have github desktop for windows 11 installed on my machine. Sometime I push/pull files in github repo using this github desktop. I have Visual Code IDE installed on my windows 11 machine and I also visual code IDE on my machine’s wsl.

When I am working with this project from windows 11 or wsl then sometimes file will get updated.

Keeping all this in mind do you have anything for me which I need to take care of github repo code.

Solution Space

Given your setup with the dasarpai-websitetest project, GitHub Desktop, Visual Studio Code (VS Code) on Windows 11, and VS Code on WSL, along with pushing/pulling files to/from the GitHub remote repository, here are some tips and considerations to ensure smooth management of your GitHub repo code:


1. Syncing Between Windows 11 and WSL

Since you’re working on the same project from both Windows 11 and WSL, there’s a risk of file conflicts or inconsistencies. Here’s what to watch out for:

  • File Path Differences: Windows uses backslashes (\) and drive letters (e.g., D:\), while WSL uses Unix-style paths (e.g., /mnt/d/). Ensure your Git repository is accessible consistently. It’s best to work on the project from a directory that both systems can access seamlessly, like D:\github-blog\dasarpai-websitetest, which WSL maps as /mnt/d/github-blog/dasarpai-websitetest.
    • Tip: Open the project in WSL using VS Code’s WSL extension (Remote - WSL) to avoid path confusion.
  • Line Endings (CRLF vs LF): Windows uses CRLF (carriage return + line feed) for line endings, while WSL/Linux uses LF (line feed). This can cause Git to detect changes in files even if you didn’t modify them.
    • Fix: Configure Git to handle line endings consistently:
      git config --global core.autocrlf true  # On Windows
      git config --global core.autocrlf input # On WSL
      

      Alternatively, add a .gitattributes file to your repo: ```

      • text=auto ```
  • File Permissions: WSL might alter file permissions in a way that Windows doesn’t recognize properly. This usually isn’t an issue for Git unless you’re tracking executable scripts.
    • Tip: Avoid committing permission changes unless necessary (e.g., use git config core.fileMode false in WSL).

2. Using GitHub Desktop and VS Code Together

You’re using GitHub Desktop for push/pull operations, but you might also use Git commands in VS Code (via its terminal or Git integration). Here’s how to avoid confusion:

  • Check Git Status Before Switching Tools: If you make changes in VS Code (Windows or WSL) and then switch to GitHub Desktop, ensure there are no uncommitted changes. Run git status in the terminal or check the Git panel in VS Code.
  • Commit Frequently: Whether in GitHub Desktop or VS Code, commit changes often with meaningful messages to avoid losing work or creating large, unwieldy commits.
  • Pull Before Push: Always pull from the remote repo (https://github.com/dasarpai/dasarpai-websitetest) before pushing, especially if you switch between Windows and WSL. GitHub Desktop usually prompts for this, but double-check in VS Code if you use its Git features.

3. Avoiding Conflicts in the Repo

Since files “sometimes get updated” when working in Windows or WSL, you might accidentally overwrite changes or create merge conflicts. Here’s how to manage this:

  • Branching Strategy: If you’re experimenting or working on features, create separate branches (git checkout -b feature-name) instead of working directly on main. This keeps your main branch stable.
    • Example:
      git checkout -b wsl-updates  # Work in WSL
      git checkout -b windows-updates  # Work in Windows
      

      Merge them later with git merge.

  • Stash Changes: If you switch between Windows and WSL mid-work, use git stash to temporarily save uncommitted changes:
    git stash  # Save changes
    git pull   # Update from remote
    git stash pop  # Reapply changes
    
  • Sync Regularly: Use GitHub Desktop or git pull to keep your local repo in sync with the remote repo before starting work in either environment.

4. Tool-Specific Tips

  • GitHub Desktop:
    • Use it for simple push/pull operations, but if you encounter merge conflicts, resolve them in VS Code or the terminal since GitHub Desktop’s conflict resolution is limited.
    • Check the “History” tab to ensure your commits align with what’s on the remote repo.
  • VS Code (Windows):
    • Use the built-in Git integration (Source Control tab) to stage, commit, and push changes if you prefer a GUI over GitHub Desktop.
    • Install the “GitLens” extension for better Git history and blame tracking.
  • VS Code (WSL):
    • Ensure your WSL distro has Git installed (sudo apt install git for Ubuntu-based distros).
    • Use the WSL terminal in VS Code for Git commands if you’re working in the WSL environment.

5. Backup and Safety

  • Remote as Backup: Since your repo is on GitHub, it’s already a good backup. Push changes frequently to avoid losing work if something goes wrong locally.
  • gitignore: Ensure you have a .gitignore file to exclude unnecessary files (e.g., build artifacts, IDE configs like .vscode/, or WSL-specific files). Example:
    node_modules/
    .vscode/
    *.log
    

6. General Workflow Suggestion

Here’s a streamlined approach:

  1. Start your session by opening D:\github-blog\dasarpai-websitetest in VS Code (Windows or WSL).
  2. Run git pull (or use GitHub Desktop) to get the latest changes from https://github.com/dasarpai/dasarpai-websitetest.
  3. Make your changes and test them.
  4. Stage and commit your changes (git add . and git commit -m "your message" in VS Code, or use GitHub Desktop).
  5. Push to the remote repo (git push or GitHub Desktop’s “Push” button).
  6. Repeat when switching between Windows and WSL.

Final Notes

This setup is fairly standard, and with a little care, you can avoid most issues. The key is to stay consistent with how you interact with Git (e.g., pull before editing, commit often) and be mindful of the Windows-WSL differences.

Updated:

Leave a comment