How to temporarily ignore untracked files on git pull?

时间:2018-04-18 17:53:17

标签: git pull

I have some new files that I didn't add to git yet; 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., where XXXX is my new file.

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?

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. 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. But I couldn't find an equivalent option for 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...

2 个答案:

答案 0 :(得分:4)

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


You can also choose to reset your current branch with the following command.

git reset --hard

答案 1 :(得分:1)

您可能知道,git pull基本上是git fetch,后跟git merge

失败的步骤不是git fetch,而是git merge

... overwritten by merge

注意这里的最后一个词是 merge 。这意味着你未定的:

  

我有一些新的文件,我还没有添加到git;我不确定我会不会,我还是犹豫不决。

已经由其他人为您决定,至少对于一个这样的文件:现在,在其他提交中添加了特定文件 。这就是Git将覆盖的那个。

您现在可以选择各种选项:

  • 不要合并(或者不要合并):您可以保留文件未跟踪。我认为这很清楚,但它使情况得不到解决。最终你(可能?)必须合并......

  • 合并:跟踪 文件。然后,您可以选择Git如何处理现有的未跟踪文件:

    • 添加并提交。 Git将把你自己的变化与他们自己的变革合并。据推测,两个更改都是添加此文件,即该文件不在合并库中。这意味着冲突将是添加/添加冲突,这是一种痛苦。
    • 将您的文件版本保存在其他位置,删除未经跟踪的文件。让Git合并(作为快进或真正的合并,无论它做什么)。手动合并您自己的更改,添加并进行自己的新提交。
    • 另一个人错了,文件应该被跟踪:将文件的版本保存在别处。让Git合并。 删除文件并提交,提交删除。然后,您可以放回自己未跟踪的文件。

还有一些其他选项(例如变基而不是合并)但它们最终会出现相同的情况:要么跟踪文件(因为它是在你告诉Git要合并的提交中),要么你必须更新到无论如何,删除文件,然后再次提交以进行提交,其中赢得

您将文件保存在其他地方的方式"取决于你:例如,你可以完全将它从工作树中移出,或者你可以使用git stash进行包含该文件的提交。

当它工作时,git stash非常方便,但是因为它实际上使得两个或三个提交不在任何分支上,所以它是一个非常复杂的小动物,当它出错时,它变得非常困难处理。因此,对于任何本身可能很复杂的事情,我都希望避免 git stash。请注意,git stash默认情况下只会隐藏跟踪的文件,因此您必须git add才能将其置于git stash通常所做的两次提交中。您可以使用git stash -u专门进行包含未跟踪文件的第三次提交,但这会使存储更难处理。