1

I have a git repo that contains the following (simplified):

-- .git/
-- data/
   |_ bar.txt
-- src/
   |_ foo.py
-- .gitignore

The .gitignore ignores only the data directory. So, in this case, src/foo.py and .gitignore were staged and commited.

I want to remove the .git repo AND all files/folders tracked by git without deleting the entire directory as I want to keep the untracked files (ie remove src/foo.py, .gitignore, and .git but keep the data folder and it's contents).

I looked at git clean and tried searching stack overflow, but I couldn't seem to find an answer.

Note: The above repo is just used as an example, the actual repo I'm concerned with is much more complex and populated.

Jay Mody
  • 3,727
  • 1
  • 11
  • 27

2 Answers2

2

In a bash shell:

for f in $(git ls-tree -r HEAD --name-only); do;
    echo rm -rf $f
done
echo rm -rf .git

Remove the echo's to make this actually destroy all the files. I don't want anyone pasting this mindlessly and regretting so I'm keeping them.

Omer Tuchfeld
  • 2,886
  • 1
  • 17
  • 24
0

Figured it out. From this post, you can run the following command to list (line by line) all files tracked by the current git HEAD.

git ls-tree -r HEAD --name-only

From there it should be as easy as piping the output to xargs with the rm command:

git ls-tree -r HEAD --name-only | xargs rm

Then, delete the .git folder:

rm -rf .git

All-in-one command:

git ls-tree -r HEAD --name-only | xargs rm && rm -rf .git

Note: This will only delete files since git doesn't track folders, meaning you'll still have leftover empty directories (in this case, src/foo.py would be deleted, but the src directory itself would be leftover).

Jay Mody
  • 3,727
  • 1
  • 11
  • 27