从git存储库中删除文件

时间:2013-09-16 10:45:31

标签: git

我在从git存储库中删除文件时遇到问题。为了演示:在空目录中执行:

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"

现在我想从存储库中完全删除文件“A.txt”(好像它从未存在过):

rm A.txt
git log --pretty=oneline --branches -- A.txt
git filter-branch --index-filter \
    'git rm --cached --ignore-unmatch A.txt' \
    -- --all

给出输出:

3ce037a722c2f382a9da42f73577628a639cae25 3. version
43a12da356ad3643657e6bb06259c84f78b82bed 1. version
Cannot rewrite branches: You have unstaged changes.

然后git status给出:

# 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")

我在这里做错了什么?如何完全删除文件A.txt

1 个答案:

答案 0 :(得分:6)

问题在于您正在执行的rm A.txt。这是gst报告的未分级变更。只需在git filter-branch之后移动此行。