简体   繁体   English

git壁球数次提交

[英]git squash several commits into one

I want to squash the last 12 commits into one. 我想将最后的12个提交压缩为一个。 I tried this command but get a fatal error: 我尝试了此命令,但收到致命错误:

>git rebase -i HEAD~12
fatal: Needed a single revision
invalid upstream HEAD~12

I have one remote repository, I already pushed some commits but it does not really matter because I pushed it to heroku, so this code is not shared with other users. 我有一个远程存储库,我已经推送了一些提交,但这并不重要,因为我将其推送到了heroku,因此该代码未与其他用户共享。 I mentioned it because I know that rewrite history is not good when some commits have been already committed. 我之所以提到它,是因为我知道当某些提交已经提交时,重写历史记录就不好了。

>git remote
heroku

The security and heroku branch are already merged into master : securityheroku分支已经合并到master

>git branch
  heroku-setup
* master
  security

My commits look like that: 我的提交看起来像这样:

>git log --pretty=oneline
04d85b2959b73daff8ce0d49002c81469748798d Switch to Google OAUth 2.0
862cd0f7a9243d602dbeb1d670c08f20a1b832d8 Switch to Google OAUth 2.0
b25a48a657b9bf5087eaa9c1821163d0746b5ac2 Fix mongodb config
bd4c95dd25056a8a8bc6ed97faf01c7007d3f598 Setup Heroku
c82762c29fc1e5563082d710a1d241ce3ec7681f Setup Heroku
976d2baabe386eb1135e7789be167b081d40eeb0 Setup Heroku
e7ae7dd8755e75fc2bfd4bee460ad7f73b6e8ae4 Setup Heroku
6a389b55b782f37daa1c7434b68fe48100bb79e2 Setup Heroku
c26583bcf92f383c66526a29ee5642c3123b650e Setup Heroku
f7d36cf7215de13ade5ff537c3954f99f05506bd Setup Heroku
1110ed9efc60a27ac6334fb5987110de27983dcc Security with Passport
073d3ba79b727e2dc938aa4345f144baa66e05e3 Security with Passport
b82898acf97cc52f94a08e13388a7641460d8ef2 RequireJS + Nested Layout
8d30a5444244229c443354d7b448098d8ead4083 Project Organization

Basically I want my history to be 基本上我希望我的历史是

<new SHA> Security with Passport
b82898acf97cc52f94a08e13388a7641460d8ef2 RequireJS + Nested Layout
8d30a5444244229c443354d7b448098d8ead4083 Project Organization

The error you got is not terribly obvious but I believe I know what caused it. 您遇到的错误并不十分明显,但我相信我知道是什么原因造成的。

With HEAD~12 you're asking git to follow 12 first-parent links. 使用HEAD~12您要git跟随12个第一个父级链接。 Meanwhile, with git log --pretty=oneline you get shown parent links ... all parent links, not just first-parent links. 同时,通过git log --pretty=oneline您可以看到父链接…… 所有父链接,而不仅仅是第一父链接。

If you add --graph to your git log command, you will see that some of these commits are on different paths through the commit graph. 如果将--graph添加到git log命令中,您将看到其中一些提交位于提交图的不同路径上。 The following is meant only as an illustration, not a proper analysis of the above (there's not enough information to do a proper analysis), but let's say the graph looks like this: 以下内容仅供参考,并非对上述内容进行适当的分析(没有足够的信息来进行适当的分析),但可以说该图形如下所示:

A - B - C1 - D1 - E1 ----- H - I   <-- HEAD=master
      \                      /
        C2 - D2 - E2 ----- G       <-- security
           \             /
             D3 - E3 - F           <-- heroku-setup

This graph contains 14 nodes total (as does your sample git log output). 该图总共包含14个节点(示例git log输出也是如此)。 Assuming the first parent of each merge commit ( I , G , and F ) corresponds to the horizontal direction (and the second to the down-and-left direction), starting from I , commit B is master~5 , and hence HEAD~5 . 假设每个合并提交( IGF )的第一个父级对应于水平方向(第二个并沿左右方向),从I开始,提交Bmaster~5 ,因此HEAD~5 Start at node I and count steps to the left: 1 ( H ), 2 ( E1 ), 3 ( D1 ), 4 ( C1 ), 5 ( B ). 从节点I开始,向左数步:1( H ),2( E1 ),3( D1 ),4( C1 ),5( B )。

HEAD~12 means to start at node I and count leftward 12 steps. HEAD~12表示从节点I开始并向左计数12步。 What happens when you try to move left from commit A ? 当您尝试从提交A向左移动时会发生什么?

Note that while I drew this graph left-to-right, with the root-most commit A on the left and the tip commit HEAD on the right, git log --graph draws it more or less bottom to top, by default (various options can shuffle the node ordering). 请注意,虽然我从左到右绘制了这张图,最根的提交A在左边,尖端的提交HEAD在右边,但是git log --graph将它从下到上绘制(默认选项可以改变节点的顺序)。 With no options at all, git log sorts by reverse chronological order, but using --graph sets --topo-order unless overridden by some other ordering option. 根本没有任何选项, git log按时间倒序排序,但是使用--graph设置--topo-order除非被其他一些排序选项覆盖。

You can also supply --first-parent to git log to instruct it to look only at each first-parent link (the ones you can name using HEAD~ n ). 您也可以提供--first-parentgit log指示它仅仅在每个第一父链接(那些你可以使用命名HEAD~ n )。 See the documentation for git log , or for git rev-list . 请参阅git loggit rev-list的文档。 Git commands generally either take a single revision, which is handled by git rev-parse , or—as with git log —a list of revisions, which is handled by git rev-list . Git命令通常采用单个修订版,由git rev-parse处理,或者(与git log修订版列表,由git rev-list处理。 The documentation under gitrevisions is also useful. gitrevisions下的文档也很有用。

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

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