问题
My ideal situation is to automatically minify CSS files and add them to the git commit. I'm not sure if #4 below can be done, but I would like the following flow to be performed:
- Modify CSS file
- Add to staging area
- Commit
- Run script that updates the minified files and adds them to the commit
- Commit completes
If there is another way, I'd be interested in that as well.
回答1:
Whether you should is another matter, but you can.
in .git/hooks/, write a script in your language of choice (make sure it's executable) named pre-commit in that script, run your minifier command, and do 'git add '
here's an example of someone who minifies javascript this way: https://gist.github.com/786460
a test hook I wrote:
#/bin/sh
tr "aeiou" "AEIOU" < test1.css > test1_diff.css
git add test1_diff.css
after running the commit, test1_diff.css was in the working directory, and in git, tracked.
回答2:
Write a smudge/clean script and mark your css files with the filter attribute. The trick is to do the work on a branch that does not have the attribute and deploy from the one that does. This is easy to set up if you back merge from the deploy branch initially with an ours merge strategy. This ensures that subsequent merges don't propagate the attribute.
That should do what you want.
回答3:
You would use a "pre-commit hook" which gets called before/as your actual commit is made. Google it -- it basically just involved putting a pre-commit
script file in your .git
folder.
来源:https://stackoverflow.com/questions/5334671/minify-css-files-via-git-hook