Out of band syncing to a local git branch.
This started as an experiment to see how commits could be created without
modifying the working directory, the index, or the state of HEAD. The end
result is like a cross between rsync
and git stash
using a specified
branch.
This allows you to deploy your current working directory:
- without polluting your master branch with build artifacts
- without modifying your current checkout at all:
- no branch switching
- no index clobbering
- no complaining about clobbering unstaged changes
Here's how you would use it to push your current working directory to a git push based hosting provider (like Heroku):
$ git-snapshot production
Creating branch 'production' if it doesn't exist...
Creating index... (this can take a while)
Creating tree from index...
Created tree 1e035b51027ba785c74155025cd616bbd20d8d17
Creating commit...
Created commit f2527eb28622d7a17403f723d36584027e595723
$ git push hosting production:master
In order of appearance:
- http://git-scm.com/docs/git-branch
- http://git-scm.com/docs/git-rev-parse
- http://git-scm.com/docs/git-ls-files
- http://git-scm.com/docs/git-hash-object
- http://git-scm.com/docs/git-update-index
- http://git-scm.com/docs/git-write-tree
- http://git-scm.com/docs/git-commit-tree
- http://git-scm.com/docs/git-update-ref
- Option to create tag instead of branch
- Specify commit message
- Don't do empty commits, it means the branch is already up to date
- Find portable replacement for
stat
It does not leave you on the deploy branch. In fact it doesn't even rely on your working copy being clean and doesn't even look at your current index if you happen to be mid-commit. This script takes syncs the specified branch to the current contents of $CWD using completely out of band means. It's like rsync if rsync allowed a branch as a destination.
The point was to demonstrate that you can do this kind of stuff without checking out the target branch or affecting the state of a working copy at all.