如何在Git(Github)中重新提交提交

时间:2015-12-18 16:29:42

标签: git github rebase

假设我在Github上有一些原始回购分叉。我不能直接提交原始仓库,但可以创建拉动请求,然后我的老板可以与主人合并。

所以我创建了拉取请求,然后添加了另一个提交git push和另一个...

commit 4b4c5adb07b5faec478c2cefa06d1cec57047b1c
Author: me
Date:   Fri Dec 18 16:34:52 2015 +0200

    - commit 4

commit 09976ccb977b9d26cdaafb9ce10a4873ce0aa72s
Author: me
Date:   Thu Dec 17 18:19:44 2015 +0200

    - commit 3


commit 959ecb664a54374569eaeda86d5ab5fcbdc1cd26
Author: me
Date:   Tue Dec 15 09:09:35 2015 +0000

    - commit 2

commit c0d5cd88ac9e8e05ac71e26cb090392a6e53f42f
Author: me
Date:   Fri Dec 11 16:59:42 2015 +0200

    - commit 1

所以现在我想将这些提交合并为一个。我试过谷歌搜索,但不完全明白如何做到这一点。也许有人可以帮助我?

2 个答案:

答案 0 :(得分:1)

使用 -

$ git rebase --interactive <commit_id>

(其中commit_id是提交1之前提交的提交ID。 您将在屏幕上看到以下内容 -

pick 7ebd974 commit 1
pick cd6d73f commit 2
pick f1d3e12 commit 3
pick 37f22bd commit 4

# Rebase 4ae22a2..37f22bd onto 4ae22a2 (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

编辑此项并将提取替换为提交2,3和4 -

pick 7ebd974 commit 1
squash cd6d73f commit 2
squash f1d3e12 commit 3
squash 37f22bd commit 4

# Rebase 4ae22a2..37f22bd onto 4ae22a2 (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

保存更改后,您将在屏幕上显示以下内容 -

# This is a combination of 4 commits.
# The first commit's message is:
commit 1

# This is the 2nd commit message:

commit 2

# This is the 3rd commit message:

commit 3

# This is the 4th commit message:

commit 4

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Dec 19 04:00:27 2015 +0530
#
# rebase in progress; onto 4ae22a2

现在最后写一个相应的提交消息,并在它们之前使用#注释掉其他提交消息 -

# This is a combination of 4 commits.
# The first commit's message is:

final commit

#commit 1

# This is the 2nd commit message:

#commit 2

# This is the 3rd commit message:

#commit 3

# This is the 4th commit message:

#commit 4

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#

在完成所有这些之后,您可以强制推动以使您的本地更改反映在遥控器上 -
git push --force <remote_name> <branch_name>

答案 1 :(得分:0)

由于你已经推动了单独的提交,将这些提交压缩成一个提交是不明智的。因为Github并不真正知道你做了什么,所以你必须再次强行推动。

使用rebase,你的提交可以被压缩到sha c0d5cd88ac9e8e05ac71e26cb090392a6e53f42f,Github上的存储库知道下一次提交,因此除非你强制它,否则不允许推送。

问题可能是你的老板已经拉了你的拉请求,现在他的本地分支与github上的那个完全不同步。

如果你真的想这样做:

  1. 删除您的提款请求。
  2. 当地:git rebase -i c0d5cd88ac9e8e05ac71e26cb090392a6e53f42f
  3. 按照屏幕说明进行操作。
  4. 再次推送到github
  5. 制作新的PR