git - 我可以完全放弃提交吗?

标签 git git-rebase

<分区>

我想完全放弃某个提交。

我做了这个实验:

初始化一个repo

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

看起来 drop 成功了,但是如果我执行这个:

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

BANG,历史记录仍在 repo 协议(protocol)中的某个地方,并没有真正被删除。

我可以完全删除这段历史吗?

最佳答案

git 是懒惰的,所有这些提交都保存在你可以看到它们使用

git reflog

这将显示您所做的所有历史更改(移动分支、提交、 rebase 等)

它们确实会被垃圾收集器清理掉,但老实说,这个功能比麻烦更有帮助。对于它将使用的少量空间,我不会担心。

关于git - 我可以完全放弃提交吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120437/

相关文章:

git - 是否可以 git stash 正在进行的交互式 rebase ?

Git:更改我已经分支的分支

linux - 在 Windows 机器上 checkout 时,保留某些文件的 unix 行结尾 (LF)

java - 如何将 Git 服务器编写或打包为 Java Servlet 或 Java webapp?

git - 将所有分支(包括 child 和孙子)从他们的祖先那里 rebase

Git rebase/master 从分支更改为 master

git - 将更改推送到 .git 目录

arrays - 附加到 bash 数组的问题

git - 将本地分支从另一个本身经常重新定位的分支中重新定位是不是很糟糕?

git - 如何 merge 来自另一个分支的单个提交而不产生副作用(又名不使用cherry-pick)?