简体   繁体   English

Git Pull Origin Master删除隐藏文件(来自.gitignore)

[英]Git pull origin master deleted hidden files (from .gitignore)

When pulling from the Github repo, would it just merge files to the local Git, or its going to delete everything and just take the files from the remote repo? 从Github存储库中提取文件时,它会只是将文件合并到本地Git中,还是要删除所有内容并从远程存储库中获取文件?

Package.json and config.js were deleted after doing git pull origin master while they were ignored in the .gitignore. Package.json和config.js在执行git pull origin master之后被删除,但在.gitignore中被忽略。

Where can I find those files? 在哪里可以找到这些文件? Should I re-create them? 我应该重新创建它们吗?

git pull is equivalent to git fetch + git merge . git pull等效于git fetch + git merge That means it does not simply override your local files. 这意味着它不会简单地覆盖本地文件。 For reference, see doc here: 供参考,请参阅此处的文档:

https://git-scm.com/docs/git-pull https://git-scm.com/docs/git-pull

Everything that was once in life committed can be retrieved. 一生中曾经犯下的一切都可以检索。 The command git reflog will print you the history of your changes locally. git reflog命令将在本地打印您的更改历史记录。 So you can do git checkout some_commit_hash to go back in history. 因此,您可以执行git checkout some_commit_hash来返回历史记录。 See reference: 请参阅参考:

https://git-scm.com/docs/git-reflog https://git-scm.com/docs/git-reflog

DISCLAIMER: Don't do git push -f to override the remote if you don't know 100% what you're doing. 免责声明:如果您不知道100%正在做什么,请不要执行git push -f覆盖远程。

Only under certain circumstances will git pull origin master remove a file from the work tree, and it should be the case that the file is recoverable under those circumstances. 只有在某些情况下, git pull origin master才会从工作树中删除文件,并且在这种情况下应该可以恢复该文件。 I think the crux of the question is here: 我认为问题的症结在这里:

Package.json and config.js were deleted after doing git pull origin master while they were ignored in the .gitignore. Package.json和config.js在执行git pull origin master之后被删除,但在.gitignore中被忽略。

(emphasis addeD). (强调地址)。 But .gitignore has a very narrow function - it prevents untracked files from accidentally becoming tracked. 但是.gitignore功能非常狭窄-它可以防止未跟踪的文件被意外跟踪。 It does not enable changes to tracked files to be ignored. 不能使对跟踪文件的更改被忽略。 If package.json and config.js were tracked files, and an upcoming commit on origin/master deletes them, adding them to .gitignore does not protect them from deletion when integrating those upcoming commits. 如果package.jsonconfig.js是跟踪文件,并且对origin/master的即将提交的提交将其删除,则将它们添加到.gitignore不能在集成这些即将提交的提交时保护它们免受删除。

This can be a pain in the neck if you have a file in the repo, and then decide you want to add it to .gitignore and remove it from the repo, because even if you add the files to .gitignore , this does nothing until the commit that removes the files; 如果您在存储库中有一个文件,然后决定将其添加到.gitignore并将其从存储库中删除,这可能会让人感到痛苦,因为即使将文件添加到.gitignore ,它也不会执行任何操作删除文件的提交; and the commit that removes the files will... well... remove the files, before the .gitignore entry is effective. 并且删除文件的提交将.... gitignore条目生效之前删除文件。

But , for git to go along with deleting them from the work tree should mean that (1) you had not locally committed any changes to them (as this would cause a conflict), and (2) you did not have any locally-uncommitted changes to them (because git would warn you that you need to undo or stash local changes before overwriting them). 但是 ,对于git并将其从工作树中删除,这意味着(1)您尚未在本地对它们进行任何更改(因为这会导致冲突),并且(2)您没有在本地未提交任何更改更改(因为git会警告您,您需要在覆盖它们之前撤消或隐藏本地更改)。

So you should be able to retrieve the files from an older commit. 因此,您应该能够从较早的提交中检索文件。

git checkout master@{1} -- package.json config.js

would likely work (taking advantage of the reflog) right after the pull . pull之后可能会起作用(利用reflog)。 Or if the reflog isn't in the right shape, substitute any expression that resolves to a commit that still has the files. 或者,如果reflog的格式不正确,则替换为可解析为仍包含文件的提交的任何表达式。 (Maybe master^ ... it depends on what all is in the repo's history.) (也许是master^ ...这取决于回购记录中的全部内容。)

And if that's what's going on, then it's a one-time annoyance. 如果那是正在发生的事情,那就是一次性的烦恼。 The files disappear when updating from a commit that had the file, to a commit that doesn't have the file. 该文件一个更新的时候消失提交了该文件, 提交不具有该文件。 After that, the .gitignore entry really will keep the file untracked and further pulls will work around it (unless the file gets re-added to a later commit, but that's another story). 之后, .gitignore条目实际上将使该文件保持未跟踪状态,并进一步处理该文件(除非将文件重新添加到以后的提交中,但这是另一回事)。

Here's some additional background if it helps: 如果有帮助,这里还有一些其他背景:

git pull origin master is a shortcut for updating the local repo from origin (a remote), and then integrating the changes from origin s version of master (a branch) into the currently checked-out version. git pull origin master是从origin (远程)更新本地存储库,然后将originmaster (分支)版本的更改集成到当前已签出版本的快捷方式。

The first step of any pull is a fetch . 任何pull第一步都是fetch A fetch does two things: fetch有两件事:

It maps refs from the remote into the local repo according to a configuration value called a "refspec". 它根据称为“ refspec”的配置值将引用从远程映射到本地存储库。 That may sound like gibberish if you're not familiar with all the git lingo, but by default it just means that for each branch in the remote, a "remote tracking ref" is created in the local repo. 如果您不熟悉所有的git lingo,这听起来像胡言乱语,但是默认情况下,这仅意味着对于远程服务器中的每个分支,都会在本地存储库中创建“远程跟踪引用”。 For example, if origin has a branch called master , then you get a local ref called origin/master . 例如,如果origin有一个名为master的分支,那么您将获得一个名为origin/master的本地引用。

It also downloads enough objects from the remote to fill in the history of the updated/added refs, and adds those to the local database. 它还从远程下载足够的对象,以填写更新/添加的引用的历史记录,并将这些对象添加到本地数据库。 To clarify, git has three types of storage. 为了明确起见,git具有三种存储类型。 The files you work on are in the working tree. 您正在处理的文件在工作树中。 The are that contains history (commits), where interaction with remotes occurs, is the database. 包含历史记录(提交)的与数据库进行交互的是数据库。 (And the index is a bridge between the two.) So a fetch only affects the database, and it only adds objects to the database. (索引是两者之间的桥梁。)因此,访存仅影响数据库,并且仅对象添加到数据库。 So far your working files are entirely uninvolved in the process. 到目前为止,您的工作文件完全不参与该过程。

The next thing fetch does is to integrate some changes into the commit you have checked out. fetch的下一步是将某些更改集成到已签出的提交中。 You specified the origin s master branch as the source of changes to integrate, and by default the integration is done by simply merging - so that the result of integrating the changes appears in a new merge commit at the tip of your branch, and in the index, and in the work tree. 您将originmaster分支指定为要集成的更改的来源,并且默认情况下,集成是通过简单地合并完成的-以便将更改集成的结果显示在分支尖端的新合并提交中,并在索引,并在工作树中。

So this is where it's possible for a file to be removed, if the changes being merged include deletion of the file (and if there are no conflicting local changes). 因此,如果要合并的更改包括文件的删除(并且没有冲突的本地更改),则可以在此处删除文件。

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

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