Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude specific files when pushing to a specific Git repository

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.

like image 900
Kevin Sylvestre Avatar asked Mar 19 '10 17:03

Kevin Sylvestre


People also ask

How do I exclude files when pushing to github?

You can create a .gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the .gitignore file in to your repository.

How do I push a folder to ignore some files?

You can use . gitignore to select what you don't want to push it up to git server. Just put . gitignore to your root folder and add ./assets/* to this file.

How do I ignore already added files in Git?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.


2 Answers

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.

  • Heroku documentation on Slugignore
like image 115
John Topley Avatar answered Sep 28 '22 16:09

John Topley


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...

like image 32
Cascabel Avatar answered Sep 28 '22 14:09

Cascabel