以此文记述工作中遇到的Git问题,及其解决方法。
恢复被同事误删的文件
场景:同事认为文件 useful.js 在当前项目中没有用,把它删了,提交代码之后,其他同事又提交了好几次代码。
解决方法
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 执行git命令恢复文件
// 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