我已经创建了这个 GitHub 存储库,它完美地重现了我的问题,但我也会在这里进行解释(但如果有任何不清楚的地方,请随时查看!)。
我还应该指出,我非常乐意使用
vi
作为我的 rebase 工具,因为我不会经常进行 rebase,除非它确实让我的生活变得非常轻松,否则我宁愿推迟下载、安装和学习一个全新的工具只是为了偶尔的 rebase。
在我的 GitHub 项目中,我有一个名为
feature/some-feature
的分支,这个分支比 develop
和 main
都提前了几次提交(功能分支是从 develop
切下来的,而它本身又是从 main
切下来的) ).
在第二次提交时,我搞砸了并将敏感密码/凭据提交到配置文件:
db.name=fizz
db.port=9000
db.user=buzz
db.password=12345
哎哟雏菊!我不仅需要编辑
db.user
和 db.password
的值,而且还需要从 git 历史记录中清除整个提交,否则有效的凭据将在源代码管理中浮动。不利于生意!
所以首先我推送了一个 redacting commit 来编辑值,使配置现在看起来像:
db.name=fizz
db.port=9000
db.user=<!redacted!>
db.password=<!redacted!>
但现在我需要从历史记录中删除错误的提交(提交 ID
a04cec8a225cdd1cf64989ca47f582f4e51e7597
)。
因此,从我的
feature/some-feature
分支,我运行:
git rebase -i a04cec8a225cdd1cf64989ca47f582f4e51e7597
这将我带到这个
vi
屏幕:
如果我理解正确,我需要从列表中选择一个选项并将其输入某处。就我而言,我相信我想
drop
承诺a04cec8a225cdd1cf64989ca47f582f4e51e7597
。因此,有了 vi
,我只需按 i
即可进入交互模式,但是一旦到达那里,我完全不知道该怎么办。
我假设我想做类似
drop a04cec8a225cdd1cf64989ca47f582f4e51e7597
的事情,但不确定在文件中的哪里写。
然后,一旦我应用了正确的更改,我是否只需点击
:wq!
然后“Enter”,git 就会为我执行 rebase 命令?或者我需要做点别的事情吗?之后我是否需要提交或推送才能将本地更改传播到 GitHub?
我听从了一位用户的建议并运行了:
git rebase -i a04cec8a225cdd1cf64989ca47f582f4e51e7597^
这将我带到了这个屏幕:
我删除了该行:
然后按
:wq!
,然后按回车键,看到这个:
zharvey@Zacs-MacBook-Pro gh-purge-history-troubleshooting % git rebase -i a04cec8a225cdd1cf64989ca47f582f4e51e7597^
Auto-merging config.properties
CONFLICT (content): Merge conflict in config.properties
error: could not apply 5d52a45... redacting
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 5d52a45... redacting
然后我手动解决冲突并点击 VS Code 中的“保存”:
然后尝试做
git add . -n
,然后git commit -m <messag>
,但现在看到:
zharvey@Zacs-MacBook-Pro gh-purge-history-troubleshooting % git commit -m "resolving merge conflicts after redacting"
U config.properties
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
这感觉比实际需要的更复杂。有人可以给我提供我需要运行的确切命令,并为每个命令的作用提供一个简单的解释吗?我只想编辑这些值并将错误的提交从历史记录中删除。
对于任何后来者,这是我的案例的解决方案:
git rebase -i <commit-I-wish-to-purge>^
vi
中,按i
进入交互模式。然后删除顶部描述要删除的提交的整行。然后执行 :wq!
并按 Enter。git add .
git rebase --continue
git push --force