我不小心提交了一个不需要的文件(文件名。Orig而解决合并)到我的仓库几个提交前,没有我注意到它直到现在。我想从存储库历史记录中完全删除该文件。
是否有可能重写更改历史这样的文件名。奥里格从一开始就没有被添加到存储库中?
我不小心提交了一个不需要的文件(文件名。Orig而解决合并)到我的仓库几个提交前,没有我注意到它直到现在。我想从存储库历史记录中完全删除该文件。
是否有可能重写更改历史这样的文件名。奥里格从一开始就没有被添加到存储库中?
当前回答
我发现的最简单的方法是由leontalbot(作为评论)提出的,这是Anoopjohn发表的一篇文章。我认为有必要用自己的空间来回答:
(我将其转换为bash脚本)
#!/bin/bash
if [[ $1 == "" ]]; then
echo "Usage: $0 FILE_OR_DIR [remote]";
echo "FILE_OR_DIR: the file or directory you want to remove from history"
echo "if 'remote' argument is set, it will also push to remote repository."
exit;
fi
FOLDERNAME_OR_FILENAME=$1;
#The important part starts here: ------------------------
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch $FOLDERNAME_OR_FILENAME" -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
if [[ $2 == "remote" ]]; then
git push --all --force
fi
echo "Done."
所有的功劳都归于Annopjohn和leontalbot,感谢他们指出了这一点。
NOTE
请注意,脚本不包括验证,因此请确保不会出错,并有备份以防出现错误。这招对我很管用,但对你可能就不管用了。小心使用它(如果你想知道发生了什么,请点击链接)。
其他回答
为了把它添加到Charles Bailey的解决方案中,我只是使用了git rebase -i来从之前的提交中删除不需要的文件,它就像一个魅力。 的步骤:
# Pick your commit with 'e'
$ git rebase -i
# Perform as many removes as necessary
$ git rm project/code/file.txt
# amend the commit
$ git commit --amend
# continue with rebase
$ git rebase --continue
这就是git filter-branch的设计目的。
如果这是你想要清理的最新提交,我尝试使用git版本2.14.3 (Apple git -98):
touch empty
git init
git add empty
git commit -m init
# 92K .git
du -hs .git
dd if=/dev/random of=./random bs=1m count=5
git add random
git commit -m mistake
# 5.1M .git
du -hs .git
git reset --hard HEAD^
git reflog expire --expire=now --all
git gc --prune=now
# 92K .git
du -hs .git
如果你还没有提交任何东西,只需git rm文件和git commit——modify。
如果你有
git filter-branch \
--index-filter 'git rm --cached --ignore-unmatch path/to/file/filename.orig' merge-point..HEAD
将从合并点到HEAD,删除文件名的每个更改。创建并重写更改。使用——ignore-unmatch意味着如果由于某种原因filename. conf命令不会失败。奥里格在变化中消失了。这是git-filter-branch手册页中的示例部分推荐的方法。
Windows用户注意:文件路径必须使用正斜杠
这是最好的方法: http://github.com/guides/completely-remove-a-file-from-all-revisions
一定要先备份文件副本。
EDIT
Neon的编辑在审查过程中不幸被拒绝了。 请看下面的霓虹灯帖子,它可能包含有用的信息!
例如,删除所有不小心提交到git仓库的*.gz文件:
$ du -sh .git ==> e.g. 100M
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.gz' HEAD
$ git push origin master --force
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now
还是不管用吗?(我现在的git版本是1.7.6.1)
$ du -sh .git ==> e.g. 100M
不知道为什么,因为我只有一个主分支。无论如何,我终于得到了我的git回购真正清理了推到一个新的空的和裸露的git仓库,例如。
$ git init --bare /path/to/newcleanrepo.git
$ git push /path/to/newcleanrepo.git master
$ du -sh /path/to/newcleanrepo.git ==> e.g. 5M
(是的!)
然后我把它克隆到一个新目录,并把它的。git文件夹移动到这个目录。如。
$ mv .git ../large_dot_git
$ git clone /path/to/newcleanrepo.git ../tmpdir
$ mv ../tmpdir/.git .
$ du -sh .git ==> e.g. 5M
(是的!)终于清理干净了!)
在确认一切正常后,您可以删除../large_dot_git和../tmpdir目录(可能在几周或几个月后,以防万一……)