I have a problem removing a file from my git repository. In order to demonstrate: In an empty directory perform:
git init
echo "A" > A.txt
echo "B" > B.txt
git add .
git commit -a -m "1. version"
echo "C" > C.txt
git add .
git commit -a -m "2. version"
echo "A" >> A.txt
git add .
git commit -a -m "3. version"
Now I would like to remove the file "A.txt" completely from the repository (as if it had never existed):
rm A.txt
git log --pretty=oneline --branches -- A.txt
git filter-branch --index-filter \
'git rm --cached --ignore-unmatch A.txt' \
-- --all
which gives output:
3ce037a722c2f382a9da42f73577628a639cae25 3. version
43a12da356ad3643657e6bb06259c84f78b82bed 1. version
Cannot rewrite branches: You have unstaged changes.
Then git status
gives:
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: A.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
What am I doing wrong here? How can I remove the file A.txt
completely?
The problem is the rm A.txt
you are performing. This is the unstaged change git reports. Simply move this line after the git filter-branch
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With