EN
Git - remove all files and directories that are not .git
11 points
In this short article, we would like to show how in Bash remove all files and directories that are not .git
directory.
The below command lets to remove ignored files and directories too.
Quick solution:
xxxxxxxxxx
1
rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')
Or with sudo
as superuser:
xxxxxxxxxx
1
sudo rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')
Note: keep attention because the command is not undoable.
Run command in the git project directory.
xxxxxxxxxx
1
$ cd /path/to/my/project
2
3
$ ls -al
4
total 24
5
drwxrwxr-x 5 john john 4096 lut 16 13:09 .
6
drwxrwxr-x 5 john john 4096 lut 12 15:14 ..
7
drwxrwxr-x 8 john john 4096 lut 16 12:14 .git
8
drwxrwxr-x 2 john john 4096 lut 16 13:08 .node_modules
9
-rw-rw-r-- 1 john john 216 lut 16 13:08 package.json
10
drwxrwxr-x 2 john john 4096 lut 16 13:09 src
11
12
$ rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')
13
14
$ ls -al
15
total 12
16
drwxrwxr-x 3 ggorecki ggorecki 4096 lut 16 13:10 .
17
drwxrwxr-x 5 ggorecki ggorecki 4096 lut 12 15:14 ..
18
drwxrwxr-x 8 ggorecki ggorecki 4096 lut 16 12:14 .git