When you stage files, git adds them to its object store in .git/objects
. So when
you edit file in working directory, staged files stay unchanged until you add them
with git add
. That's cool, until you want to perform some actions on staged files
themselves. You can always stash local changes, edit files, stage them, and get
changes back from the stash. But this way you might get some merge conflicts
when unstaging. This may be fine when you are editing files yourself, but what if
you want to edit them with scripts? For example, lint your code with git hooks.
The solution: edit files directly in the staging area. This library can help you do this by retrieving file from stage and presenting temporary file with its contents to you. All you have to do is pass a closure, that will modify this file, and it will be modified and saved back to stage.
- Install
git-stage-editor
into your projectcomposer require --dev xwillq/git-stage-editor
- Instantiate
GitStagedFileEditor
with full path to your git repository$editor = new \Xwillq\GitStageEditor\GitStagedFileEditor($git_root);
- Run it with your callback
$editor->execute(function ($file, string $file_path) { exec("php vendor/bin/php-cs-fixer fix $file_path"); });