在 git 工作树中,我有一个文件“foo”。我对该文件进行了重大修改,然后将其重命名为“bar”。Git 将此检测为已删除/新文件。根据这个答案,为了让 git 检测到它们是同一个文件,应该先提交移动,然后是修改。鉴于文件已被修改和移动(但尚未暂存或提交),首先提交移动操作然后进行修改(确保我不会在此过程中丢失我的修改)要遵循的程序是什么?
同样,如果移动/修改已经在单个提交中提交,那么执行此操作的程序是什么?
老实说,最安全的做法是:
$ mv bar bar.safe # move bar someplace safe
$ git checkout foo # get the old copy of foo
$ git mv foo bar # tell git about the move
$ git commit -m 'I moved foo to bar'
$ mv bar.safe bar
$ git add bar
$ git commit -m 'But bar is totally changed now'
Run Code Online (Sandbox Code Playgroud)
如果已经提交了移动/修改,最安全的做法是撤消提交(在默认--mixed模式下,更改索引但保持工作目录原样):
$ git reset HEAD^
Run Code Online (Sandbox Code Playgroud)
然后使用上面的步骤。
技术上可以使用git update-index独立于工作目录的内容直接操作索引(见--cacheinfo参数),但我认为上面的方法更直接。