Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Git] 两种方法合并多个commit为一个_goalnd 合并commit-CSDN博客 #101

Open
HenryTSZ opened this issue Jul 11, 2024 · 0 comments

Comments

@HenryTSZ
Copy link
Owner

[Git] 两种方法合并多个commit为一个_goalnd 合并commit-CSDN博客

0. 前情提要

要准备提交MR了,改了半天搞了很多个commit,都push上去了,但是提交MR的时候要合成一个commit,咋搞呢?

0.1 我期望的效果
1. 合并commit之前

比如我现在有4个 commit ID,从新到旧分别为:

85d5d8fa468b06bb9a62fafde01d80cbb7396682 # 我改的

621ca4121f971d9604e395556763551427d799d9 # 我改的

f744d2e91916ab7831f3a7695d1d1825916db164 # 我改的

5c135e49e683563fa470d7f5c281050ec1d73af9 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的

2. 合并commit之后

我想把我改的 commit ID 全部合成一个新的 commit ID :

8403afe13664d6bb7f5a5557716a030e9389a944 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的

1. 合并commitID的方法

1.1 第一种方法:先从版本库回退内容到暂存区,再重新提交工作区的内容

思路:使用 git reset --soft 回退版本库和暂存区的版本,同时保留工作区的变动,之后再重新提交工作区的内容就好了。

 `git log -10

git reset --soft 295ac3b842b4ecb6eff1c9954a281a4606a8bc84	

git add -u

git commit -m "修改信息"

git push --force` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10


如果push失败,出现Reject,则需要开启分支强制合入的选项,取消分支保护。

Settings -> Repository -> Protected Branches -> Protected branch (找到分支) -> Unprotect

1.2 第二种方法:git rebase
 `git log -10

git rebase -i HEAD~4	

git add -u

git commit -m "修改信息"

git push --force` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10


注意:git rebase 会临时创建一个新分支进行,如果弄着出错了,可以 git checkout 原分支名 切换回原分支之后重新 git rebase

2. git rebase 压缩 commit 图示

2.1 git log 查看分支

我想合并前四个 commit 到最后一个。如下所示:

2. git rebase -i HEAD~n

使用 git rebase -i HEAD~5 压缩5个commit为1个,或者git rebase -i 51efaef517abdbf674478de6073c12239d78a56a (第一个commit的id)

vim编辑器,按i编辑,将后4个commit的pick修改为fixup,保留第一个pick。按esc键,输入:wq保存退出。

  • pick:使用commit。

  • reword:使用commit,修改commit信息。

  • squash:使用commit,将commit信息合入上一个commit。

  • fixup:使用commit,丢弃commit信息。

操作完之后,发现commit都合并成了一个。

3. git push --force 提交

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant