git 修改已提交的 commit

 2023-09-05 阅读 427 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 修改历史的操作,原理上都是通过变基(rebase)实现的。 因为发生了修改,则每个涉及的 commit 都会计算出新的 SHA-1 校验和。 不使用 --force 选项,最好**不要修改已经推送到远端的 commit!**

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

修改历史的操作,原理上都是通过变基(rebase)实现的。

因为发生了修改,则每个涉及的 commit 都会计算出新的 SHA-1 校验和。

不使用 --force 选项,最好**不要修改已经推送到远端的 commit!**这样会与其他工作者产生冲突。

摘录自官方文档 Git v2 重写历史

修改最后一次 commit

只对最后一次 commit 做修改是比较方便的。

$ git commit --amend

这个命令会使用当前暂存区的内容,覆盖最后一次的 commit,还允许你修改 commit 附带的 message。

修改多个 commit

通过交互式的变基操作 git rebase -i 可以在任何想要修改的 commit 处暂停,修改后再继续。

$ git rebase -i HEAD~3

这是一个变基命令,在你确认变基后 HEAD~3..HEAD 范围内的每一个提交都会被重写,无论你是否修改过它。

假设此时的 git log

4 310154e fixed typo
3 f7f3f6d updated README
2 a5f4a0d added LICENSE
1 b85f921 init

上文的命令会打开文本编辑器,展示如下的列表

pick a5f4a0d added LICENSE
pick f7f3f6d updated README
pick 310154e fixed typo# Rebase b85f921..310154e onto b85f921
#
# 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
#
# 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

格式为 <command> <commit> <message>

命令有短长两种写法

  • p, pick 正常提交该 commit,继续变基
  • r, reword 提交该 commit,但重新编辑 message,编辑后继续变基
  • e, edit 提交该 commit,提交后暂停变基,使用户可以修改
  • s, squash 合并该 commit 到前一个 commit,并重新编辑 message,编辑后继续变基
  • f, fixup 合并该 commit 到前一个 commit,使用前一个 commit 的 message,合并后继续变基
  • x, exec 将本行之后部分视为 shell 命令执行

变基命令非常灵活,你可以在文本编辑器中改变 commit 的顺序,删除 commit 或者引入其他 commit,只要使得编辑后的变基操作列表能够正常运行即可。

因为 edit 命令而暂停了变基时,你可以执行各种操作,比如:

  • git commit --amend 修改暂停前的最后一次 commit
  • git reset HEAD^ 撤销最后一次 commit 到暂存区,再分次提交

修改妥当后,输入 git rebase --continue 继续变基。

转载于:https://my.oschina.net/tridays/blog/1505486

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/504.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息