简体   繁体   English

如何将两个提交合并为一个提交?

[英]How can I combine two commits into one commit?

I have a branch 'firstproject' with 2 commits.我有一个分支 'firstproject' 有 2 个提交。 I want to get rid of these commits and make them appear as a single commit.我想摆脱这些提交并使它们显示为单个提交。

The command git merge --squash sounds promising, but when I run git merge --squash my terminal just brings up options for the command.命令git merge --squash听起来很有希望,但是当我运行git merge --squash我的终端只会显示该命令的选项。 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'

You want to git rebase -i to perform an interactive rebase .您想要git rebase -i来执行交互式 rebase

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.如果您当前“提交 1”上,并且要合并的提交“提交 2”是前一个提交,则可以运行git rebase -i HEAD~2 ,这将生成一个列出所有提交的编辑器rebase 将遍历。 You should see two lines starting with "pick".您应该看到两行以“pick”开头。 To proceed with squashing, change the first word of the second line from "pick" to "squash".要继续挤压,请将第二行的第一个单词从“pick”更改为“squash”。 Then save your file, and quit.然后保存您的文件,然后退出。 Git will squash your first commit into your second last commit. Git 会将您的第一次提交压缩到您的最后一次提交中。

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.如果你把你的代码推送到某个地方,你必须git push -f并且任何共享你代码的人都必须跳过一些箍来拉你的更改。

Note that if the two commits in question aren't the last two commits on the branch, the process will be slightly different.请注意,如果有问题的两次提交不是分支上的最后两次提交,则过程将略有不同。

Lazy simple version for forgetfuls like me:懒惰的简单版本,适合像我这样健忘的人:

git rebase -i HEAD~3 or however many commits instead of 3. git rebase -i HEAD~3或许多提交而不是 3。

Turn this转这个

pick YourCommitMessageWhatever
pick YouGetThePoint
pick IdkManItsACommitMessage

into this进入这个

pick YourCommitMessageWhatever
s YouGetThePoint
s IdkManItsACommitMessage

and do some action where you hit esc then enter to save the changes.并在按esc地方执行一些操作,然后enter以保存更改。 [1] [1]

When the next screen comes up, get rid of those garbage # lines [2] and create a new commit message or something, and do the same escape enter action.当下一个屏幕出现时,摆脱那些垃圾 # 行 [2] 并创建一个新的提交消息或其他内容,并执行相同的escape enter操作。 [1] [1]

Wowee, you have fewer commits. Wowee,你的提交更少了。 Or you just broke everything.或者你只是打破了一切。


[1] - or whatever works with your git configuration. [1] - 或任何适用于您的 git 配置的东西。 This is just a sequence that's efficient given my setup.鉴于我的设置,这只是一个有效的序列。

[2] - you'll see some stuff like # this is your n'th commit a few times, with your original commits right below these message. [2] - 你会看到一些类似的东西# this is your n'th commit# this is your n'th commit ,你的原始提交就在这些消息的正下方。 You want to remove these lines, and create a commit message to reflect the intentions of the n commits that you're combining into 1.您想删除这些行,并创建一个提交消息来反映您正在合并为 1 的 n 个提交的意图。

  1. Checkout your branch and count quantity of all your commits.检查您的分支并计算所有提交的数量。
  2. Open git bash and write: git rebase -i HEAD~<quantity of your commits> (ie git rebase -i HEAD~5 )打开 git bash 并写入: git rebase -i HEAD~<quantity of your commits> (即git rebase -i HEAD~5
  3. In opened txt file change pick keyword to squash for all commits, except first commit (which is on the top).在打开txt文件更改pick关键字squash所有提交,除第一次提交(这是在顶部)。 For top one change it to reword (which means you will provide a new comment for this commit in the next step) and click SAVE!对于 top one 将其更改为reword (这意味着您将在下一步中为此提交提供新评论)并单击 SAVE! If in vim, press esc then save by entering wq!如果在 vim 中,按esc然后输入wq!保存wq! and press enter.并按回车键。
  4. Provide Comment.提供评论。
  5. Open Git and make "Fetch all" to see new changes.打开 Git 并执行“Fetch all”以查看新更改。

Done完毕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将3次提交与主服务器的一次合并重新定为一次提交? - How can I rebase 3 commits with one merge of master into one commit? 如何将A和K之间的提交压缩到一个提交Z中 - How can I squash commits between A and K into one commit Z 如何将两个早期提交加入一个提交 - How to join two early commits to one commit 将两个合并合并为一个(或者,我可以更改提交的父级吗?) - Combine two merges into one (or, can I change a commit's parents?) 如何在不使用交互式 git 变基的情况下将两个 git 提交合并为一个提交? - How do i combine two git commits into a single commit without using an interactive git rebasing? 如果中间还有其他提交,如何将所有git提交合并到一个提交中? - How to combine all git commits into one commit if there are others inbetween? 为什么我不能将提交合并为一个? - Why can't I combine commits into one? 如何将两个旧提交合并为一个? - How can I merge two old commits into one? 如何仅合并两个提交并压缩一个提交即可合并到另一个分支? - How to merge to another branch with only two commits and with one commit squashed? 如何删除git中的最后两个提交并在工作目录中将我的提交1作为更改? - How can I remove last two commits in git and get my commit 1 as changes in working directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM