以此文記述工作中遇到的Git問題,及其解決方法碉渡。
恢復(fù)被同事誤刪的文件
場景:同事認(rèn)為文件 useful.js 在當(dāng)前項目中沒有用聚谁,把它刪了,提交代碼之后滞诺,其他同事又提交了好幾次代碼形导。
解決方法
S1 查看哪個 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