[英]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 个提交的意图。
git rebase -i HEAD~<quantity of your commits>
(ie git rebase -i HEAD~5
)git rebase -i HEAD~<quantity of your commits>
(即git rebase -i HEAD~5
)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!reword
(这意味着您将在下一步中为此提交提供新评论)并单击 SAVE! If in vim, press esc
then save by entering wq!
esc
然后输入wq!
保存wq!
and press enter. Done完毕
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.