简体   繁体   English

如何在git pull上暂时忽略未跟踪的文件?

[英]How to temporarily ignore untracked files on git pull?

I have some new files that I didn't add to git yet; 我有一些我没有添加到git的新文件; I'm not sure I will, I'm still undecided. 我不确定我会不会,我还是犹豫不决。 But I want to get whatever is latest in the server. 但我想得到服务器中的最新信息。

However, when I do a git pull I get the error error: Your local changes to 'XXXX' would be overwritten by merge. Aborting. 但是,当我执行git pull我收到错误error: Your local changes to 'XXXX' would be overwritten by merge. Aborting. error: Your local changes to 'XXXX' would be overwritten by merge. Aborting. , where XXXX is my new file. ,其中XXXX是我的新文件。

How can I tell git to download the changes and new files from the server, tell me about possible conflicts between what's on the server and my local modifications to files already in the repository, while at the same time not aborting because I have new files locally? 我如何告诉git从服务器下载更改和新文件,告诉我服务器上的内容与我对存储库中已有文件的本地修改之间可能存在的冲突,同时不会中止,因为我在本地有新文件?

I don't want to add them to .gitignore, as I have this situation frequently and I don't want to be adding and removing files from .gitignore all the time for silly stuff... especially because I don't want to run the risk of forgetting about a certain file that in the end I would decide that it has to be added to the repository, so I want git status to keep telling me about them. 我不想将它们添加到.gitignore,因为我经常遇到这种情况,我不想一直在添加和删除.gitignore中的文件,因为我不想这么做...冒着忘记某个文件的风险,我最终决定将它添加到存储库中,所以我希望git status能够不断告诉我这些文件。 Sometimes I run git status -uno when I want to ignore new files, and then sometimes I run git status to see what's new and decide what to keep and what to discard. 有时我想忽略新文件时运行git status -uno ,然后有时我运行git status来查看新内容并确定要保留什么以及丢弃什么。 But I couldn't find an equivalent option for git pull . 但我找不到git pull的等价选项。

My attempts at googling take me to people who want to overwrite their local changes with what's on the repository, or other similar scenarios. 我在谷歌搜索的尝试把我带到那些想要用存储库或其他类似场景中的内容覆盖他们的本地更改的人。 :( After reading the documentation I found that I can do a git fetch to bring my local repository up to date, that didn't yield any errors, but it also didn't bring the changes to my working copy. I can't figure out the next step after that. :-/ In any case, a pull with no errors would be ideal... :(阅读文档后,我发现我可以做一个git fetch来使我的本地存储库更新,这不会产生任何错误,但它也没有将更改带到我的工作副本。我不能弄清楚之后的下一步。: - /无论如何,没有错误的pull将是理想的......

You will have to either stash it first and apply the changes after you have pulled from the remote or reset the current branch to the last commit. 您必须首先存储它并在从远程控制器拉出或将当前分支重置为上次提交后应用更改。 Resetting the branch will cause you to lose your changes. 重置分支将导致您丢失更改。

git stash

// after you have pulled from remote
git stash apply 
// OR
git stash pop

A very good explanation on the difference between apply and pop can be found here 关于applypop之间区别的一个非常好的解释可以在这里找到


You can also choose to reset your current branch with the following command. 您还可以使用以下命令选择重置当前分支。

git reset --hard

As you probably know, git pull is basically git fetch followed by git merge . 你可能知道, git pull基本上是git fetch然后是git merge

The step that is failing is not the git fetch , it's the git merge : 失败的步骤不是git fetch ,而是git merge

 ... overwritten by merge 

Note the last word here is merge . 注意这里的最后一个词是合并 What this means is that your undecided-ness: 这意味着你未定的:

I have some new files that I didn't add to git yet; 我有一些我没有添加到git的新文件; I'm not sure I will, I'm still undecided. 我不确定我会不会,我还是犹豫不决。

has already been decided for you , by someone else, at least for one such file: that particular file is added now, in that other commit. 已经决定对你来说,别人的,至少一个这样的文件:特定的文件现在添加,在其他承诺。 That's the one that Git will overwrite. 这就是Git将覆盖的那个。

You now have a choice of various options: 您现在可以选择各种选项:

  • Don't merge (or don't merge yet): you can leave the file untracked. 不合并(或不合并):您可以保留文件未跟踪。 I think this is pretty clear, but it leaves the situation unresolved. 我认为这很清楚,但它使情况得不到解决。 Eventually you'll (probably?) have to merge.... 最终你(可能?)必须合并....

  • Do merge: the file will become tracked. 合并: 跟踪文件。 You then have sub-choices about how Git should deal with your existing untracked file: 然后,您可以选择Git如何处理现有的未跟踪文件:

    • Add and commit. 添加并提交。 Git will merge your change-since-the-base with their change-since-the-base. Git将把你自己的变化与他们自己的变革合并。 Presumably both changes are add this file , ie, the file is not in the merge base. 据推测,两个更改都添加此文件 ,即文件不在合并库中。 This means the conflict will be an add/add conflict , which is kind of a pain. 这意味着冲突将是一个添加/添加冲突 ,这是一种痛苦。
    • Save your version of the file elsewhere, removing the untracked one. 将您的文件版本保存在其他位置,删除未跟踪的文件。 Let Git merge (as fast-forward or real merge, whichever it does). 让Git合并(作为快进或真正的合并,无论它做什么)。 Manually merge in your own changes, add, and make your own new commit. 手动合并您自己的更改,添加并进行自己的新提交。
    • The other guy is wrong, and the file should not be tracked: Save your version of the file elsewhere. 另一个人错了, 应该跟踪文件:将文件版本保存在别处。 Let Git merge. 让Git合并。 Remove the file and commit, committing the removal. 删除文件并提交,提交删除。 You can then put your own untracked file back. 然后,您可以将自己未跟踪的文件放回原处。

There are some additional options (such as rebasing instead of merging) but they all wind up with the same situation: either the file is tracked (as it is in the commit you told Git to merge), or you must update to that commit anyway, remove the file, and commit again to make a commit in which the file won't be tracked. 还有一些额外的选项(例如rebase而不是合并)但是它们最终都是相同的情况:要么跟踪文件(因为它在你告诉Git要合并的提交中),要么你必须更新到那个提交, 删除该文件,并再次提交以进行不会跟踪该文件的提交。

The way you "save the file elsewhere" is up to you: for instance, you can just move it out of the work-tree entirely, or you can use git stash to make a commit that has the file. “将文件保存在别处”的方式取决于您:例如,您可以完全将其从工作树中移出,或者您可以使用git stash进行具有该文件的提交。

When it works, git stash is pretty convenient, but because it actually makes two or three commits that are not on any branch, it is quite a complicated little beast, and when it goes wrong it becomes very difficult to deal with. 当它工作时, git stash非常方便,但是因为它实际上使得两个或三个提交不在任何分支上,所以它是一个非常复杂的小动物,当它出错时它变得非常难以处理。 So for anything that itself might be complicated, I like to avoid git stash . 因此,对于任何本身可能很复杂的事情,我都希望避免使用 git stash Note that git stash by default stashes only tracked files, so you must git add it to get it into the two commits that git stash normally makes. 请注意, git stash默认只保存被跟踪的文件,所以你必须git add它才能进入git stash通常发出的两个提交。 You can use git stash -u to specifically make a third commit that holds the untracked files, but this makes the stash harder to deal with. 您可以使用git stash -u专门进行第三次提交以保存未跟踪的文件,但这会使存储更难处理。

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

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