Git Rebase或壁球? [英] Git Rebase or Squash?

查看:59
本文介绍了Git Rebase或壁球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从develop创建了一个功能分支,该功能分支包含大约20的一堆提交.在将功能分支合并到develop之前,我想将所有提交转换为一个提交.哪种方法是完成git squash或变基的最佳方法,以及如何做到这一点?

I have created a feature branch from develop and that feature branch contains bunch of commits for approximately 20. Before merging the feature branch to the develop I want to convert all commits into one. Which one is the best way to accomplish this task git squash or rebase and how to do it?

推荐答案

如果要将一堆提交压缩到 feature 分支上的单个提交中,则可以按以下步骤进行操作,但是如果这样做一堆提交已经被推送到远程了,您必须确保没有人在 feature 分支上工作,因为您必须强制推送到该分支,这意味着重写了远程历史记录(通常不想在公共存储库上工作,或者有更多的人在该分支上工作.

If you want to squash your bunch of commits into a single commit on feature branch you can do it as follows, but if this bunch of commits is already pushed to remote you have to be sure that no one is working on the feature branch because you'll have to force push to that branch and this implies rewriting remote history (something you usually don't want to do if working on a public repository or more people are working on that branch).

  1. git checkout FEATURE(其中FEATURE必须替换为您的 feature 分支的名称.
  2. git rebase -i HEAD~21这是假设您要压缩20个最近的提交.如果您拥有最早提交的HASH,也可以执行git rebase -i HASH^
  3. 现在,您必须在除第一行以外的所有行上将pick替换为squash(或s)并保存文件
  4. 您必须为此新提交选择一个提交消息. Git向您显示您正在压榨的提交消息
  5. 如果原始提交已经被推送到远程,则您必须强制推送,因为您正在重写远程历史记录,所以用新的提交替换了这20个提交.如果您要与该分支上的更多人一起工作,通常就不需要这样做. git push -f
  1. git checkout FEATURE (where FEATURE has to be replaced by the name of your feature branch.
  2. git rebase -i HEAD~21 This is assuming you want to squash the 20 most recent commits. If you have the HASH of the oldest commit you can also do git rebase -i HASH^
  3. Now you have to replace pick by squash (or s) on all lines except the first one and save the file
  4. You have to choose a commit message for this new commit. Git shows you the messages of the commits your are squashing
  5. If the original commits had been already pushed to remote, you'll have to force push, because you are rewriting remote history, replacing those 20 commits by a new one. This is something you usually don't want to do if you are working with more people on that branch. git push -f

这篇关于Git Rebase或壁球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆