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:
git status
will show uncommitted changesIs there a better way to remove the working directory's files? Something akin to hg co null
or p4 sync ...#none
?
For an existing project to become a Git repository, navigate into the targeted root directory. Then, run git init . Or, you can create a new repository in a directory in your current path. Use git init <directory> and specify which directory to turn into a Git repository.
There are two ways to create a bare Git repo: Clone an existing repository with the git clone –bare switch. Create a new bare git repo with the git init –bare switch.
To change this current working directory, you can use the "cd" command (where "cd" stands for "change directory"). For example, to move one directory upwards (into the current folder's parent folder), you can just call: $ cd ..
What is a bare repository? A bare repository is the same as default, but no commits can be made in a bare repository. The changes made in projects cannot be tracked by a bare repository as it doesn't have a working tree. A working tree is a directory in which all the project files/sub-directories reside.
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
You can use the --assume-unchanged
feature:
git ls-files big-unused-component | xargs git update-index --assume-unchanged
Then, you can remove your files:
rm -rf big-unused-component
The first command lists all the tracked files in the big-unused-component
directory, and then update-index --assume-unchanged
sets the "assume this file is unchanged" bit in the Git index. Then, when you remove that whole directory, Git doesn't consider that a change.
To undo the effect of the previous command, repeat it with --no-assume-unchanged
. The git ls-files -v
command can show you which files have been ignored in this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With