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

查看:691
本文介绍了使用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

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在我的主分支中被忽略,但它仍然存在,所以我的项目可以使用它。

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.

以下是快速和肮脏的:

Here's the quick and dirty:

$ 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天全站免登陆