git .gitignore 和“以下未跟踪的工作树文件将被结帐覆盖”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4858047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:59:57  来源:igfitidea点击:

.gitignore and "The following untracked working tree files would be overwritten by checkout"

gitgit-mergegitignore

提问by marcamillion

So I added a folder to my .gitignore file.

所以我在我的 .gitignore 文件中添加了一个文件夹。

Once I do a git statusit tells me

一旦我做了git status它就会告诉我

# On branch latest
nothing to commit (working directory clean)

However, when I try to change branches I get the following:

但是,当我尝试更改分支时,我得到以下信息:

My-MacBook-Pro:webapp marcamillion$ git checkout develop
error: The following untracked working tree files would be overwritten by checkout:
    public/system/images/9/thumb/red-stripe.jpg
    public/system/images/9/original/red-stripe.jpg
    public/system/images/8/thumb/red-stripe-red.jpg
    public/system/images/8/original/red-stripe-red.jpg
    public/system/images/8/original/00-louis_c.k.-chewed_up-cover-2008.jpg
    public/system/images/7/thumb/red-stripe-dark.jpg
    public/system/images/7/original/red-stripe-dark.jpg
    public/system/images/7/original/DSC07833.JPG
    public/system/images/6/thumb/red-stripe-bw.jpg
    public/system/images/6/original/website-logo.png
    public/system/images/6/original/red-stripe-bw.jpg
    public/system/images/5/thumb/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/5/original/logocompv-colored-squares-100px.png
    public/system/images/5/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/4/thumb/DSC_0001.JPG
    public/system/images/4/original/logo.png
    public/system/images/4/original/DSC_0001.JPG
    public/system/images/4/original/2-up.jpg
    public/system/images/3/thumb/logo2.gif
    public/system/images/3/original/logo2.gif
    public/system/images/3/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/3/original/11002000962.jpg
    public/system/images/2/thumb/Profile Pic.jpg
    public/system/images/2/original/Profile Pic.jpg
    public/system/images/2/original/02 Login Screen.jpg
    public/system/images/1/original/Argentina-2010-World-Cup.jpg
Please move or remove them before you can switch branches.
Aborting

This is what my .gitignore file looks like:

这是我的 .gitignore 文件的样子:

.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/**/*
public/system/images/*
public/system/avatars/*

How do I get this working so I can switch branches without deleting those files?

我如何让它工作,以便我可以在不删除这些文件的情况下切换分支?

If I make a change, will it affect those files? In other words, if I came back to this branch afterwards would everything be perfect as up to my latest commit?

如果我进行更改,它会影响那些文件吗?换句话说,如果我之后回到这个分支,一切都会像我最近的提交一样完美吗?

I don't want to lose those files, I just don't want them tracked.

我不想丢失这些文件,我只是不想跟踪它们。

采纳答案by Arrowmaster

It seems like you want the files ignored but they have already been commited. .gitignore has no effect on files that are already in the repo so they need to be removed with git rm --cached. The --cachedwill prevent it from having any effect on your working copy and it will just mark as removed the next time you commit. After the files are removed from the repo then the .gitignore will prevent them from being added again.

似乎您希望文件被忽略,但它们已经被提交。.gitignore 对存储库中已有的文件没有影响,因此需要使用 .gitignore 删除它们git rm --cached。这--cached将防止它对您的工作副本产生任何影响,并且它只会在您下次提交时标记为已删除。从 repo 中删除文件后, .gitignore 将阻止再次添加它们。

But you have another problem with your .gitignore, you are excessively using wildcards and its causing it to match less than you expect it to. Instead lets change the .gitignore and try this.

但是你的 .gitignore 有另一个问题,你过度使用通配符,导致它匹配的比你预期的要少。相反,让我们更改 .gitignore 并尝试这个。

.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/
public/system/images/
public/system/avatars/

回答by Scott Schafer

WARNING: it will delete untracked files, so it's not a great answer to the question being posed.

警告:它会删除未跟踪的文件,因此它不是所提出问题的一个很好的答案。

I hit this message as well. In my case, I didn't want to keep the files, so this worked for me:

我也点击了这条消息。就我而言,我不想保留文件,所以这对我有用:

git 2.11 and newer

git 2.11 及更新版本

git clean  -d  -f .

older git

老git

git clean  -d  -f ""

If you also want to remove files ignored by git, then execute the following command.

如果你还想删除被 git 忽略的文件,那么执行以下命令。

BE WARNED!!! THIS MOST PROBABLY DESTROYS YOUR PROJECT, USE ONLY IF YOU KNOW 100% WHAT YOU ARE DOING

被警告!!!这很可能会破坏您的项目,只有在您 100% 知道自己在做什么时才使用

git 2.11 and newer

git 2.11 及更新版本

git clean  -d  -fx .

older git

老git

git clean  -d  -fx ""

http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

  • -xmeans ignored files are also removed as well as files unknown to git.

  • -dmeans remove untracked directories in addition to untracked files.

  • -fis required to force it to run.

  • -x意味着忽略的文件以及 git 未知的文件也将被删除。

  • -d意味着除了未跟踪的文件之外还删除未跟踪的目录。

  • -f需要强制它运行。

回答by Régis

Warning: This will delete the local files that are not indexed

警告:这将删除未编入索引的本地文件

Just force it : git checkout -f another-branch

只需强制它: git checkout -f another-branch

回答by mattbasta

If you're on OS X, it may be because a file's name has had certain characters change case. Try setting the following config option:

如果您使用的是 OS X,则可能是因为文件名的某些字符大小写发生了变化。尝试设置以下配置选项:

git config core.ignorecase true

回答by Greg Hewgill

Git is telling you that it wants to create files (named public/system/images/9/...etc), but you already have existing files in that directory that aren'ttracked by Git. Perhaps somebody else added those files to the Git repository, and this is the first time you have switched to that branch?

Git 告诉您它想要创建文件(命名public/system/images/9/...等),但是您在该目录中已经有Git跟踪的现有文件。也许其他人将这些文件添加到 Git 存储库中,这是您第一次切换到该分支?

There's probably a reason why those files in your developbranch but not in your current branch. You may have to ask your collaborators why that is.

这些文件在您的develop分支中而不在您当前的分支中可能是有原因的。您可能需要询问您的合作者为什么会这样。

how do I get this working so I can switch branches without deleting those files?

我如何让它工作,以便我可以在不删除这些文件的情况下切换分支?

You can't do it without making the files disappear somehow. You could rename publicto my_publicor something for now.

如果不以某种方式使文件消失,您就无法做到这一点。你现在可以重命名publicmy_public或什么。

if I came back to this branch afterwards would everything be perfect as up to my latest commit?

如果我之后回到这个分支,一切都会像我最近的提交一样完美吗?

If you commit your changes, Git won't lose them. If you don't commit your changes, then Git will try really hard notto overwrite work that you have done. That's what Git is warning you about in the first instance here (when you tried to switch branches).

如果您提交更改,Git 不会丢失它们。如果你不提交你的更改,那么 Git 会非常努力地覆盖你已经完成的工作。这就是 Git 在此处的第一个实例(当您尝试切换分支时)警告您的内容。

回答by Chamara Jayalath

This worked for me.

这对我有用。

 1. git fetch --all
 2. git reset --hard origin/{branch_name}

回答by Abhishek Goel

There is a command for this delicate task (permanently deleting untracked files)

这个微妙的任务有一个命令(永久删除未跟踪的文件)

git clean -i

Then git pullwill do.

然后git pull会做。

回答by mc_kaiser

For those who need something less far-reaching than Scott Schafer's answer,

对于那些需要比Scott Schafer 的答案影响更小的人

git clean -f

will likely work. I highlysuggest running

可能会起作用。我强烈建议跑步

git clean --dry-run

first. That command will output a list of files that Git will remove if you run git clean -f, and might save you the pain of inadvertently removing something you didn't want to.

第一的。如果您运行git clean -f,该命令将输出 Git 将删除的文件列表,并且可能会避免无意中删除您不想删除的内容。

See this Stack Oveflow answeror the docsfor more information on git clean.

这个堆栈Oveflow答案与文档有关的更多信息git clean

回答by Kyle Clegg

Unfortunately neither git rm --cachedor git clean -d -fx ""did it for me.

不幸的是既不git rm --cached或者git clean -d -fx ""为我做。

My solution ended up being pushing my branch to remote, cloning a new repo, then doing my merge in the new repo. Other people accessing the repo had to do the same.

我的解决方案最终将我的分支推送到远程,克隆一个新的 repo,然后在新的 repo 中进行合并。其他访问 repo 的人也必须这样做。

Moral of the story: use a .gitignorefile from inception.

故事的寓意:.gitignore从一开始就使用文件。

回答by GeekHades

If you want to quickly resolve this question,You can use this command:

如果你想快速解决这个问题,可以使用这个命令:

git checkout -f dev