git 在 github 中的拉取请求之前,将所有提交压缩为一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31668794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Squash all your commits in one, before a pull request in github
提问by anquegi
I need to put all my commits in a single one called initial snapshot, in order to release it in a github Repo, I know that for doing this I follow this
我需要将我所有的提交放在一个称为初始快照的单个文件中,以便在 github Repo 中发布它,我知道为此我遵循这个
My first question is that I want to share it with a external repository and this say:
我的第一个问题是我想与外部存储库共享它,这说:
A word of caution: Only do this on commits that haven't been pushed an external repository. If others have based work off of the commits that you're going to delete, plenty of conflicts can occur. Just don't rewrite your history if it's been shared with others.
警告:仅对尚未推送到外部存储库的提交执行此操作。如果其他人的工作基于您要删除的提交,则可能会发生大量冲突。如果已与他人共享,请不要重写您的历史记录。
and what happens I want to put all my commits in a single one, how to do this:
发生了什么我想把我所有的提交放在一个单一的提交中,如何做到这一点:
I found also 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
but nothing happens:
但没有任何反应:
$ 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
回答by EnriMR
The easiest way to do that is by using rebase
command.
最简单的方法是使用rebase
命令。
Imagine you have this repository:
想象一下你有这个存储库:
$> git log --oneline
af28aeb Another test
a680317 Try something
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a
So you have done some test with commits af28aeb Another test
and a680317 Try something
. We want to squash them after d93792b Edit both files
to clean the repository.
所以你已经用提交af28aeb Another test
和a680317 Try something
. 我们想在d93792b Edit both files
清理存储库后压缩它们。
To do that the command will be
git rebase -i d93792b
要做到这一点,命令将是
git rebase -i d93792b
Where -i
is to indicate to enter in interactive mode and d93792b
is the commit hash where we want to absorbe the previous one.
where-i
表示以交互模式进入,d93792b
是我们要吸收前一个的提交哈希。
Note: in case you want to squash all your commits like the first one, you have to use git rebase --root -i
注意:如果您想像第一个一样压缩所有提交,则必须使用 git rebase --root -i
That command will show you that:
该命令将显示:
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
You have to tell to rebase command what you want to do. In that case, I suggest you to reword the first commit and squash the second as follow:
你必须告诉 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
Then your text edit will be opened to set the new commit message.
然后将打开您的文本编辑以设置新的提交消息。
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
#
Now you have to git commit --amend
and git rebase --continue
to finish the process.
现在,你必须git commit --amend
和git rebase --continue
完成过程。
And your repository will show like this:
您的存储库将显示如下:
$> git log --oneline
5f98806 Fix bug
d93792b Edit both files
f23cdbd Second commit add b
6f456bc First commit add a