我有一個文件被重命名的提交。是否有可能從舊文件名和提交鍵中獲取新文件名?獲取新文件名
示例:file.php
在承諾5119afae52e5ae08ca8e378e00895d824a179f03
處重命名爲filex.php
。
我在尋找的東西從5119afae52e5ae08ca8e378e00895d824a179f03
和file.php
信息返回filex.php
。
我有一個文件被重命名的提交。是否有可能從舊文件名和提交鍵中獲取新文件名?獲取新文件名
示例:file.php
在承諾5119afae52e5ae08ca8e378e00895d824a179f03
處重命名爲filex.php
。
我在尋找的東西從5119afae52e5ae08ca8e378e00895d824a179f03
和file.php
信息返回filex.php
。
下面的bash腳本將做的工作:
使用:
new_name_of_renamed_file <oldname> <commitid>
new_name_of_renamed_file:
#!/usr/bin/env bash
if [ $# -ne 2 ]
then
echo "Usage: new_name_of_renamed_file <oldname> <commitid>"
exit 1
fi
pattern="^rename from ${1/./\\.}\$"
git diff -M100% "$2"^.."$2"|grep -A1 "$pattern"|tail -n1|sed 's/^rename to //'
注意
git diff -M
命令。oldname
在commitid
的父級中不存在或未在該提交中重命名,則不輸出任何內容。如果你運行:
git log --oneline -- file.php
,你會看到所有修改這個file.php
的提交列表。
當file.php
已被重命名爲filex.php
時,這些提交中的最新提交(最上面的那個提交)。
您可以運行git diff <hash>
來確認這一點,其中<hash>
是您使用git log
找到的提交ID。
(注:這也將工作沒有--oneline
選項; --oneline
選項可以讓你每犯正好一條線,這使得它可能更容易理解整個列表
您也可以嘗試git log --oneline --graph ...
)
感謝您的回答。 :)但它似乎並沒有像預期的那樣工作,因爲這個特定的文件似乎從''file.php''重命名爲''filex.php'',後來又重命名爲''file.php''。但除此之外,我已經有了具體的提交哈希,我只是缺少文件名「filex.php」。 – ScientiaEtVeritas
好的。試試這個:'git log --name-status - file.php'。您應該能夠發現何時刪除或添加此文件。 – LeGEC
這個文件上的'''git diff'''不起作用嗎? – Zooly