代码之家  ›  专栏  ›  技术社区  ›  gangelo

如何修复提交到头部之前的“尾随/前导空格”错误?

  •  0
  • gangelo  · 技术社区  · 6 年前

    首先道歉,我是一个有抱负的git大师,但还不太清楚。。。

    我目前有5个本地提交尚未推送到我的远程分支;前两个禁止我推,因为他们 尾随/前导空格错误 在一些源文件中。

    当我试图推动。。。

    $ git push -u origin my-branch

    我收到以下错误。。。

    $ git push -u origin my-branch
    Counting objects: 124, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (122/122), done.
    Writing objects: 100% (124/124), 23.19 KiB | 3.31 MiB/s, done.
    Total 124 (delta 94), reused 2 (delta 2)
    remote: Resolving deltas: 100% (94/94), completed with 16 local objects.
    remote: bin/pre_receive_hook: failed with exit status 1
    remote: Starting pre_receive_hook.rb at 02:30:52.384
    remote: Updating refs/heads/my-branch from 89e1e6deeec412a914c4cfc4c6dec101b15ba4c5 to 0cd270067efecff743505d6613c2cff07a4f7f14
    remote: Running Conventions Check...
    remote: Starting ConventionCheck at 02:30:53.162
    remote: Checking Commit 0cd270067: OB-221 Remove pry bindings left in spec, remove leading/trailing spaces in files, add newline to end of files
    remote: Checking Commit 1342336cd: OB-221 Remove pry bindings left in spec
    remote: Checking Commit 7dc993531: OB-221 Refactored SoapRequest and CalculateInvoiceTaxRequest to accomodate a more traditional Adapter pattern
    remote:     Trailing/Leading spaces error, use --check in your diff to see the problem: Conventions
    To github.my-site.com:my-site/argh_express.git
     ! [remote rejected]         OB-221-taxware-api-adapter -> OB-221-taxware-api-adapter (pre-receive hook declined)
    error: failed to push some refs to 'git@github.my-site.com:on-site/argh_express.git'
    

    我已将尾随/前导空格错误缩小到提交到前两(2)次提交的源文件,使用

    $ git diff --check -R 7dc993531 bbfd2723d

    产生以下输出(尾随空格由\b表示)

    ROOT/rails/app/models/integration/tax_ware/calculate_invoice_tax_result_model.rb:100: trailing whitespace.
    +\b\b\b\b\b\b\b
    ROOT/rails/spec/models/integration/tax_ware/soap_request_spec.rb:17: trailing whitespace.
    +\b\b
    ROOT/rails/spec/models/integration/tax_ware/soap_request_spec.rb:18: trailing whitespace.
    +  let(:soap_request) do\b
    

    因此,错误的提交如下(以粗体标记)

    • 0CD27067
    • 1342336cd
    • 7dc993531
    • 9ca4f853f <<<尾随空格
    • bbfd2723d <<<尾随空格

    我基本上想

    1. 修复那些源文件(这对我来说没有太大意义,因为文件已随最近的注释而更改)或
    2. 以某种方式推送最近的提交(0cd270067)

    但是,最近的提交并不包含需要推送的所有文件! 如何推动最近的本地提交,包括我需要的所有文件,并放弃以前的所有本地提交?

    2 回复  |  直到 6 年前
        1
  •  1
  •   LightBender    6 年前

    在您的情况下,如果您只想在新提交上进行当前更改,而之前没有推送任何提交,那么交互式回退实际上是过火了。最简单的方法是执行软重置回到以前的版本并进行新的提交:

    git reset --soft bbfd2723d^
    

    这将在您进行提交之前向后移动头部,但保持工作目录和索引不变。然后,您可以像往常一样构建新的提交。

    为了便于参考,git book一章中介绍了此方法 7.7

        2
  •  0
  •   ashmaroli    6 年前

    由于您想丢弃旧的评论,只推送最新的提交, git rebase -I 会帮助你的。

    # activate `git rebase` on the last 5 commits on your branch
    git rebase -i HEAD~5
    

    您现在有以下选择(除了 pick reword ):

    • edit :分别更改提交
    • squash :将提交平坦化为上一个提交,但将两个提交消息合并为一个提交消息
    • fixup :将提交平展到上一个,但放弃当前提交消息

    如果要保留5条提交消息的记录,请选择 南瓜 其他的 修正 如以下示例所示:

    pick  bbfd2723d
    fixup 9ca4f853f
    fixup 7dc993531
    fixup 1342336cd
    fixup 0cd270067
    
    • 请注意,此处的提交相反。。
    • 选择 这个 最老的 提交要调整的内容,并 squash / fixup 后续提交。
    • 您可以选择 重写 而不是 选择 以使最终提交消息反映最终差异。
    • 结果将给您留下一个完全不同SHA的单一未推式提交,并且由于您说空白问题已在较新的提交中解决,因此最终提交将是一个单一的干净提交 可推动的 犯罪