【问题标题】:When rebasing, why doesn't git add --patch work?变基时,为什么 git add --patch 不起作用?
【发布时间】:2020-07-08 12:43:05
【问题描述】:

在 git 存储库中,我有一个包含大型提交的文件,我想将其分成多个提交。我正在尝试按照this answer 的说明进行操作,但遇到了问题。 git status 显示 file.js 未被跟踪,但 git add -p file.js 响应“无更改”。 我做了一个github reposiotry 来说明我的意思。

在这个特定的存储库中,我想将提交 92cc4839abe2bfe54981d8227c17acf07c24c84b 分成几个提交,每个提交都向 file.js 添加一个函数。但是,我发现的唯一有用的说明失败了。

如何重现:

  1. git clone https://github.com/jgibbons94/rebase-error.git
  2. cd rebase-error/
  3. git rebase -i HEAD~2 并标记提交 92cc483 以进行编辑。保持其他一切不变。
  4. git reset HEAD~
  5. git statuslists file.js 未被跟踪。
  6. git diff file.js 没有变化。
  7. git add -p file.js 回复“没有变化”。

这是已知解决方法的已知问题吗?我是不是误会了什么?我们将不胜感激。

【问题讨论】:

    标签: git git-rebase git-diff git-add


    【解决方案1】:

    问题出在这里:

    1. git reset HEAD~
    2. git status lists file.js 没有被跟踪。

    提交92cc483 添加该文件,所以当您使用git reset 退出92cc483 的副本时,这已从Git 的索引中删除该文件.

    提醒:indexstaging area(同一事物的两个术语)保存每个文件的副本,该副本将进入 next 提交。它最初是当前提交的副本。所以git reset HEAD~1(或任何等价物)意味着取出索引中的所有内容,然后放入HEAD~1中的所有内容。文件file.js不在之前的提交中,所以现在它不在索引中

    git add -p 命令需要在索引中有一些东西来修补。索引中的一个空文件就足够了,git add -N 会创建这样一个条目,所以:

    git add -N file.js
    

    会让你跑git add -p file.js

    这并不是那么有用,因为整个file.js 现在将显示为补丁。您不妨在编辑器中打开file.js,剪掉大部分内容,将其写回,然后在编辑器仍处于打开状态时运行git add file.js,然后撤消剪掉的部分,以便您拥有整个事物。您现在在索引中拥有您想要的file.js 的副本:

    $ vi file.js        # open file.js in editor
    [snip]
    :w
    ^Z                  # suspend editor - or, open another window to run git add
    $ git add file.js
    $ fg                # resume editor
    [undo snippage]
    :x                  # write and exit
    

    这一切的结果是:

    $ git status
    interactive rebase in progress; onto 85aa6aa
    Last command done (1 command done):
       edit 92cc483 A really big commit
    Next command to do (1 remaining command):
       pick 3891479 Add description.txt
      (use "git rebase --edit-todo" to view and edit)
    You are currently splitting a commit while rebasing branch 'master' on '85aa6aa'.
      (Once your working directory is clean, run "git rebase --continue")
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   file.js
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   file.js
    

    我现在可以git commit、恢复我的编辑器、剪断(这次少)、写入、暂停、git addgit commit、恢复编辑器和撤消片段等,直到我完成所有提交对于每个功能。无需为笨重的git add -p 烦恼。

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2023-02-23
      • 1970-01-01
      • 2016-06-23
      • 2016-01-24
      • 2021-01-02
      • 2012-04-19
      • 2018-08-10
      • 1970-01-01
      相关资源
      最近更新 更多