24. git revert

基本概述

git revert 的作用是:撤销某次的提交。与 git reset 不同的是,git revert 不会修改提交历史,而是创建一个新的提交来反转之前的提交。

基本用法

1.基本语法

git revert <commit-hash>
  • 该命令会生成一个新的提交,内容是将指定提交的更改反向应用

2.跳过编辑信息

git revert --no-edit <commit-hash>
  • 默认会打开编辑器让你编辑提交信息,可用 --no-edit 跳过

高级用法

1.撤销连续的多个提交

git revert <older-commit-hash>..<newer-commit-hash>
# 示例:撤销 commit1 到 commit3(假设 commit1 是更早的提交)
git revert commit1..commit3
  • 按提交顺序的反向依次撤销,例如先撤销较新的提交,再撤销较旧的

2.撤销非连续的多个提交

  • 多次执行 git revert ,依次指定要撤销的提交哈希

3.处理冲突
如果 git revert 导致冲突(无法自动合并)

  • 手动解决冲突文件
  • 使用 git add 标记冲突已解决
  • 继续完成撤销操作:git revert --continue
  • 若想放弃撤销使用:git revert --abort

4.手动提交

git revert -n/--no-commit <commit-hash>
  • 撤销更改但不自动提交,允许你手动提交

你可能感兴趣的:(Git,git)