在github中的pull请求之前,将所有提交压缩为一个

时间:2015-07-28 06:25:12

标签: git github

我需要将我的所有提交放在一个名为初始快照的提交中,以便在github Repo中发布它,我知道为了这样做我遵循this

我的第一个问题是我想与外部存储库共享它,并说:

  

提醒一句:只对尚未推送的提交执行此操作   外部存储库。如果其他人的工作基于提交的工作   你要删除,可能会发生很多冲突。只是不要   如果已与他人共享,请重写您的历史记录。

如果发生了什么我想把我的所有提交放在一个提交中,如何做到这一点:

我也发现了这个:

# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch # this may be necessary (depending on your git config) to receive updates on origin/master
git pull

# Merge the feature branch into the master branch.
git merge feature_branch

# Reset the master branch to origin's state.
git reset origin/master

# Git now considers all changes as unstaged changes.
# We can add these changes as one commit.
# Adding . will also add untracked files.
git add --all
git commit

但没有任何反应:

$ git reset  origin/master
$ git add --all
$ git commit -m "initial snapshot"
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

1 个答案:

答案 0 :(得分:19)

最简单的方法是使用rebase命令。

想象一下,你有这个存储库:

$> git log --oneline
af28aeb Another test
a680317 Try something
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a

因此,您已对提交af28aeb Another testa680317 Try something进行了一些测试。我们想在d93792b Edit both files之后压缩它们以清理存储库。

要做到这一点,命令将是 git rebase -i d93792b

-i表示要以交互模式进入,d93792b是我们想要吸收前一个的提交哈希值。

注意:如果您想要像第一次提交那样压缩所有提交,则必须使用git rebase --root -i

该命令将向您显示:

pick a680317 Try something
pick af28aeb Another test

# Rebase d93792b..af28aeb onto d93792b (       2 TODO item(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

你必须告诉rebase命令你想做什么。在这种情况下,我建议你改写第一次提交并压缩第二次提交,如下所示:

reword a680317 Try something
squash af28aeb Another test

# Rebase d93792b..af28aeb onto d93792b (       2 TODO item(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

然后将打开您的文本编辑以设置新的提交消息。

Fix bug

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jul 28 08:40:04 2015 +0200
#
# rebase in progress; onto d93792b
# You are currently editing a commit while rebasing branch 'master' on 'd93792b'.
#
# Changes to be committed:
#       new file:   c
#

现在您必须git commit --amendgit rebase --continue才能完成此过程。

您的存储库将显示如下:

$> git log --oneline
5f98806 Fix bug
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a