Git pull 命令失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23356437/
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
Git pull command failed
提问by rubyist
I am trying to make a Git pull request from the remote repository to local, but it shows below the error, and I am not able to pull.
我正在尝试从远程存储库向本地发出 Git 拉取请求,但它显示在错误下方,我无法拉取。
For example, git pull origin master
:
例如git pull origin master
:
error: The following untracked working tree files would be overwritten by merge:
db/development.sqlite3
db/test.sqlite3
log/development.log
log/test.log
tmp/cache/assets/C7D/310/sprockets%2F38d8bc4c0599099a0404900b377c353b
tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953
tmp/cache/assets/CDC/870/sprockets%2Fa77b26f54f3481a4b4daf08514b49074
tmp/cache/assets/CF0/DA0/sprockets%2Fd7d5b37686831d37c4dd75e645f5e016
tmp/cache/assets/D09/A10/sprockets%2Fd608b39f93a782efe7398ab3797f6948
tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705
tmp/cache/assets/D33/290/sprockets%2F94f084e7801a02e1fd62140be0f5e7cb
tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655
tmp/cache/assets/D57/600/sprockets%2Fec4b1ce010bcc065da023308f49a9d55
tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6
tmp/cache/assets/D6A/C30/sprockets%2Fd5b294a2f636c6ee22c18e24b19fdc41
tmp/cache/assets/D84/A20/sprockets%2Fd88ae988de6a691a6d472eee0441ad88
tmp/cache/assets/DCE/C90/sprockets%2Febaf322f09c9ee13f29fc061542fe6af
tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994
tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af
tmp/cache/assets/E25/4C0/sprockets%2Fde2fd9fd11c04a582cdbbe3d84a35ae6
tmp/pids/server.pid
Please move or remove them before you can merge.
I just started using Git now and had no clue why this error happened. How do I resolve it?
我现在才开始使用 Git,不知道为什么会发生这个错误。我该如何解决?
采纳答案by janos
You have untracked files in your working tree. Those listed in your post. Some of the commits you're trying to merge with git pull
would be overwritten by the merge. This is not safe. It cannot be safe, because these files are untracked, so Git cannot possibly know what to do with them. Should it keep the files you have and ignore the incoming changes? Should it overwrite them? It cannot possibly know, only you can know. So it refuses to do something dangerous.
您的工作树中有未跟踪的文件。那些在你的帖子中列出的。您尝试合并的某些提交git pull
将被合并覆盖。这并不安全。它不可能是安全的,因为这些文件未被跟踪,所以 Git 不可能知道如何处理它们。它是否应该保留您拥有的文件并忽略传入的更改?它应该覆盖它们吗?它不可能知道,只有你能知道。所以它拒绝做危险的事情。
Normally, you need to move these files out of the way. For example, create a temporary directory and move these files there. Or put them in a backup file and delete the files from the working tree.
通常,您需要将这些文件移开。例如,创建一个临时目录并将这些文件移动到那里。或者将它们放在备份文件中并从工作树中删除这些文件。
I wrote normally, because this looks like a special case. Your local repository is correct that these files are not tracked: tmp/*
look like temporary cache files, log/*
files look like log files, and db/*
look like binary database files. None of these should be tracked by version control, normally. But they are tracked now in the remote repository you are trying to merge from. It looks like a developer made a mistake and added these files to version control by accident.
我写的是正常的,因为这看起来像一个特例。您的本地存储库没有跟踪这些文件是正确的:tmp/*
看起来像临时缓存文件,log/*
文件看起来像日志文件,db/*
看起来像二进制数据库文件。通常,版本控制不应跟踪这些。但是现在在您尝试合并的远程存储库中跟踪它们。看起来开发人员犯了一个错误,不小心将这些文件添加到了版本控制中。
The best thing to do is to find the developer who aded those files and ask him to clean up after himself (so he doesn't do it again). After he removes the files from the repository and pushes again, your git pull
command will work.
最好的办法是找到添加这些文件的开发人员并让他自己清理(这样他就不会再做了)。在他从存储库中删除文件并再次推送后,您的git pull
命令将起作用。
If it's difficult to get the author to fix the problem in the repository, you can fix it yourself:
如果很难让作者修复仓库中的问题,你可以自己修复:
# create a clean clone to preserve your workspace
git clone URL /tmp/cleanup
cd /tmp/cleanup
# remove the junk
git rm -r db log tmp
# commit the fix (copy-pasted from @gturri's answer)
git commit -m "Removed binary and temporary files from Git"
git push
After this your git pull
command will work.
在此之后,您的git pull
命令将起作用。
Finally, something that should help preventing mistakes like this is correctly configured .gitignore
files in the project. In your current workspace, if you do git status
and you see tmp
, log
, db
as untracked, that's not so good. That means these directories are not marked to be ignored and it's easier to make mistakes like this and accidentally commit these files. In that case, update your .gitignore
, that will make it a lot harder to make a mistake like this in the future.
最后,.gitignore
在项目中正确配置文件应该有助于防止此类错误。在当前的工作空间,如果你这样做git status
,你会看到tmp
,log
,db
如未经跟踪,这不是那么好。这意味着这些目录没有被标记为被忽略,并且更容易犯这样的错误并意外提交这些文件。在这种情况下,更新您的.gitignore
,这将使将来更难犯这样的错误。
回答by gturri
It means that:
这意味着:
- You locally have those files, and you haven't versioned them.
- Those files are tracked in the
master
of the remote repository.
- 您在本地拥有这些文件,并且您还没有对它们进行版本控制。
- 这些文件
master
在远程存储库中被跟踪。
A quick-and-dirty solution if you want to merge: remove those local files:
如果要合并,一个快速而肮脏的解决方案:删除这些本地文件:
rm -rf db log tmp
git pull origin master
Less quick, but cleaner solution: you're probably right: you shouldn't version binary or temporary files. A developer likely messed up when he/she added those files. You could ask him/her to fix it and pull
afterwards.
不太快但更简洁的解决方案:您可能是对的:您不应该对二进制文件或临时文件进行版本控制。开发人员在添加这些文件时可能会搞砸。您可以要求他/她修复它,pull
然后再进行修复。
You could also fix it yourself, that is: after having pulled, you could do
也可以自己修,就是:拉了之后,就可以了
git rm -rf db log tmp
git commit -m "Removed binary and temporary files from Git"
To ensure sure an error will not happen again, after removing them, you could ask Git to ignore those files, to ensure they won't be added again by mistake.
为了确保不会再发生错误,在删除它们之后,您可以要求 Git 忽略这些文件,以确保它们不会被错误地再次添加。
To do it, add a file called .gitignore
at the root of your repository, and make it like this:
为此,请.gitignore
在存储库的根目录中添加一个名为的文件,并使其如下所示:
*.sqlite3
*.log
tmp
BTW, after adding this file, you could try doing a git status
, to see that Git doesn't care about them any more.
顺便说一句,添加此文件后,您可以尝试执行git status
, 以查看 Git 不再关心它们。