问题出在这里:
- git reset HEAD~
- git status lists file.js 没有被跟踪。
提交92cc483 添加该文件,所以当您使用git reset 退出92cc483 的副本时,这已从Git 的索引中删除该文件.
提醒:index 或 staging 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 add、git commit、恢复编辑器和撤消片段等,直到我完成所有提交对于每个功能。无需为笨重的git add -p 烦恼。