gitgit-clean

Convert a Git working directory to a bare-like repository?


Our development environment uses multiple repositories as part of an overall build system. The build dynamically adapts depending on which repositories you have checked out: if you don't need to build a particular component, just don't clone it.

However, once you have cloned a component, removing it from the build is problematic:

Is there a better way to remove the working directory's files? Something akin to hg co null or p4 sync ...#none?


Solution

  • After finding similar questions that didn't quite do what I want, this article provided the answer: create an empty branch.

    git checkout --orphan empty
    git rm -rf .
    git commit --allow-empty -m "An empty working directory"
    

    Of course, ensure you've committed any important files to your repo first. I've called this branch empty, but you can give it any name via the checkout command.

    Once set up, you can switch to any branch to get your files back:

    git checkout master
    

    When you need to "remove" the working directory, commit your changes, then run:

    git checkout empty
    

    This removes all tracked files. If you also need to remove untracked files and directories, follow this up with:

    git clean -fdx