Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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:

  • You can delete the files in the working directory, but git status will show uncommitted changes
  • You can delete the repository, at the expense of re-cloning when you need it again
  • You can move the repository, but your multi-repo tools might "helpfully" re-clone

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

like image 691
Syeberman Avatar asked Sep 30 '22 08:09

Syeberman


People also ask

How do I turn a directory into a git repository?

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.

How do you make an existing repository bare?

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.

How do I change my current working directory to an existing repository?

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 git repository?

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.


2 Answers

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
like image 82
Syeberman Avatar answered Oct 02 '22 13:10

Syeberman


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.

like image 26
Greg Hewgill Avatar answered Oct 02 '22 14:10

Greg Hewgill