When dealing with numerous git repositories scattered across various directories, managing multiple projects can often lead to clutter. The manual procedure of identifying and removing these repositories can prove to be time-consuming and prone to errors.
Imagine a scenario where your projects are spread across dozens of directories, each containing its own .git
folder. Navigating through each directory manually to clean up unnecessary repositories eats up your valuable time and disrupts your workflow. This clutter can slow down your system, create confusion, and hinder productivity.
To streamline this process, I’ve crafted a simple yet effective Linux script 🤖 that automates the tedious task of locating and optionally deleting .git
directories within the current directory and all its subdirectories.
This script ensures you can focus more on development and less on directory management.
Cleaning Up Git Directories
Understanding the Script
The script is designed to search for .git
directories recursively, print their paths, and give you the option to delete them all at once. It uses the powerful find
command to achieve this, enhancing efficiency and accuracy.
#!/bin/bash
# Array to hold paths of found .git directories
git_dirs=()
# Function to find .git directories and list them
find_git_directories() {
echo -e "🔍 Searching for .git directories...\n"
(sleep 1) &
show_loader
# Use find to locate all .git directories starting from the current directory
git_dirs=($(find . -type d -name ".git"))
# Print each found .git directory
if [ ${#git_dirs[@]} -eq 0 ]; then
echo "❌ No .git directories found."
else
for dir in "${git_dirs[@]}"; do
echo "📂 Found .git directory: $dir"
done
fi
}
# Function to delete found .git directories after confirmation
delete_confirmed_git_directories() {
if [ ${#git_dirs[@]} -eq 0 ]; then
return
fi
# Ask if the user wants to delete all found .git directories
echo -e "\n"
read -p "🚨 Do you want to delete all found .git directories? (y/n): " choice
if [[ "$choice" == "y" || "$choice" == "Y" ]]; then
(sleep 1) &
show_loader
for dir in "${git_dirs[@]}"; do
rm -rf "$dir" && echo "🗑️ Deleted: $dir"
done
else
echo -e "\n✅ No directories deleted."
fi
}
# Function to display a rotating loader
show_loader() {
local delay=0.1
local spinner='|/-\'
local i=0
# Loop until the background job completes
while kill -0 $! 2>/dev/null; do
printf "\r%c" "${spinner:i++%${#spinner}:1}"
sleep $delay
done
# Clean up
printf "\r"
}
# Call the functions
find_git_directories
delete_confirmed_git_directories
Breaking Down the find Command
find
This robust command allows for searching through directories..
Sets the current directory as the starting point of the search, ensuring every subdirectory is included.-type d
Filters the search to directories only (d
stands for directory).-name ".git"
Matches directories with the name.git
, crucial for targeting git repositories.
Implementing the Solution
First you should create a new file, say cleanup_git_dirs.sh
, and paste the script above into it. Then run chmod +x cleanup_git_dirs.sh to make the script executable.
Finally navigate to the directory where you want to clean up git repositories and run ./cleanup_git_dirs.sh
. The script will display all found .git
directories and prompt you for deletion confirmation.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Conclusion
By automating the cleanup of your .git directories, you save time and decrease the risk of errors. This streamlined process keeps your system organized and running efficiently, allowing you to maintain focus on what truly matters: developing top-tier code without the distraction of directory clutter.