git revert: 回滚某个特定的 commit
将我代码中某一个修改的 commit 丢弃掉,git 命令是什么?
这个 commit 已经提交到 remote 的 repo 里了
这个 commit 已经提交到 remote 的 repo 里了
在 Git 中,如果你需要丢弃一个已经提交到远程仓库的 commit,可以通过以下步骤实现。请注意,这种操作会修改历史记录,因此需要谨慎操作,尤其是在多人协作的项目中。
已知信息
- 你有一个已经提交到远程仓库的 commit。
- 你希望丢弃这个 commit。
解决方案
使用 git revert
(推荐)
git revert
是一种安全的方式来撤销某个 commit 的更改。它不会直接删除 commit,而是创建一个新的 commit 来抵消指定 commit 的更改。这种方式不会修改历史记录,适合多人协作的场景。
步骤:
1. 找到你想丢弃的 commit 的哈希值(可以通过 git log
查看)。
git log
假设你想丢弃的 commit 的哈希值是 abc1234
。
- 使用
git revert
撤销该 commit:
git revert abc1234
这会生成一个新的 commit,内容是撤销 abc1234
的更改。
- 将撤销后的更改推送到远程仓库:
git push origin <branch_name>
优点:
- 不会修改历史记录,适合多人协作。
- 安全且易于追踪。