Git的使用場(chǎng)景

以此文記述工作中遇到的Git問(wèn)題,及其解決方法。

恢復(fù)被同事誤刪的文件

場(chǎng)景:同事認(rèn)為文件 useful.js 在當(dāng)前項(xiàng)目中沒(méi)有用,把它刪了,提交代碼之后,其他同事又提交了好幾次代碼。
解決方法

S1 查看哪個(gè) commit 刪除了該文件
// git log [--] <path>
// Show only commits that are enough to explain how the files
// that match the specified paths came to be.
// To prevent confusion with options and branch names, paths may
// need to be prefixed with "--" to separate them from options 
// or refnames.
git log -- path_to/useful.js

S2 找到 useful.js 被刪除提交之前的 [commit]

S3 執(zhí)行g(shù)it命令恢復(fù)文件
// After running git reset <paths> to update the index entry, you can
// use git-checkout to check the contents out of the index to the
// working tree. Alternatively, using git-checkout and specifying a
// commit, you can copy the contents of a path out of a commit to the
// index and to the working tree in one go.
git reset [commit] path_to/useful.js
git checkout -- path_to/useful.js

// display the deleted file
git ls-files --deleted
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容