所以我在。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/*

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

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

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


当前回答

我在Windows 8系统上就遇到过这种情况,我在命令提示符下使用Git。我的团队其他成员使用TFS,我使用微软的Git -tf在TFS和我的本地Git存储库之间进行推/拉操作。

问题的产生是由于一些文件被重命名只是为了改变它们的大小写。事情的经过是这样的:

The files were checked in with mixed casing in their names. In a later commit, the file names were changed to all lower-case. git-tf initially got the files in mixed case. When the files were renamed to lower-case, git-tf didn't get the files because to Windows 8 those file names are equivalent. Since Git is case-sensitive, it complained that I had the mixed-case files that weren't in source control. But using git status, I couldn't see any changes, since in the Windows command prompt those file names are equivalent.

对我来说最简单的解决方法是:

Git在添加这些文件之前就签出了以前的项目版本。 然后git用正确的文件套出项目的最新版本。

其他回答

移动文件,而不是删除

避免删除文件的一种方法是移动它们。例如:

cd "`git rev-parse --show-toplevel`"
git checkout 2>&1 | while read f; do [ ! -e "$f" ] || mv "$f" "$f".bak; done

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

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

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

如果不让文件消失,你就不能这么做。你现在可以把public重命名为my_public或者别的什么。

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

如果您提交了更改,Git不会丢失它们。如果您没有提交更改,那么Git会尽量不覆盖您所做的工作。这就是Git在这里的第一个实例(当您尝试切换分支时)警告您的内容。

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

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

这很容易解决,git说你在两个分支中有相同的文件,因此你必须从主分支中删除特定的文件,然后你将能够合并:

git merge "your branch"

我希望它对你有用,我刚刚解决了我的错误。 我的错误是:

error: The following untracked working tree files would be overwritten by merge:
        .vs/slnx.sqlite
Please move or remove them before you merge.
Aborting

现在起作用了! 在我的例子中。vs/slnx。sqlite是visual studio生成的,删除之前需要关闭它。

.gitignore对已经在repo中的文件没有任何影响,因此需要使用git rm -cached删除它们。——cached会阻止它对你的工作副本产生任何影响,下次提交时它只会标记为已删除。文件从repo中删除后,.gitignore将阻止它们再次被添加。

但是你有另一个问题与你的。gitignore,你过度使用通配符,导致它匹配少于你期望它。相反,让我们改变。gitignore,试试这个。

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