将所有提交压缩为一个 GitHub 拉请求

我在 GitHub 上提交了请求。现在存储库的所有者要求将所有提交压缩到一个提交中。

当我键入 git rebase -i记事本时,打开的内容如下:

noop


# Rebase 0b13622..0b13622 onto 0b13622
#
# 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

我在谷歌上搜索,但我不知道如何做到这一点。

106764 次浏览

Try git rebase -i, and use 'squash' for all the commits you want to squash.

Edit:

git rebase -i will show you an interactive editor with the list of commits you are rebasing. The default command before each commit is "pick", so you just need to s/pick/squash/ for all the commits you want to squash, and then all of them will be squash into their last previous commit.

Make sure you are rebasing on a correct branch.

ok I figured it out ... First I had to write git rebase -i xxxxxxxxxxxxxxxx where xxxxxxxxxx is the SHA of the commit upto which I've to squash. Then in Notepad I edited the first as pick and rest of all as squash. Then a new notepad window will come and there in the first line I typed the name of my new commit. And then I had to do a force push :

git push --force origin master

Just a simple addition to help someone else looking for this solution. You can pass in the number of previous commits you would like to squash. for example,

git rebase -i HEAD~3

This will bring up the last 3 commits in the editor.

As of April 1, 2016, the repository's manager can squash all the commits in a pull request into a single commit by selecting "Squash and merge" on a pull request.

Squash and merge option

If you want to manually squash commits in a pull request, refer to fontno's answer.