某次的意外 commit 中误将密码写到代码中并且 push 到了 remote repo 里面, 本文将围绕这个场景讨论如何弥补.
git clone https://gitee.com/lpwm/myrepo.git
cd myrepo
echo "A file" > A.txt
echo "B file" > B.txt
git add *
git commit -m "Initial commit"
git push
echo "Password is admin" >> A.txt
echo "Normal content appended" >> B.txt
git add *
git commit -m "Fault commit"
git push
主要思路是, 首先从当前已经误提交的状态创建一个新的 branch 作为备份, 在主 branch revert 回滚到误提交前一次的 commit, 强制 push 到 remote 端, 此时远端 repo 中就不会存在后面误提交的 commit 了. 再从备份的 branch merge 到当前 branch, 修改误操作的文件后提交新的 commit, 然后使用 squash 合并最近的两次 commit, 最后再 push 一把.
git log
commit 327d1b09d7fdb43af596c05713d8424181a6b97b (HEAD -> master, origin/master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:21:24 2023 +0800
Fault commit
commit 27b6ee4ed4a0ff44f2a5831fd5162075bef6a4b4
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:15:30 2023 +0800
Initial commit
git branch backup
git branch
backup
* master
git reset --hard 27b6ee4ed4a0ff44f2a5831fd5162075bef6a4b4
cat A.txt
A file
git push --force
检查确认 Gitee 中误提交的那次 commit 已经被删除
误提交的内容是删掉了, 但是其他正常修改的文件内容也没了, 肯定是不行的. 接下来我们继续使用前面创建的 backup branch 来进行合并恢复和修补
确认当前激活的 branch 是 master
git branch
backup
* master
git merge backup
git log
commit 327d1b09d7fdb43af596c05713d8424181a6b97b (HEAD -> master, backup)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:21:24 2023 +0800
Fault commit
commit 27b6ee4ed4a0ff44f2a5831fd5162075bef6a4b4 (origin/master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:15:30 2023 +0800
Initial commit
cat A.txt
A file
Password is admin
echo "A file" > A.txt
git add *
git commit -m "Fixed commit"
commit b71cde372e30f7bec24a7a8d9086711ab517a2ba (HEAD -> master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:52:32 2023 +0800
Fixed commit
commit 327d1b09d7fdb43af596c05713d8424181a6b97b (backup)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:21:24 2023 +0800
Fault commit
commit 27b6ee4ed4a0ff44f2a5831fd5162075bef6a4b4 (origin/master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:15:30 2023 +0800
Initial commit
git rebase -i HEAD~2
注意这里的 HEAD~2
表示从 HEAD
位置往前倒推选择两次 commit 记录进行编辑, 从上面的 log 也可以找到 HEAD 位置就是最近提交的这次 Fixed commit
. git rebase
命令执行后会进入到交互编辑模式
注意这里的 commit 显示顺序是倒着来的, 我们想要将 Fixed commit 合并入 Fault commit 里面, 因此需要修改 Fixed commit 前面的 pick
为 squash
, :wq
保存后会出现新的 commit message, 默认是将两次的 commit message 拼接到了一起, 愿意改的就改一下, 然后继续保存, 提示 rebase 成功
git rebase -i HEAD~2
[detached HEAD 2f1bc19] Fault commit
Date: Tue Dec 5 22:21:24 2023 +0800
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.
git log
commit 2f1bc195739602a5d6d20e8438e143941b4e3359 (HEAD -> master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:21:24 2023 +0800
Fault commit
Fixed commit
commit 27b6ee4ed4a0ff44f2a5831fd5162075bef6a4b4 (origin/master)
Author: lpwm <[email protected]>
Date: Tue Dec 5 22:15:30 2023 +0800
Initial commit
git push
最后要做的一步就是清理本地 backup branch, 注意这里要用 -D
参数, 等同于 --delete --force
强制删除, 如果是普通的 -d
删, 会提示 error: The branch 'backup' is not fully merged.
git branch -D backup
Squash commits into one with Git