← Back to Git Course | Chapter 2: Basic Commands | Lesson 17 of 18

Git clean Command

git clean एक room-tidying robot जैसा है जो उन toys फेंक देता है जिनके बारे में Git को कभी पता नहीं था। एक बार वे गए तो गए, इसलिए पहले list पर झाँकें।
Syntax
bash
git clean -n
git clean -f
git clean -fd

Clean Dry-Run करना

git clean आपकी working directory से untracked files permanently delete करता है -- क्योंकि इसे undo नहीं किया जा सकता, कुछ भी actually हटाने से पहले हमेशा पहले git clean -n (dry run) चलाएँ यह देखने के लिए कि exactly कौन सी files हटेंगी।

उदाहरण: Dry-Running Clean

bash
git clean -n

Untracked Files Delete करना

एक बार dry-run list सही लगे, git clean -f (force) इसने list की हर untracked file का actual deletion perform करता है -- Git को accidental data loss से बचाव के लिए यह explicit force flag चाहिए।

उदाहरण: Deleting Untracked Files

bash
git clean -f

Untracked Directories साफ करना

Default रूप से, git clean सिर्फ loose files हटाता है और अब-खाली folders को छोड़ देता है; -d add करना इसे untracked directories भी हटाने तक extend करता है, आपको genuinely एक clean working tree देते हुए।

उदाहरण: Cleaning Untracked Directories

bash
git clean -fd

Ignored Files साफ करना

आपके .gitignore से match होने वाली files (जैसे build artifacts या dependency caches) एक clean के दौरान भी default रूप से skip हो जाती हैं; -x flag इसे override करता है और ignored files भी delete करता है, एक true from-scratch rebuild के लिए उपयोगी।

उदाहरण: Cleaning Ignored Files

bash
git clean -fx

Interactive Cleaning

git clean -i एक साथ सब कुछ delete करने के बजाय एक interactive menu खोलता है, आपको review, pattern से filter, या individual files select करने देते हुए -- जब आप पूरी तरह sure न हों कि क्या untracked है तब clean करने का सबसे safe तरीका।

उदाहरण: Interactive Cleaning

bash
git clean -i
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना dry run के git clean -f चलाना, ऐसी untracked files delete करते हुए जिन्हें recover नहीं किया जा सकता; पहले git clean -n चलाएँ।
  2. यह उम्मीद करना कि git clean untracked folders हटा देगा, जबकि आपको -d चाहिए (git clean -fd)।
  3. यह उम्मीद करना कि यह .gitignore में listed files delete कर देगा, जबकि वे skip होती हैं जब तक आप -x इस्तेमाल न करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.