我希望删除一个特定的提交。
我进行了以下实验:
初始化一个仓库
bash-3.2$ git init
Initialized empty Git repository in /Users/*****/test/.git/
创建一个文件。
bash-3.2$ touch readme
bash-3.2$ ls
readme
提交此次创建。
bash-3.2$ git add .
bash-3.2$ git commit -m "first commit"
[master (root-commit) b9f8e60] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme
编辑该文件(出现了一些错误)
bash-3.2$ echo WrongMsg > readme
bash-3.2$ cat readme
WrongMsg
疏忽地提交了那个修改
bash-3.2$ git add .
bash-3.2$ git commit -m "careless commit"
[master 1eec681] careless commit
1 file changed, 1 insertion(+)
检查日志
bash-3.2$ git log --oneline
将会给予
1eec681 careless commit
b9f8e60 first commit
现在,我意识到了这个错误,并想要修正它。
bash-3.2$ git rebase -i HEAD^
将提示
pick 1eec681 careless commit
# Rebase b9f8e60..1eec681 onto b9f8e60 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
将pick
更改为drop
,然后使用:wq
保存,它将会输出结果。
Successfully rebased and updated refs/heads/master.
检查日志
bash-3.2$ git log --oneline
b9f8e60 first commit
更正错误
bash-3.2$ echo CorrectMsg > readme
bash-3.2$ cat readme
CorrectMsg
提交
bash-3.2$ git add .
bash-3.2$ git commit -m "correction"
[master f224289] correction
1 file changed, 1 insertion(+)
日志
bash-3.2$ git log --oneline
f224289 correction
b9f8e60 first commit
似乎删除成功了,但如果我执行以下操作:
bash-3.2$ git reset --hard 1eec681
HEAD is now at 1eec681 careless commit
bash-3.2$ git log --oneline
1eec681 careless commit
b9f8e60 first commit
历史记录仍然存储在存储库中,没有被完全清除。
我能完全删除这个历史记录吗?
git gc
;或者您可以克隆存储库并删除原始存储库,这更为激进)。但是,我给了exussum的答案点赞,因为它通常是最适用的。 - torek