Languages
[Edit]
EN

Git - remove all files and directories that are not .git

11 points
Created by:
Kate_C
21340

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:

rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')

Or with sudo as superuser:

sudo rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')

Note: keep attention because the command is not undoable.

Practical example

 Run command in the git project directory.

$ cd /path/to/my/project

$ ls -al
total 24
drwxrwxr-x 5 john john 4096 lut 16 13:09 .
drwxrwxr-x 5 john john 4096 lut 12 15:14 ..
drwxrwxr-x 8 john john 4096 lut 16 12:14 .git
drwxrwxr-x 2 john john 4096 lut 16 13:08 .node_modules
-rw-rw-r-- 1 john john  216 lut 16 13:08 package.json
drwxrwxr-x 2 john john 4096 lut 16 13:09 src

$ rm -rf $(ls -al | awk 'NR>3 {if($9!=".git") print $9}')

$ ls -al
total 12
drwxrwxr-x 3 ggorecki ggorecki 4096 lut 16 13:10 .
drwxrwxr-x 5 ggorecki ggorecki 4096 lut 12 15:14 ..
drwxrwxr-x 8 ggorecki ggorecki 4096 lut 16 12:14 .git
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join