views:

273

answers:

3

Is it possible to exclude specific files (*.ai, *.psd) when pushing to certain repositories with Git?

My need comes from trying to use Git for both version control and deployment to Heroku. If I include my graphic assets in the deploy, the slug size is larger than desired. However, I do need to include all project files in my main github repository.

Thanks,

Kevin

+4  A: 

You can maintain a second branch for deployment to Heroku, which contains none of those files, but still merges from master. (Of course, you'll have to work out a system for resolving the merge conflicts you get when you modify the .ai and .psd files in master).

The specific thing you ask is impossible, for the simple reason that when you push, you transfer the exact commits from one repository to another, and two commits which don't have the same tree are by definition different commits.

Tip: The most recent versions of git have a --porcelain option for git status which will give easy to parse information like "M file1" "DU file2" (modified and unmerged/deleted by us, respectively). You could write a git-merge wrapper for your deployment branch which attempts the merge, and automatically cleans up the expected conflicts:

git checkout deploy
if ! git merge master; then
    git rm $(git status --porcelain | awk '/^DU/ {print $NF}')
fi

(The reason I printed $NF instead of $2 is that if the file's renamed, it'll look like "DU original_name -> new_name", and the copy placed in the work tree will be new_name, not original_name.)

Of course, the script could get more complex if your situation is - you could look for only certain extensions (add them to the limiting awk pattern), or even capture the whole output in a perl script so you can easily do some more fancy logic...

Jefromi
Can I have separate exclude files for different branches? Will I need to create two projects on my local machine? Thanks.
Kevin Sylvestre
You don't need two repositories for two branches - that's one of the core benefits of using something like git. Branching and merging are really easy. And yes, you can have different .gitignores for different branches (it's just a tracked file inside the repository, after all!), but remember that gitignores aren't for files that are already tracked - they're for helping keep you from committing things (like build objects) that aren't already committed.
Jefromi
Ah, OK I'll have to do some more reading on GIT (I am an SVN user trying to switch over). Thanks for the advice.
Kevin Sylvestre
+3  A: 

There isn't a direct easy way to do that. It's certainly manageable, but with a lot of pain (git wasn't designed to do this).

It would be easier probably if you ask Heroku to provide a way to exclude some files from the deploy.

hasen j
Yeah, I love the hosting solution in general, except that feature. I will try to send the creators a note and see if they have any functionality on the way.
Kevin Sylvestre
It seems this is already possible, as per John Topley's answer.
hasen j
+7  A: 

The easy way to solve your actual problem is to create a .slugignore file in the root of the repository that lists files that shouldn't be packaged in the slug.

John Topley
awesome! this helps me a lot. thanks
stephenmurdoch
Thanks! That is exactly what I needed!
Kevin Sylvestre