如何避免使用git-pull时合并特定文件?

8
在我的本地代码库中,有一个修改后的设置文件,与存储在GitHub的文件版本不同。每当我从GitHub拉取到我的本地仓库时,我的本地副本就会被改变。
有没有办法避免本地副本被git-pull覆盖?

下投票者想给个理由吗?这似乎是一个不错的问题... - Abe Miessler
这完全没有任何研究努力。既没有阅读任何教程,也没有使用谷歌。@AbeMiessler - Nevik Rehnel
3个回答

1

无法拉取部分提交

虽然您可以通过一些花式操作来获取分支并仅检出特定文件,但实际上git-pull将合并提交清单中的所有文件。因此,您需要改变工作流程。

使用示例文件的正确工作流程

如果有需要存储在代码库中的文件,但不希望覆盖本地更改,则正确的做法是创建一个示例文件,并使用您的.gitignore文件避免将文件的本地副本提交回代码库。例如:

# Move the repository-managed copy of the example file, 
# then add the related local filename to your repository's
# .gitignore file.
git mv .rcfile rcfile.example
echo '.rcfile' >> .gitignore

# Commit the necessary files.
git add .gitignore
git commit -m 'Add example file.'

# Copy the repository-managed example to your local file,
# which will not be overwritten on future pulls.
cp rcfile.example .rcfile

1

这就是gitignore的作用 - 还有Google。


1
非常感谢您,点踩先生。提问者在前两个答案发布后更改了问题,因此gitignore的内容自然不再准确。 - Nevik Rehnel

0

你需要将你的设置文件添加到 .gitignore 中。 了解更多信息,请访问link


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接