如何将两个提交合并为一个提交? [英] How can I combine two commits into one commit?
问题描述
我有一个分支"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'
推荐答案
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.
请注意,此过程将重写分支的历史记录.如果要将代码推送到某个地方,则必须
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屋!