git_rm_and_mv
git rm & mv
Remove and move/rename files in Git.
❌ git rm
Remove File from Git and Disk
git rm filename.txt
Removes file from Git tracking AND deletes it from disk.
Remove from Git Only (Keep File)
git rm --cached filename.txt
Removes from Git but keeps the file on disk. Good for adding to
.gitignore.
Remove Multiple Files
git rm file1.txt file2.txt file3.txt
Removes multiple specified files.
Remove by Pattern
git rm "*.log"
Removes all files matching pattern.
Remove Directory
git rm -r folder/
Recursively removes a directory and all its contents.
Force Remove Modified File
git rm -f filename.txt
Forces removal of a file that has uncommitted changes.
Dry Run (Preview)
git rm --dry-run "*.log"
Shows what would be removed without actually removing.
📊 rm Workflow
graph LR
A[File in Git] -->|git rm| B[Staged for Deletion]
B -->|git commit| C[Removed from History]
A -->|git rm --cached| D[Untracked]
D --> E[File remains on disk]
📁 git mv
Rename File
git mv old-name.txt new-name.txt
Renames a file and stages the change.
Move File to Directory
git mv filename.txt folder/
Moves file into a folder.
Move and Rename
git mv old-folder/old-name.txt new-folder/new-name.txt
Moves and renames in one command.
Move Multiple Files
git mv file1.txt file2.txt destination-folder/
Moves multiple files to a folder.
📊 mv is Shortcut
git mv is equivalent to:
mv old-name.txt new-name.txt
git rm old-name.txt
git add new-name.txt
Git detects renames automatically even if you use regular
mv.
🔍 Track Renames in Log
Follow File Renames
git log --follow -- filename.txt
Shows history including before file was renamed.
Show Renames in Diff
git log --diff-filter=R --summary
Shows only commits that have file renames.
❌ Remove Untracked Files
Preview Cleanup
git clean -n
Shows what untracked files would be deleted (dry run).
Delete Untracked Files
git clean -f
Deletes untracked files (requires force flag).
Delete Untracked Files and Directories
git clean -fd
Deletes untracked files AND directories.
Delete Ignored Files Too
git clean -fdx
Also removes files in
.gitignore. ⚠️ Be careful!
Interactive Cleanup
git clean -i
Interactive mode to choose what to delete.
💡 Tips
git rm --cached config.local
echo "config.local" >> .gitignore
git add .gitignore
git commit -m "Ignore config.local"
Files removed with --cached are already in history. Use BFG or filter-repo to remove from history completely.
🔗 Related
#git #rm #mv #delete #rename