我如何创建一个复制的文件,使它使git理解它来自哪里?如果我只是复制并将其添加到分支中,git无法找出它:
$ git log --oneline --graph --name-status master
* 70f03aa (master) COpying file straight
| A new_file.txt
* efc04f3 (first) First commit for file
A hello_world.txt
$ git diff master:new_file.txt master:hello_world.txt
$ git blame -s master -- hello_world.txt
^efc04f3 1) I am here
^efc04f3 2)
^efc04f3 3) Yes I am
$ git blame -s master -- new_file.txt
70f03aab 1) I am here
70f03aab 2)
70f03aab 3) Yes I am
$ git blame -s --follow master -- new_file.txt
70f03aab 1) I am here
70f03aab 2)
70f03aab 3) Yes I am
这可以通过在一个单独的分支上做一些额外的工作来完成。
git checkout -b rename-branch
git mv a.txt b.txt
git commit -m "Renaming file"
# if you did a git blame of b.txt, it would _follow_ a.txt history, right?
git checkout main
git merge --no-ff --no-commit rename-branch
git checkout HEAD -- a.txt # get the original file back
git commit -m "Not really renaming file"
在旁边使用重命名并在合并我用于问题的原始文件时获得文件,我们得到:
$ git log --oneline --graph master2 --name-status
* 30b76ab (HEAD, master2) Not really renaming
|
| * 652921f Renaming file
|/
| R100 hello_world.txt new_file.txt
* efc04f3 (first) First commit for file
A hello_world.txt
$ git log --oneline --graph --name-status master2 -- new_file.txt
* 652921f Renaming file
A new_file.txt
$ git log --oneline --graph --follow --name-status master2 -- new_file.txt
...
| * 652921f Renaming file
|/
| R100 hello_world.txt new_file.txt
* efc04f3 (first) First commit for file
A hello_world.txt
$ git blame -s master2 -- new_file.txt
^efc04f3 hello_world.txt 1) I am here
^efc04f3 hello_world.txt 2)
^efc04f3 hello_world.txt 3) Yes I am
$ git blame -s master2 -- hello_world.txt
^efc04f3 1) I am here
^efc04f3 2)
^efc04f3 3) Yes I am
的基本原理是,如果你想查看原始文件的历史记录,git可以毫无问题地完成它....如果你想在拷贝上做,那么git将不得不遵循用于移动文件的单独分支(因为这是文件的来源),然后它将能够在时间返回时跳转到原始文件。