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

一旦我输入git状态,它就会告诉我

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

然而,当我尝试改变分支时,我得到以下结果:

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

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

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

我如何让这个工作,所以我可以切换分支而不删除这些文件?

如果我做了更改,会影响那些文件吗?换句话说,如果我之后回到这个分支,在我最近一次提交之前,一切都是完美的吗?

我不想弄丢那些文件,我只是不想让他们被追踪到。


当前回答

如果你使用的是OS X系统,可能是因为某个文件的名称发生了某些字符的变化。尝试设置以下配置选项:

git config core.ignorecase true

其他回答

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

我也打出了这条信息。以我为例,我不想保留这些文件,所以这对我来说很有效:

Git 2.11及更新版本

git clean  -d  -f .

变老

git clean  -d  -f ""

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

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

Git 2.11及更新版本

git clean  -d  -fx .

变老

git clean  -d  -fx ""

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

-x意味着被忽略的文件以及git不知道的文件也会被删除。 -d表示除未跟踪的文件外,还删除未跟踪的目录。 -f命令用于强制运行。

对于那些不需要斯科特·谢弗的答案那么深远的答案的人来说,

git clean -f

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

git clean --dry-run

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

有关git clean的更多信息,请参阅堆栈溢出的回答或文档。

这两个函数

Git rm——缓存 Git checkout -f另一个分支

对我不起作用。

相反,我按照Git告诉你的那样,在eclipse中物理删除了该文件;

Please move or remove them before you can switch branches.

然后添加/提交。

然后我拉了一下,成功了!

在我的例子中,问题在于子模块。Master与另一个分支合并,该分支向项目添加了一个新的子模块。我试图签出的分支没有它,这就是为什么git抱怨未跟踪文件,其他建议的解决方案都不适合我。我强行到我的新分行结账,拉上了主人。

Git checkout -f my_branch Git拉源主 Git子模块update——init

一个简单的解决方案是: 只需确保您在github中的正确工作目录中。 如果用户试图合并文件夹层次结构中太高的目录,几乎每次都会出现该消息。

例子:

/workspace/git/myBashSourceFolder/myProjectSourcefolder

Scenario: User cloned repo in git-folder he created a new Java Project in Eclipse, imported the cloned repo. Eclipse set myProjectSourceFolder as Source Folder in his local Project. therefore the User entered it in git bash and pushed, pulled and commited his project from there. git syncs therefore myProjectSourceFolder - but has no record in his history for myBashSourceFolder. Therefore a push / pull /merge from myBashSourceFolder will produce the given output, if User tries to sync from there next time, instead of the folder he worked before.

解决方案:输入正确的文件夹并再次尝试拉。几乎在每一个 我遇到的时候,这个解决方案工作得很好:)