Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file from a git repository

Tags:

git

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?

like image 776
Håkon Hægland Avatar asked Sep 16 '13 10:09

Håkon Hægland


1 Answers

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.

like image 65
Daniel Hilgarth Avatar answered Oct 19 '22 00:10

Daniel Hilgarth