我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
当前回答
git checkout -b temp
git checkout yourbranch
git fetch
git reset --hard origin/master
git merge --squash temp
git commit -m "new message"
最简单的方法。
这将创建一个新的分支,然后将你的分支重置为基础分支,然后在将临时分支合并回我们的分支之前,我们压缩更改并创建一个新的提交
其他回答
git checkout -b temp
git checkout yourbranch
git fetch
git reset --hard origin/master
git merge --squash temp
git commit -m "new message"
最简单的方法。
这将创建一个新的分支,然后将你的分支重置为基础分支,然后在将临时分支合并回我们的分支之前,我们压缩更改并创建一个新的提交
假设你在特征分支上:
在特性分支中找到第一个提交。如果你正在使用gitlab或github,你可以直接在分支中查看它,并从那里复制散列,或者你可以使用以下命令:
Git日志<source_branch>..< feature_branch >——漂亮=格式:% h
执行以下命令:
git reset --soft <base_commit_hash>
git commit --amend --no-edit
现在在这个阶段,在您的本地,您有一个提交,其中包括在所有以前的提交中所做的更改。
回顾它,你需要用力推它。在强制推送之后,所有的更改都将合并到一个提交中,而您的分支将只有1个提交。
在特征分支中强制推送
git push --force
Git重置,正如之前在许多回答中提到的,是迄今为止实现你想要的最好和最简单的方法。我在以下工作流程中使用它:
(有关发展分支)
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
另一种解决方案是将所有提交日志保存到一个文件中
分支> git 日志.log
现在branch.log将拥有自开始以来的所有提交id。向下滚动并进行第一次提交(这在终端中很困难) 使用第一次提交
Git复位-软
所有提交都将被压缩
为了完善一下Caveman的回答,使用git reset——soft <commit>。从文档中,这个命令:
根本不触及索引文件或工作树(但将头部重置为<commit>,就像所有模式一样)。这将使所有更改过的文件都变成“要提交的更改”,就像git状态所显示的那样。
换句话说,它将撤销到<commit>之前的所有提交。但是它不会改变工作目录。您最终会得到所有的更改,这些更改都是未分期和未提交的。就好像那些介入的提交从未发生过一样。
例子:
# on master
git checkout -b testbranch
# make many commits
git reset --soft master
git add .
git commit -m 'The only commit.'
此时,您仍然在testbranch上,它只有一次提交。像往常一样合并到master中。
在我的手中,Caveman回答的第一部分(git rebase -i)并没有压缩提交。