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

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

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

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


当前回答

我在根据之前的提交检出到分支时遇到了同样的问题。Git因为未跟踪文件而拒绝签出。

我找到了一个解决办法,希望它也能帮助到你。

将受影响的目录添加到.gitignore并在其中缓存$ git rm -r——显然是不够的。

假设你想基于之前的commit K创建一个分支来测试一些东西,然后回到当前版本。我会按照以下步骤来做:

Setup the untracked files: edit the .gitignore and apply $ git rm -r --cached on the files and directories you want the git to ignore. Add also the file .gitignore itself to .gitignoreand don't forget to issue $ git rm -r --cached .gitignore. This will ensure the the ignore behavior of git leaves the same in the earlier commits. Commit the changes you just made: $ git add -A $ git commit Save the current log, otherwise you may get problems coming back to the current version $ git log > ../git.log Hard reset to the commit K $ git reset --hard version_k Create a branch based on the commit K $ git branch commit_k_branch Checkout into that branch $ git checkout commit_k_branch Do your stuff and commit it Checkout back into master again $ git checkout master Reset to the current Version again $ git reset current_version or $ git reset ORIG_HEAD Now you can reset hard to the HEAD git reset --hard HEAD

注意!不要跳过倒数第二步(例如:$ git reset——hard ORIG_HEAD )否则git上面抱怨的未跟踪文件将会丢失。

我还确保git抱怨的文件没有被删除。我将它们复制到一个文本文件中,并在$(cat ../test.txt)中发出命令$ for I;做ls -ahl $i;完成

如果再次签出到上面提到的分支,不要忘记发出$ git状态,以确保没有不必要的更改出现。

其他回答

在我的情况下,问题是由于文件名称从订阅更改。ts到Subscribe.ts。

所以我检查我的分支开发人员是否与我所在的当前分支同步。

(电流支路主)

Git合并开发 已经是最新的了 Git分支-d dev Git checkout -b dev

我在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用正确的文件套出项目的最新版本。

在我的例子中,我看到这个错误是因为我使用的是一个流行的开源CMS,导致问题的目录是CMS写入的上传目录。

它说的是有些文件你没有,但你不能从版本控制中获得。

我抓取所有的文件从现场到我的本地,然后我会检查这到回购,希望这解决了问题。

我也面临着类似的问题,我尝试了上面发布的所有解决方案,但都不起作用

当我在开发分支中将我的onmusicupdatelister .java重命名为onmusicupdatelister .java时,导致了这个问题。

master有onmusicupdatelister .java 和develop有和onmusicupdatelister .java相同的文件

现在,每当我切换到master,它就会给我一个错误

The following untracked working tree files would be overwritten by checkout

然后它中止了。

为了解决这个问题,我强制检查了主分支 然后将我的onmusicupdatelister .java重命名为onmusicupdatelister .java,提交它,然后将其合并到开发分支。

然后我更新了我的开发分支合并到主,现在一切恢复正常,问题解决了。

2个名称相同但情况不同的文件可能会出现问题。

您可以删除这些文件中的一个或重命名它。例:

Pdf.html.twig (The GOOD one)

pdf.html.twig (The one I deleted)