5

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Seems like I do not need the rm A.txt at all.. It seems to be removed by git filter-branch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.