本文介绍了有没有一种方法可以设置.gitignore,以便在克隆时可以忽略文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,有没有一种方法可以设置.gitignore文件,以便除了克隆文件之外,都忽略该文件?

As stated in the title, is there a way to set .gitignore file so that a file is ignored except when being clone?

我现在正在研究一个项目,该项目将在5台计算机上进行测试,并且所有项目都会定期更新.使用git pull可使事情变得简单,除非需要更改配置文件.由于某些原因,在开发过程中在localhost上工作时,我需要经常更改配置文件,但对于其他计算机,它们可以保持不变.当前,我只是忽略配置文件,但是在将代码克隆到新计算机之后,需要复制配置文件.因此,为了使事情变得更简单,有没有一种方法可以设置.gitignore文件,以便除了git clone时可以忽略文件?

I am now working on a project that will be tested on 5 machines and all will be updated regularly. Using git pull makes things simple, except when I need to change the configuration files. For some reason I need to change the configuration file frequently when I am working on localhost during development but they can be remain the same for other machines. Currently I simply ignore the configuration files, but then I need to copy the configuration files after cloning the code to a new machine. So, to make things even simpler, is there a way I can set .gitignore file so that a file is ignored except when git clone?

推荐答案

您可以做的一件事是,在更改配置文件的计算机上,运行以下命令:git update-index --assume-unchanged <file>.这样一来,当您运行git commit -a时,即使文件已更改,指定的文件也不会添加到索引中,因此也不会提交.然后,您不必担心忽略其他5台计算机上的文件.

One thing you can do is, on the machine where you change the config files, run the following: git update-index --assume-unchanged <file>. This will make it so when you run git commit -a, the specified file will not be added to the index, and thus not committed, even though it was changed. Then you do not have to worry about ignoring the file on the 5 other machines.

这篇关于有没有一种方法可以设置.gitignore,以便在克隆时可以忽略文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:48