简体   繁体   English

如何重做压缩分支上的一些选定提交

[英]How to redo squash some selected commits on branch

How to redo or undo squash some selected commits on a branch, in simple word my question is如何重做或撤消压缩分支上的某些选定提交,简而言之,我的问题是

  1. first time第一次

commits is ABCDEFG提交是 ABCDEFG

  1. after squash some commits they are在压缩了一些提交之后,它们是

commits is AB-CD-EF-G提交是 AB-CD-EF-G

  1. but now i need to redo this second step, and squash commits like this way step但现在我需要重做这第二步,并像这种方式那样压缩提交
 commits is AB-CD-E-FG

Is there something I am wrong to let me correct.有什么我错了让我纠正。 I am just trying to simplify my query我只是想简化我的查询

The action you did to go from 1. to 2. has rewritten your history, so G in 2. does not have the same hash as G in 1.您从1.2.对 go 所做的操作已经改写了您的历史记录,因此2.中的G与 1. 中的G不具有相同的 hash 1.

Re-taking your notation:重新使用你的符号:

  1. first time第一次

commits is ABCDEFG提交是 ABCDEFG

  1. after squash some commits they are在压缩了一些提交之后,它们是

commits is AB-C'-D'-EF-G'提交是 AB-C'-D'-EF-G'


To undo the rewrite, you need to get the hash of your original G commit (not G' ).要撤消重写,您需要获取原始G提交(不是G' )的 hash 。

Your local git repo should still have it in its reflog:您本地的 git 存储库仍应在其 reflog 中包含它:

# in the following list of commits, spot the one that would match your initial commit :
git reflog

# you will have a shorter list if you look at the reflog of your working branch :
git reflog my/branch

Once you have that hash:一旦你有了 hash:

  • make sure you have a clean repo (if you have local changes: stash them or discard them),确保你有一个干净的仓库(如果你有本地更改:存储它们或丢弃它们),
  • run git reset --hard <that hash> ,运行git reset --hard <that hash>
  • and re-do your squashing.并重新做你的挤压。

mandatory warning about git reset --hard :关于git reset --hard的强制警告

git reset --hard is one of those few destructive git commands: if you have changes on your disk (on tracked files) that aren't stored in git, this command will forcefully drop these changes, and re-set the files content to the target commit's content. git reset --hard是为数不多的破坏性 git 命令之一:如果您的磁盘(跟踪的文件)上有未存储在 git 中的更改,则此命令将强制删除这些更改,并重新设置这些文件的内容目标提交的内容。

This is why it is advised to look at your local changes first, and know for sure that the changes you discard are not important.这就是为什么建议首先查看您的本地更改,并确定您丢弃的更改并不重要。 The way to keep them is easy: just create a commit or a stash -- they will at least be reachable from the reflog afterwards.保存它们的方法很简单:只需创建一个提交或存储——它们至少可以在之后从 reflog 中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM