简体   繁体   English

我如何忽略一些文件到与 github 同步的本地存储库?

[英]How can i ignore some files into a local repository synched with github?

i'm using github to create a copy of the latest version of my code in a local machine, i don't want to make push requests i just want to make pull requests and modify some files in my local machine and try to say to github to ignore some files and update the other files.我正在使用 github 在本地机器上创建我的最新版本代码的副本,我不想发出推送请求我只想发出拉取请求并修改我本地机器上的一些文件并尝试说github 忽略一些文件并更新其他文件。

I use the following command to ignore a file named "config.inc.php":我使用以下命令忽略名为“config.inc.php”的文件:

git rm /home/escogit/www/inc/config.inc.php

Then i made changes to other files in the remote github repository and when i try to get the new changes with the command "git pull" i get the following error:然后我对远程 github 存储库中的其他文件进行了更改,当我尝试使用命令“git pull”获取新更改时,出现以下错误:

remote: Counting objects: 9, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 5 (delta 4), reused 5 (delta 4)
Unpacking objects: 100% (5/5), done.
From github.com:Visooal/Colegios
   e724ba3..a624059  master     -> origin/master
Updating e724ba3..a624059
error: Your local changes to the following files would be overwritten by merge:
www/inc/config.inc.php
Please, commit your changes or stash them before you can merge.
Aborting

Note: even if i commit with the command git commit -m ".config.inc.php ignored" i get that error注意:即使我使用命令 git commit -m ".config.inc.php ignored" 提交,我也会收到该错误

First, git rm does not ignore a file.首先, git rm不会忽略一个文件。 It removes the file locally and from the index.它从本地和索引中删除文件。 If you committed the file before you removed it, it will always be in your tree, even if it's not in the current index.如果您在删除文件之前提交了文件,它将始终在您的树中,即使它不在当前索引中也是如此。 Another person can easily re-add it by accident.另一个人可以很容易地不小心重新添加它。 You should setup a.gitignore entry for that file after you git rm it.您应该在git rm之后为该文件设置一个 .gitignore 条目。 People can still manually bypass.gitignore but the file won't show up in git status or any UI tools anymore.人们仍然可以手动绕过 .gitignore,但该文件不会再显示在git status或任何 UI 工具中。

You need to do what it says.你需要按照它说的去做。 git commit -a -m "My Commit Comment" before you do git pull git commit -a -m "My Commit Comment"在你做之前git pull

Just to give a clearer picture, run these commands assuming you already git rm 'ed that file.为了给出更清晰的图片,假设您已经git rm编辑了该文件,请运行这些命令。

echo 'config.inc.php' > .gitignore
git add .gitignore
git commit -a -m "Removed uneeded file"
git pull
// Fix any merge issues
git commit -a -m "Merge"
git push

Git won't allow you to pull with uncommitted changes. Git 不允许您提取未提交的更改。 To make git ignore files usually you would make a .gitignore file, but you can't here because that would be a change;要使 git 忽略文件,通常你会创建一个.gitignore文件,但你不能在这里,因为那将是一个变化; but you can add the file names to repo/.git/info/exclude .但您可以将文件名添加到repo/.git/info/exclude This file uses the same syntax as.gitignore but is not committed or seen by the git index.此文件使用与 .gitignore 相同的语法,但未被 git 索引提交或查看。 So in your example you would add the line所以在你的例子中你会添加行

www/inc/config.inc.php

To escogit/.git/info/exclude .escogit/.git/info/exclude

You may still have a problem if you change one of these ignored files on the remote and then pull that change.如果您在远程更改其中一个被忽略的文件然后拉取该更改,您可能仍然会遇到问题。 So the best thing to do is commit the local changes.所以最好的办法是提交本地更改。 They're not going to be pushed anyway so it doesn't matter.反正他们不会被推,所以没关系。 To do this run要执行此操作

git commit -a -m "a message explaining commit"

Then you can pull and git will automatically merge the changes for you.然后你可以 pull 和 git 会自动为你合并更改。 If you need to get the local back to a clean state you can do this with git checkout.如果您需要让本地恢复到干净的 state,您可以通过git checkout.

You can ignore a file, then ignore changes under.gitignore.你可以忽略一个文件,然后忽略 .gitignore 下的变化。

so it would be something like this: Add your file to .gitignore :所以它会是这样的:将你的文件添加到.gitignore

echo '/home/escogit/www/inc/config.inc.php' >>.gitignore

then ignore changes in gitignore :然后忽略gitignore中的更改:

git update-index --assume-unchanged.gitignore

And it's DONE!!它完成了!

warning: all local changes in.gitignore are going to be omitted to undo this action, and re-undo it again with desired features use: git update-index --no-assume-unchanged.gitignore警告: .gitignore 中的所有本地更改都将被省略以撤消此操作,并使用所需的功能再次重新撤消它使用: git update-index --no-assume-unchanged.gitignore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM