0

Is there a git command to do the inverse of a git-clean operation? I want to delete the .git folder and all tracked files, leaving only untracked files and folders.

Background: I have a tendency to create note files and other context throughout my git repos. For example, I might create a hints file or Windows shortcut file that make it easy for me to jump to a folder with related content. When I make a new repo from the tip of a branch in another repo, I would like to add back in the context files. I'm looking for a procedure that would allow me to make a copy of the original repo structure, remove all git-related stuff, then copy what remains into the new clean repo I just built, effectively transferring all my non-git files from one repo to another.

edj
  • 523
  • 7
  • 17
  • Does this answer your question? [git clean only tracked files](https://stackoverflow.com/questions/37781162/git-clean-only-tracked-files) – mkrieger1 Nov 05 '20 at 18:01
  • I would try to find a way to *delete nothing* and instead copy only the untracked files out of the repository. Trying to delete an entire Git repository is risky, if you accidentally do it in the wrong working directory. – mkrieger1 Nov 05 '20 at 18:10
  • Took suggestion from @EncryptedWatermelon, and found the solution that works for me: – edj Nov 05 '20 at 19:45

5 Answers5

0

you could chain git ls-files with rm. then you'd need to delete empty directories left over -- something like this

git ls-files -z | xargs -0 rm
find . -type d -empty -print0 | xargs -0 rm -r
rm -rf .git

note that I'm using -z and -print0 combined with xargs -0 to preserve any whitespace-containing filenames (these use the null byte ('\x00') as a delimiter)

trying this on a repository of mine:

$ git clone -q git@github.com:asottile/astpretty
$ cd astpretty/
$ touch note
$ git ls-files -z | xargs -0 rm
$ find . -type d -empty -print0 | xargs -0 rm -r
$ rm -rf .git/
$ ls -al
total 16
drwxr-xr-x  2 asottile asottile  4096 Nov  5 10:03 .
drwxrwxrwt 19 root     root     12288 Nov  5 10:02 ..
-rw-r--r--  1 asottile asottile     0 Nov  5 10:02 note
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
0

You can do a non destructive copy by parsing the output of git clean. This will create a save_dir in the directory above your checkout. It will also preserve the directory structure. If you don't need the directories remove the --parents option.

mkdir ../save_dir
for x in `git clean -dfxn | awk '{print $3}'`; do
  cp --parents $x ../save_dir/
done
EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
0

I want to delete the .git folder and all tracked files, leaving only untracked files and folders.

git read-tree -um `git mktree <&-`   # track nothing, remove formerly-tracked
rm -rf .git                          # wipe
jthill
  • 55,082
  • 5
  • 77
  • 137
0

My stated goal was to find a way to preserve non-git files and I had assumed that cleaning git files was the best solution. As suggested by @mkreiger1, the best solution is not to alter the original repo, but rather to use git to identify non-repo files and copy them out. The solution below works with spaces in the filenames and folder paths, keeping the 3rd field through EOL (deleting "would remove" from the front of the line).

mkdir ../save_dir/
git clean -dfxn | awk '{$1=$2="" ; print $0}' | while read -r p
do 
  cp --parents "$p" ../save_dir/  
done
edj
  • 523
  • 7
  • 17
-1

To answer the question you directly asked:

git rm -r :/:*

will remove all tracked files from the worktree. (You may have to add a -f if you have uncommitted work, but that probably isn't a time when this would make sense.) You would still have to remove the .git folder, such as by

rm -rf .git

because git does not have an "opposite" to git init.

Based on the background you've provided, that may or may not be the best way to do what you're trying to do - and it sounds like you're reconsidering that - but I can't figure out why others are giving such complex answers to the original question so thought this should be added to the convesation.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52