使用 git,如何忽略一个分支中的文件但将其提交到另一个分支中? [英] Using git, how do I ignore a file in one branch but have it committed in another branch?

查看:49
本文介绍了使用 git,如何忽略一个分支中的文件但将其提交到另一个分支中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目要部署到 Heroku.源代码树包括一堆 mp3 文件(该网站将用于我大量参与的录音项目).

I've got a project that I'm deploying to Heroku. The source code tree includes a bunch of mp3 files (the website will be for a recording project I was heavily involved with).

我想将它的源代码放在 GitHub 上,但 GitHub 对其有 300 MB 的限制免费帐户.我不想在一堆 mp3 文件上使用 50 MB 的限制.显然,我可以将它们添加到 .gitignore 文件中,以将它们排除在我的仓库之外.

I'd like to put the source code for it up on GitHub, but GitHub has a 300 MB limit on their free accounts. I don't want to use 50 MB of my limit on a bunch of mp3 files. Obviously, I could add them to the .gitignore file to keep them out of my repo.

但是,我使用 git push heroku 部署到 Heroku.mp3 文件必须存在于我推送到 Heroku 的分支中,以便部署它们.

However, I deploy to Heroku using git push heroku. The mp3 files must be present in the branch I push to Heroku so that they get get deployed.

理想情况下,我希望 .gitignore 本地主分支中的 mp3 文件,以便当我将其推送到 GitHub 时,不包含 mp3.然后我会保留一个本地生产分支,该分支已提交而不是忽略 mp3.为了部署,我会将 master 合并到生产中,然后将生产分支推送到 Heroku.

Ideally, I'd like to .gitignore the mp3 files in my local master branch so that when I push that to GitHub, the mp3s are not included. Then I'd keep a local production branch that has the mp3s committed rather than ignored. To deploy, I would merge master into production, and then push the production branch to Heroku.

我无法让它正常工作.

这是我正在尝试做的一个例子......

Here's an example of what I'm trying to do...

$ git init git-ignore-test
$ cd git-ignore-test
$ echo "*.ignored" >> .gitignore
$ git add .gitignore && git commit -m "Ignore .ignored files"
$ touch Foo.ignored

此时,Foo.ignored 在我的 master 分支中被忽略了,但它仍然存在,所以我的项目可以使用它.

At this point, Foo.ignored is ignored in my master branch, but it's still present, so my project can use it.

$ git checkout -b unignored
$ cat /dev/null > .gitignore
$ git add Foo.ignored .gitignore && git commit -m "Unignore .ignored files"

现在我有了一个提交这些文件的分支,正如我所希望的.然而,当我切换回我的主分支时,Foo.ignored 不见了.

Now I've got a branch with these files committed, as I want. However, when I switch back to my master branch, Foo.ignored is gone.

有人对更好的设置方法有什么建议吗?

Anyone got any suggestions for a better way to set this up?

只是为了澄清,我希望 mp3 文件出现在两个分支中,以便当我在本地运行站点(使用任一分支)时,站点可以工作.我只希望在一个分支中忽略文件,因此当我推送到 GitHub 时,它们也不会被推送.通常 .gitignore 适用于这种事情(即保留一个文件的本地副本,该副本不会包含在推送到远程中),但是当我切换到签入文件的分支,然后返回到忽略文件的分支,文件消失.

just to clarify, I want the mp3 files to be present in both branches so that when I run the site locally (using either branch) the site works. I just want the files ignored in one branch so when I push to GitHub they are not pushed as well. Usually .gitignore works well for this kind of thing (i.e. keeping a local copy of a file that does not get included in a push to a remote), but when I switch to the branch with the files checked in, and then back to the branch with the files ignored, the files vanish.

推荐答案

此解决方案似乎仅适用于某些已打补丁的 git 版本.请参阅指向解决方法的新答案另一个答案和后续评论 以提示哪些版本可能有效.

This solution appears to work only for certain, patched versions of git. See a new answer pointing to workarounds and another answer and subsequent comments for a hint which versions may work.

我写了一篇关于如何有效地将 excludesfile 用于不同分支的博客文章,例如一个用于公共 github 和一个用于 heroku 部署.

I wrote a blog post on how to effectively use the excludesfile for different branches, like one for public github and one for heroku deployment.

这是快速而肮脏的:

$ git branch public_viewing
$ cd .git/
$ touch info/exclude_from_public_viewing
$ echo "path/to/secret/file" > info/exclude_from_public_viewing 

然后在 .git/config 文件中添加以下几行:

then in the .git/config file add these lines:

[core]
excludesfile = +info/exclude


[branch "public_viewing"]
excludesfile = +info/exclude_from_public_viewing

现在所有全局忽略的东西都在 info/exclude 文件中,分支特定在 info/exclude_from_public_viewing

Now all the global ignore stuff is in the info/exclude file and the branch specific is in the info/exclude_from_public_viewing

希望有帮助!

http://cogniton-mind.tumblr.com/post/1423976659/howto-gitignore-for-different-branches

这篇关于使用 git,如何忽略一个分支中的文件但将其提交到另一个分支中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆