如何将两个提交合并为一个提交? [英] How can I combine two commits into one commit?

查看:131
本文介绍了如何将两个提交合并为一个提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分支"firstproject",包含2个提交.我想摆脱这些提交,并使它们显示为单个提交.

I have a branch 'firstproject' with 2 commits. I want to get rid of these commits and make them appear as a single commit.

命令 git merge --squash 听起来很有前途,但是当我运行 git merge --squash 时,我的终端仅显示该命令的选项.正确的命令是什么?

The command git merge --squash sounds promising, but when I run git merge --squash my terminal just brings up options for the command. What is the correct command?

Commit 1:
Added 'homepage.html'
Added 'contacts.html'

Commit 2:
Added 'homepage.php'
Added 'homepage.php'
Deleted 'homepage.html'
Deleted 'contacts.html'

推荐答案

您要 git rebase -i 来执行如果您当前在 on 上,您的"commit 1"和您要合并的提交"commit 2"是上一次提交,则可以运行 git rebase -iHEAD〜2 ,它将产生一个编辑器,列出了所有将重新运行的提交.您应该看到两行以"pick"开头.要进行压扁,请将第二行的第一个单词从"pick"更改为"squash".然后保存文件,然后退出.Git会将您的第一次提交挤压到您的第二次提交提交.

If you're currently on your "commit 1", and the commit you want to merge, "commit 2", is the previous commit, you can run git rebase -i HEAD~2, which will spawn an editor listing all the commits the rebase will traverse. You should see two lines starting with "pick". To proceed with squashing, change the first word of the second line from "pick" to "squash". Then save your file, and quit. Git will squash your first commit into your second last commit.

请注意,此过程将重写分支的历史记录.如果要将代码推送到某个地方,则必须 git push -f ,共享您代码的任何人都必须跳过一些箍才能完成更改.

Note that this process rewrites the history of your branch. If you are pushing your code somewhere, you'll have to git push -f and anybody sharing your code will have to jump through some hoops to pull your changes.

请注意,如果有问题的两个提交 分支中的最后两个提交,则过程将稍有不同.

Note that if the two commits in question aren't the last two commits on the branch, the process will be slightly different.

这篇关于如何将两个提交合并为一个提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆