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

How to rebase commits in Git (Github)

假设我在 Github 上有一些原始仓库的分支。我不能直接提交到原始 repo,但可以创建拉取请求,然后我的老板可以将其与 master 合并。

所以我创建了拉取请求,然后添加了另一个提交 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

所以现在我想将这些提交合并为一个。我试过谷歌搜索,但不完全理解如何正确地进行搜索。也许有人可以帮助我?

由于您已经推送了单独的提交,因此将这些提交压缩为一个是不明智的。您将不得不再次用力推动,因为 Github 并不真正知道您做了什么。

通过变基,您的提交可以被压缩到 sha c0d5cd88ac9e8e05ac71e26cb090392a6e53f42f 中,Github 上的存储库知道下一次提交,因此它不会允许推送,除非您强制执行。

问题可能是您的老板已经拉取了您的拉取请求,现在他的本地分支机构将与 github 上的分支机构完全不同步。

如果你真的想这样做:

  1. 删除您的拉取请求。
  2. 本地:git rebase -i c0d5cd88ac9e8e05ac71e26cb090392a6e53f42f
  3. 按照屏幕上的说明进行操作。
  4. 再次推送到 github
  5. 创建新 PR

使用 -

$ 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 替换为 squash -

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>