.gitignore 和“以下未跟踪的工作树文件将被签出覆盖”
所以我在 .gitignore 文件中添加了一个文件夹。
一旦我执行了 git status ,它就会告诉我
# 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/*
如何让它工作,以便我可以切换分支而不删除那些文件?
如果我进行更改,会影响这些文件吗?换句话说,如果我之后回到这个分支,一切都会像我最新的提交一样完美吗?
我不想丢失这些文件,我只是不想让它们被跟踪。
So I added a folder to my .gitignore file.
Once I do a git status
it tells me
# 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:
.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
警告:它会删除未跟踪的文件,因此这不是对所提出问题的一个很好的答案。
我也点击了此消息。就我而言,我不想保留这些文件,所以这对我有用:
git 2.11 和较新的
较旧的 git
如果您还想删除 git 忽略的文件,请执行以下命令。
警告!这很可能会毁掉你的项目,只有当你 100% 知道你在做什么时才使用
git 2.11 和更新的
旧版 git
http://www.kernel.org/pub/software/scm/git/docs/git-clean.html
-x 表示忽略的文件以及 git 未知的文件也将被删除。
-d
表示除了未跟踪的文件之外还删除未跟踪的目录。-f
来强制它运行。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
older git
If you also want to remove files ignored by git, then execute the following command.
BE WARNED!!! THIS MOST PROBABLY DESTROYS YOUR PROJECT, USE ONLY IF YOU KNOW 100% WHAT YOU ARE DOING
git 2.11 and newer
older git
http://www.kernel.org/pub/software/scm/git/docs/git-clean.html
-x
means ignored files are also removed as well as files unknown to git.-d
means remove untracked directories in addition to untracked files.-f
is required to force it to run.只需强制执行即可:
git checkout -f another-branch
Just force it :
git checkout -f another-branch
看起来您希望忽略这些文件,但它们已经被提交了。 .gitignore 对存储库中已有的文件没有影响,因此需要使用 git rm --cached 删除它们。
--cached
将防止它对您的工作副本产生任何影响,并且在您下次提交时它将标记为已删除。从存储库中删除文件后,.gitignore 将阻止再次添加它们。但是你的 .gitignore 还有另一个问题,你过度使用通配符,导致它的匹配程度低于你的预期。相反,让我们更改 .gitignore 并尝试这个。
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--cached
will 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.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.
如果您使用的是 OS X,则可能是因为文件名的某些字符更改了大小写。尝试设置以下配置选项:
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:
这对我有用。
This worked for me.
Git 告诉您它想要创建文件(名为
public/system/images/9/...
等),但该目录中已有文件没有< /em> 由 Git 跟踪。也许其他人将这些文件添加到了 Git 存储库,而这是您第一次切换到该分支?这些文件位于您的
develop
分支中但不在您当前的分支中,这可能是有原因的。您可能需要询问您的合作者为什么会这样。如果不让文件以某种方式消失,你就无法做到这一点。您现在可以将
public
重命名为my_public
或其他名称。如果您提交更改,Git 不会丢失它们。如果您不提交更改,Git 将非常努力不覆盖您已完成的工作。这就是 Git 在第一个实例中向您发出的警告(当您尝试切换分支时)。
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't tracked by Git. Perhaps somebody else added those files to the Git repository, and this is the first time you have switched to that branch?There's probably a reason why those files in your
develop
branch but not in your current branch. You may have to ask your collaborators why that is.You can't do it without making the files disappear somehow. You could rename
public
tomy_public
or something for now.If you commit your changes, Git won't lose them. If you don't commit your changes, then Git will try really hard not to 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 pull 就可以了。
There is a command for this delicate task (permanently deleting untracked files)
Then
git pull
will do.这两个函数
git rm --cached
git checkout -f another-branch
对我不起作用。
相反,我按照 Git 的指示物理删除了该文件(在 Eclipse 中);
然后我添加/提交它。
然后我拉了一下,它成功了!
These two functions
git rm --cached
git checkout -f another-branch
did NOT work for me.
Instead, I physically removed the file (in eclipse) as what Git tells you to do;
and then I add/committed it.
and then I pulled and it worked!
对于那些需要比 Scott Schafer 的答案影响较小的人来说,
可能会起作用。我强烈建议
先跑步。该命令将输出一个文件列表,如果您运行 git clean -f,Git 将删除这些文件,并且可能会避免无意中删除您不想删除的内容的痛苦。
请参阅此 Stack Oveflow 答案 或文档 了解有关 git clean 的更多信息。
For those who need something less far-reaching than Scott Schafer’s answer,
will likely work. I highly suggest running
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.See this Stack Oveflow answer or the docs for more information on
git clean
.这个命令解决了我的问题:
this commands solved my problem:
不幸的是,
git rm --cached
或git clean -d -fx ""
都没有为我做到这一点。我的解决方案最终是将我的分支推送到远程,克隆一个新的存储库,然后在新的存储库中进行合并。访问该存储库的其他人也必须执行相同的操作。
这个故事的寓意是:从一开始就使用
.gitignore
文件。Unfortunately neither
git rm --cached
orgit clean -d -fx ""
did it for me.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.
Moral of the story: use a
.gitignore
file from inception.如果你想快速解决这个问题,可以使用这个命令:
If you want to quickly resolve this question,You can use this command:
我在 Windows 8 系统上通过命令提示符使用 Git 时发生了这种情况。我团队的其他成员使用 TFS,我使用 Microsoft 的 git-tf 在 TFS 和我的本地 Git 存储库之间推送/拉取。
出现此问题的原因是某些文件被重命名只是为了更改其大小写。发生的事情似乎是这样的:
对我来说最简单的解决方案是:
git checkout
项目的先前版本,在添加这些文件之前。git checkout
项目的最新版本。This happened to me on a Windows 8 system, using Git from the command prompt. The rest of my team uses TFS, and I use Microsoft's git-tf to push/pull between TFS and my local Git repository.
The problem arose due to some files that had been renamed only to change their case. What appears to have happened was this:
git status
, I couldn't see any changes, since in the Windows command prompt those file names are equivalent.The simplest solution for me was:
git checkout
a previous version of the project, well before those files were ever added.git checkout
the latest version of the project, with the correct file casing.在根据较早的提交签出分支时,我遇到了同样的问题。由于未跟踪的文件,Git 拒绝签出。
我找到了一个解决方案,希望它也能帮助你。
将受影响的目录添加到
.gitignore
并对其发出$ git rm -r --cached
显然是不够的。假设您想基于较早的提交 K 创建一个分支来测试一些内容并返回到当前版本。我将按照以下步骤进行操作:
设置未跟踪的文件:
编辑
.gitignore
并对您希望 git 忽略的文件和目录应用$ git rm -r --cached
。将文件.gitignore
本身添加到.gitignore
并且不要忘记发出$ git rm -r --cached .gitignore
。这将确保 git 的忽略行为在早期提交中保持相同。提交您刚刚所做的更改:
<前><代码>$ git add -A
$ git 提交
保存当前日志,否则,返回当前版本时可能会出现问题
<代码>$ git log > ../git.log
硬重置到提交 K
$ git reset --hard version_k
根据提交 K 创建分支
$ git 分支 commit_k_branch
签出该分支
$ git checkout commit_k_branch
做你的事情并提交
再次签回master
$ git checkout master
再次重置为当前版本
$ git reset current_version
或$ git reset ORIG_HEAD
现在您可以硬重置到 HEAD
注意! 不要跳过倒数第二个步骤(例如
$ git reset --hard ORIG_HEAD
)否则,上面 git 抱怨的未跟踪文件将会丢失。
我还确保 git 抱怨的文件没有被删除。我将它们复制到一个文本文件并发出命令
$ for i in $(cat ../test.txt);执行 ls -ahl $i;完成
如果您再次签出上述分支,请不要忘记发出
$ git status
以确保不会出现不需要的更改。I had the same problem when checking out to a branch based on an earlier commit. Git refused to checkout because of untracked files.
I've found a solution and I hope it will help you too.
Adding the affected directories to
.gitignore
and issuing$ git rm -r --cached
on them is apparently not enough.Assume you want to make a branch based on an earlier commit K to test some stuff and come back to the current version. I would do it in the following steps:
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.gitignore
and don't forget to issue$ git rm -r --cached .gitignore
. This will ensure that the ignore behavior of git leaves the same in the earlier commits.Commit the changes you just made:
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
NOTE! Do not skip the next-to-last step (like e. g.
$ git reset --hard ORIG_HEAD
) otherwise, the untracked files git complained about above will get lost.
I also made sure the files git complained about were not deleted. I copied them to a text-file and issued the command
$ for i in $(cat ../test.txt); do ls -ahl $i; done
If you checkout to the branch mentioned above again, do not forget to issue
$ git status
to ensure no unwanted changes appear.就我而言,问题出在子模块上。
master
与另一个分支合并,该分支向项目添加了一个新的子模块。我试图签出的分支没有它,这就是为什么 git 抱怨未跟踪的文件,并且其他建议的解决方案都不适合我。我强制结账到我的新分支,并拉了master。git checkout -f my_branch
git pull origin master
git submodule update --init
In my case, the problem was with the submodules.
master
was merged with another branch which added a new submodule to the project. The branch I was trying to checkout didn't have it, that's why git was complaining about untracked files and none of the other suggested solutions worked for me. I forced the checkout to my new branch, and pulled master.git checkout -f my_branch
git pull origin master
git submodule update --init
我也遇到了类似的问题,我尝试了上面发布的所有解决方案,但它不起作用
该问题是当我重命名我的
onMusicUpdateListener.java
到时引起的
。develop
分支中的 OnMusicUpdateListener.java现在
master
有了onMusicUpdateListener.java
和
develop
与OnMusicUpdateListener.java
具有相同的文件现在,每当我切换到 master 时,它都会给我一个错误
,然后它
中止
。为了解决这个问题,我强行
签出
master
分支然后将我的
onMusicUpdateListener.java
重命名为OnMusicUpdateListener.java
,提交
它,然后将其与合并
开发分支。然后我通过将其
合并
到master
来更新我的develop
分支,现在一切都恢复正常并且问题得到解决。I was also facing a similar issue and i tried all the solutions posted above but it didn't work
The issue was caused when i renamed my
onMusicUpdateListener.java
toOnMusicUpdateListener.java
indevelop
branch.Now
master
hadonMusicUpdateListener.java
and
develop
had the same file asOnMusicUpdateListener.java
Now whenever i switched to master it gave me an error
and then it
aborted
.In order to solve this, i forcefully
checked out
master
branchand then renamed my
onMusicUpdateListener.java
toOnMusicUpdateListener.java
,committed
it and thenmerged
it withdevelop
branch.Then i updated my
develop
branch bymerging
it intomaster
and now everything is back to normal and problem is solved.使用
git switch
时,可以使用-f
/--force
/--discard-changes
标志:When using
git switch
, you can use the-f
/--force
/--discard-changes
flag:移动文件,而不是删除
避免删除文件的一种方法是移动文件。例如:
Move files, instead of delete
One way of avoiding deleting files is to move them instead. For example:
这很容易解决,git 说你在两个分支中都有相同的文件,因此你必须从 master 分支中删除特定文件,然后你就可以合并:
我希望它对你有用,我只是解决了我的错误。
我的错误是:
现在它可以工作了!
就我而言,
.vs/slnx.sqlite
是由 Visual Studio 生成的,我需要在删除它之前将其关闭。that's easy to solve, git is saying that you have the same files in both branches, therefore you have to delete the specific files from master branch and then you will be able to merge:
I hope it works for you, I just solved my error.
my error was:
Now it is working!
In my case
.vs/slnx.sqlite
was generated by visual studio, I needed to close it before delete it.就我而言, git rm --cached 不起作用。
但我通过 git rebase 得到了它
In my case
git rm --cached
didn't work.But i got it with a
git rebase
这可能是权限问题,
更改所有权,
This could be a permission issue,
change the ownership,
2 个同名但大小写不同的文件可能是问题所在。
您可以删除这些文件中的一个或重命名它。前任:
2 files with the same name but different case might be the issue.
You can Delete one on these files or rename it. Ex:
如果您在本地重命名了文件,然后执行
pull
,它将显示该错误消息。If you have renamed a file locally and then do a
pull
, it will display that error message.就我而言,我看到此错误是因为我使用的是流行的开源 CMS,并且导致问题的目录是 CMS 写入的上传目录。
所以它的意思是,有些文件您没有,但无法从版本控制中获取。
我正在将所有文件从实时站点抓取到本地站点,然后将其签入存储库,希望能够解决问题。
In my case, I was seeing this error because I am using a popular open source CMS and the directory which was causing issues was the uploads directory which the CMS writes to.
So what it was saying is that there are files which you don't have, but which you can't get from versioning.
I'm grabbing all the files from the live site to my local, then I'll check this into the repo in the hope that this fixes the issue.
从 appname/gen/ 中删除 .gitignore 文件来解决此问题。
Delete .gitignore file from appname/gen/ to solve this issue.
我只是进入文件系统并直接删除文件,然后继续 git checkout ,它就工作了。
我已经多次遇到这个问题,它可能与开发人员执行删除、推送、重新添加、推送或类似操作有关。
I just went to the file system and deleted the file directly, then continued with git checkout and it worked.
I've had the problem occur several times and it may be related to developers doing delete, push, re-add, push or some such thing.
大多数答案都考虑删除或删除文件,这是最简单的方法。但有时您不想删除本地文件。但要与策略合并,所以 git 也有解决方案;
Most of the answers consider deleting or removing the files, which is the easy way. But sometimes you don't want to get rid of the local files. But merge with a strategy, so git has solution for this too ;
只需删除文件或重命名它们即可。
例如,
我必须重命名/删除ajax/product.php和ajax/produtPrice.php。
别担心, git pull 会把它们带回来。我建议您重命名它们而不是删除它们,因为您可能会丢失一些更改。
如果这没有帮助,那么您必须删除整个分支并再次创建它,然后执行
git pull origin remotebranch
Just delete the files or rename them.
e.g.
I had to rename/delete ajax/product.php and ajax/produtPrice.php.
Don't worry, git pull will bring them back. I suggest you to rename them instead of deleting, because you might loose some changes.
If this does not help, then you have to delete the whole Branch and create it again and then do
git pull origin remotebranch
以便保存修改后的文件以及以后使用修改后的内容。
我在尝试检查分支和尝试变基时发现了此错误。
尝试 Git stash
git stash
In order to save the modified files and to use the modified content later.
I found this error while i try checking out a branch and when trying to rebase.
Try Git stash
git stash
检查是否有任何文件夹名称包含“/”或任何特殊符号,然后重命名该文件夹。
然后您只需将存储库克隆到另一个位置即可。
Check if any folder name having '/' or any special symbol then rename that folders.
Then you just clone the repository to another location.