简体   繁体   English

如何找出“git add”。 历史

[英]How to find out “git add .” history

I have work on my project and backup only with "git add.".我在我的项目上工作并仅使用“git add”进行备份。 Unfortunately, I forget to commit my code and I loss the file.不幸的是,我忘记提交我的代码并且我丢失了文件。 Right now, I git clone the previous code I have last time.现在,我 git 克隆了我上次的代码。 How can I track the history on my previous "git add" and able to restore the code I miss?如何跟踪我以前的“git add”的历史记录并能够恢复我错过的代码? Thank you.谢谢你。

git add ed files are stored under .git/objects for a while, even if they have not been committed. git add ed 文件会在.git/objects下存储一段时间,即使它们尚未提交。 You may be able to recover them if it is not GC'd.如果不是 GC,您也许可以恢复它们。

To find out files you lost:要找出丢失的文件:

  1. git fsck to list dangling blobs and trees (means "files and directories not ended up in any commit" in git jargon) git fsck列出悬空的 blob 和树(在 git 行话中表示“文件和目录未在任何提交中结束”)
  2. git cat-file -p SHA1-of-the-file-or-directory to extract them. git cat-file -p SHA1-of-the-file-or-directory来提取它们。

Related links:相关链接:

How can I track the history on my previous "git add" and able to restore the code I miss?如何跟踪我以前的“git add”的历史记录并能够恢复我错过的代码?

You can't.你不能。 Git doesn't track any changes until you commit them. Git 在您提交之前不会跟踪任何更改。 If you still have the directory where you made these changes, you can view the final result there, but you can't get the history of the changes because there is none.如果您仍然有进行这些更改的目录,您可以在那里查看最终结果,但您无法获取更改的历史记录,因为没有。

Unfortunately, git only keeps the history of committed files.不幸的是,git 只保留提交文件的历史。 Even so, unless the commits have been pushed to the remote server, cloning all over again would erase any local history.即便如此,除非提交已被推送到远程服务器,否则再次克隆将删除任何本地历史记录。

As a piece of advice, committing adds a reflog entry which makes it much easier to go back to a previous state.作为一条建议,提交添加了一个 reflog 条目,这使得 go 更容易回到之前的 state。

You should also check the stash functionality.您还应该检查存储功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM