Let's follow below example to understand file deleting scenarios
git status
clean directory
Create new file using mate in MacOS
mate doomed.txt
Delete untracked file
git status
Untracked files:
doomed.txt
git rm doomed.txt
fatal: pathspec 'doomed.txt' did not match any files
So we cannot delete using git command. cause doomed.txt file is yet to be indexed ot not yet indexed in git.
So we remove using OS command
rm doomed.txt
ls --> doomed.txt is deleted. verified
git status
nothing to commit, working directory clean
git ls-files
... so many files and last newfile.txt
newfile.txt
Remove a tracked file from git
git rm newfile.txt
rm 'newfile.txt' --> message shown
ls - newfile.txt is deleted. verified
git status
Changes to be committed:
deleted: newfile.txt
deletion is not permanent yet. to move forward need to commit.
git commit -m "deleting newfile"
git status
nothing to commit, working directory clean
Delete stage file
ls
hip.txt
git ls-files
....
hip.txt
..
..
git rm hip.txt
rm 'hip.txt'
ls - hip.txt is deleted. verified
git status
Changes to be committed:
deleted: hip.txt
Now if you need to revert this, delete --> Unstage
git reset HEAD hip.txt --> here becareful, tab will not be available as file is not present. So write the name of the file correctly.
git reset HEAD hip.txt
Unstaged changes after reset:
D hip.txt
--------
ls --> hip.txt still not available.
git status
Changed not staged for commit:
deleted: hip.txt
previously with reset unstaged the file, did not restore the file back to OS file system.
we need revert changes made for the working directory. follow the message in "git status"
git checkout -- hip.txt
ls --> verify. hip.txt is available
git status
nothing to commit, working directory clean
Above explain, how to delete a file using git.
what happens if we delete a file tracked or indexed in the git
In simple - git is tracking. you don't use git to remove. instead remove using terminal command or file explorer
let's use terminal command or bash command to delete a file
rm hip.txt --> not using git rm
ls - verified, hip.txt no longer exist
git status --> detects the remove
Changes not staged for commit:
deleted: hip.txt
We can handle that
git add -A
git status
Changes to be committed:
deleted: hip.txt
git commit
git status
nothing to commit, working directory clean
Remove entire folder structure
rm -rf lv1
ls --> verified, lv1 folder deleted
git status
Changes not staged for commit:
lv1/lv1.txt
lv1/lv2/lv2.txt
lv1/lv2/lv3/lv3.txt
git add -A
git status
Changes to be committed:
lv1/lv1.txt
lv1/lv2/lv2.txt
lv1/lv2/lv3/lv3.txt
git commit -m "level 1 and all childern"
git status
nothing to commit, working directory clean
No comments:
Post a Comment