Git的使用场景

以此文记述工作中遇到的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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容