Git:如何压缩分支上的所有提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25356810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:19:39  来源:igfitidea点击:

Git: How to squash all commits on branch

gitversion-control

提问by user3803850

I make new branch from masterwith:

我从以下位置创建新分支master

git checkout -b testbranch

I make 20 commits into it.

我对它进行了 20 次提交。

Now I want to squash those 20 commits. I do that with:

现在我想压缩这 20 个提交。我这样做:

git rebase -i HEAD~20

What about if I don't know how many commits? Is there any way to do something like:

如果我不知道有多少提交怎么办?有没有办法做这样的事情:

git rebase -i all on this branch

回答by VonC

Another way to squash all your commits is to reset the index to master:

压缩所有提交的另一种方法是将索引重置为 master:

 git checkout yourBranch
 git reset $(git merge-base master yourBranch)
 git add -A
 git commit -m "one commit on yourBranch"

This isn't perfect as it implies you know from which branch "yourBranch" is coming from.
Note: finding that origin branch isn't easy/possible with Git(the visual way is often the easiest, as seen here).

这并不完美,因为它意味着您知道“yourBranch”来自哪个分支。
注意:使用 Git找到原始分支并不容易/不可能视觉方式通常是最简单的,如这里所见)。



EDIT: you will need to use git push --force

编辑:您将需要使用 git push --force



Karlotcha Hoaadds in the comments:

Karlotcha Hoa在评论中补充道:

For the reset, you can do

对于重置,您可以执行

git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD)) 

[That] automatically uses the branch you are currently on.
And if you use that, you can also use an alias, as the command doesn't rely on the branch name.

[那] 自动使用您当前所在的分支。
如果您使用它,您还可以使用别名,因为该命令不依赖于分支名称

回答by Eevee

What you're doing is pretty error-prone. Just do:

你在做什么很容易出错。做就是了:

git rebase -i master

which will automatically rebase only your branch's commits onto the current latest master.

这将仅自动将您的分支的提交变基到当前最新的主节点上。

回答by shanky

Checkout the branch for which you would like to squash all the commits into one commit. Lets say its called,feature_branch.

签出您希望将所有提交压缩为一个提交的分支。可以说它叫做,feature_branch

git checkout feature_branch

git checkout feature_branch

Step 1:

第1步:

Do a soft reset of your origin/feature_branchwith your local masterbranch (depending on your needs, you can reset with origin/master as well). This will reset all the extra commits in your feature_branch, but without changing any of your file changes locally.

origin/feature_branch使用本地master分支进行软重置(根据您的需要,您也可以使用 origin/master 进行重置)。这将重置 中的所有额外提交feature_branch,但不会在本地更改任何文件更改。

git reset --soft master

git reset --soft master

Step 2:

第2步:

Add all of the changes in your git repo directory, to the new commit that is going to be created. And commit the same with a message.

将 git repo 目录中的所有更改添加到将要创建的新提交。并使用消息提交相同的内容。

git add -A && git commit -m "commit message goes here"

git add -A && git commit -m "commit message goes here"

回答by WaZaA

Another simple way to do this: go on the origin branch and do a merge --squash. This command don't do the "squased" commit. when you do it, all commit messages of yourBranch will be gathered.

另一种简单的方法是:在 origin 分支上做一个merge --squash. 此命令不执行“squased”提交。当你这样做时,你的分支的所有提交消息都将被收集。

$ git checkout master
$ git merge --squash yourBranch
$ git commit # all commit messages of yourBranch in one, really useful
 > [status 5007e77] Squashed commit of the following: ...

回答by mles

Assuming you were branching from the master, you don't need to enter yourBranchinto the reset step all the time:

假设你是从 master 分支,你不需要一直进入yourBranch重置步骤:

git checkout yourBranch
git reset --soft HEAD~$(git rev-list --count HEAD ^master)
git add -A
git commit -m "one commit on yourBranch"

Explanation:

说明

  • git rev-list --count HEAD ^mastercounts the commits since you made your feature branch from the master, f.ex. 20.
  • git reset --soft HEAD~20will make a soft reset of the last 20 commits. This leaves your changes in the files, but removes the commits.
  • git rev-list --count HEAD ^master计算自您从 master 分支创建功能分支以来的提交,f.ex。20.
  • git reset --soft HEAD~20将对最近 20 次提交进行软重置。这会将您的更改保留在文件中,但会删除提交。

Usage:

用法

In my .bash_profile I have added an alias for gisquashto do this with one command:

在我的 .bash_profile 中,我添加了一个别名gisquash来使用一个命令执行此操作:

# squash all commits into one
alias gisquash='git reset --soft HEAD~$(git rev-list --count HEAD ^master)'

After reseting and committing you need to do a git push --force.

重置并提交后,您需要执行git push --force.

Hint:

提示

If you're using Gitlab >= 11.0 you don't need to do this anymore as it has a squashing optionwhen merging branches. Gitlab Squashing option

如果您使用 Gitlab >= 11.0,则无需再执行此操作,因为它在合并分支时具有 压缩选项Gitlab 压缩选项

回答by Travis Reeder

Based on reading several Stackoverflow questions and answers on squashing, I think this is a good one liner to squash all commits on a branch:

基于阅读有关压缩的几个 Stackoverflow 问题和答案,我认为这是压缩分支上所有提交的好方法:

git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master

This is assuming master is the base branch.

这是假设 master 是基本分支。

回答by Marcin Szymczak

Solution for people who prefer clicking:

对于喜欢点击的人的解决方案:

  1. Install sourcetree(it is free)

  2. Check how your commits look like. Most likely you have something similar to this enter image description here

  3. Right click on parentcommit. In our case it is master branch.

  1. 安装sourcetree(它是免费的)

  2. 检查您的提交的样子。很可能你有类似的东西 在此处输入图片说明

  3. 右键单击提交。在我们的例子中,它是 master 分支。

enter image description here

在此处输入图片说明

  1. You can squash commit with previous one by clicking a button. In our case we have to click 2 times. You can also change commit message enter image description here

  2. Results are awesome and we are ready to push! enter image description here

  1. 您可以通过单击一个按钮来压缩前一个提交。在我们的例子中,我们必须点击 2 次。您还可以更改提交消息 在此处输入图片说明

  2. 结果很棒,我们准备好推动了! 在此处输入图片说明

Side note: If you were pushing your partial commits to remote you have to use force push after squash

旁注:如果您将部分提交推送到远程,则必须在壁球后使用强制推送

回答by pranav

Another solution would be to save all commit logs to a file

另一种解决方案是将所有提交日志保存到一个文件中

git log > branch.log

git 日志> branch.log

Now branch.log will have all commit ids since beginning.. scroll down and take the first commit (this will be difficult in terminal) using the first commit

现在 branch.log 将拥有自开始以来的所有提交 ID.. 向下滚动并使用第一次提交进行第一次提交(这在终端中会很困难)

git reset --soft

git reset --soft

all commits will be squashed

所有提交都将被压缩

回答by Marcus

Git reset, as mentioned in many answers before, is by far the best and simplest way to achieve what you want. I use it in the following workflow:

正如之前许多答案中提到的那样,Git reset 是迄今为止实现您想要的最佳和最简单的方法。我在以下工作流程中使用它:

(on development branch)

(在开发分支上)

git fetch
git merge origin/master  #so development branch has all current changes from master
git reset origin/master  #will show all changes from development branch to master as unstaged
git gui # do a final review, stage all changes you really want
git commit # all changes in a single commit
git branch -f master #update local master branch
git push origin master #push it

回答by ItaiRoded

All this git reset, hard, soft, and everything else mentioned here is probably working (it didn't for me) if you do the steps correctly and some sort of a genie.
If you are the average Joe smo, try this:
How to use git merge --squash?

所有这些 git reset、hard、soft 以及这里提到的所有其他内容都可能有效(对我来说不是),如果您正确地执行这些步骤并且是某种精灵。
如果您是普通的 Joe smo,请尝试以下操作:
如何使用 git merge --squash?


Saved my life, and will be my go to squash, been using this 4 times since I found out about it. Simple, clean and basically 1 comamnd. In short:


If you are on a branch lets call it "my_new_feature" off develop and your pull request has 35 commits (or however many) and you want it to be 1.

A. Make sure your branch is up to date, Go on develop, get latest and merge and resolve any conflicts with "my_new_feature"
(this step really you should take as soon as you can all the time anyway)

B. Get latest of develop and branch out to a new branch call it "my_new_feature_squashed"


救了我的命,我会去壁球,自从我发现它以来已经使用了 4 次。简单,干净,基本上是 1 个命令。简而言之:


如果您在一个分支上,让我们在开发之外将其称为“my_new_feature”,并且您的拉取请求有 35 次提交(或任何数量),并且您希望它是 1。

A. 确保您的分支是最新的,继续开发,获取最新版本并合并并解决与“my_new_feature”的任何冲突
(无论如何,您确实应该尽快执行此步骤)

B. 获取最新的开发版本并分支到一个新分支,称为“my_new_feature_squashed”

C. magic is here.
You want to take your work from "my_new_feature" to "my_new_feature_squashed"
So just do (while on your new branch we created off develop):
git merge --squash my_new_feature

C.魔法来了。
你想把你的工作从“my_new_feature”转移到“my_new_feature_squashed”
所以就这样做(在你的新分支上我们从开发中创建):
git merge --squash my_new_feature

All your changes will now be on your new branch, feel free to test it, then just do your 1 single commit, push, new PR of that branch - and wait for repeat the next day.
Don't you love coding? :)

您的所有更改现在都将在您的新分支上,随时进行测试,然后只需执行该分支的 1 个单次提交、推送、新 PR - 并等待第二天重复。
你不喜欢编码吗?:)