To set up PhpStorm to automatically make a Git commit and push when saving files, you can use a combination of PhpStorm's file watchers and Git integration. Here's a step-by-step guide:
Note: If you already have Git setup, you can skip to step 3.
1. Install Git:
If you haven't already, make sure Git is installed on your system and properly configured.
- Enable Git integration in PhpStorm:
- Open your project in PhpStorm.
- Go to File -> Settings (or PhpStorm -> Preferences on macOS) to open the Settings dialog.
- In the left pane, navigate to Version Control -> Git.
- Click the "+" button to add your Git executable path. If Git is already installed and in your system's PATH variable, PhpStorm should auto-detect it. Otherwise, you'll need to provide the path manually.
- Click "Test" to verify that PhpStorm can locate the Git executable.
3. Configure File Watcher:
- In the Settings dialog, go to Tools -> File Watchers.
- Click the "+" button to add a new file watcher.
- Choose "Custom" from the list of available file watchers.
-
Configure the file watcher settings:
- Name: Give your file watcher a descriptive name (e.g., "Git Commit and Push").
- File Type: Select the specific file types or patterns you want to trigger the Git commit and push action. For example, "*.php" to target PHP files.
- Scope: Choose the appropriate scope for your project files. I use a project folder and have everything within that push to my repository.
- Program: Set the path to the Git executable (already configured in step 2).
- Arguments: Enter the arguments to execute the Git commands. You can use the following template:
commit -a -m "Auto-commit on save"
Note: I have a seperate file watcher for push so I can have commits happen with errors, but push does not.
commit -a -m "push"
- Working Directory: Set the working directory to
$ProjectFileDir$
. - Output paths to refresh: Add the path to the file(s) you want to refresh after the commit and push. For example, I use
$ProjectFileDir$
.
-
Enable the file watcher:
- Check the "Auto-save edited files to trigger the watcher" option.
- Ensure that the file watcher is enabled by checking the box next to its name.
-
Apply and save the changes:
- Click "Apply" and then "OK" to save the settings.
Now, whenever you save a file that matches the configured file type or pattern, PhpStorm will automatically trigger the file watcher, which will perform a Git commit and push using the specified Git executable and arguments. The output paths you specified will also be refreshed, ensuring the latest changes are visible in PhpStorm.
Here is what my File Watcher settings look like:
Note: Be cautious when setting up automatic commit and push actions on save, as it may lead to unintended commits. It's a good practice to review the changes before committing and pushing to ensure the quality and correctness of your code.
So there you have it! Personally I use two separate file watchers, one for committing, and a separate one for pushing. They both run on save, but this enables more control over the advanced settings such as "Trigger regardless of syntax errors" - where my commit will happen, but it won't push if there are errors. I leave that as an exercise for the reader. If you have any questions, please feel free to drop a comment below! Thanks for reading!
Top comments (3)
Hi. Title should clarify it’s only with phpstorm. Thanks
Thank you for the feedback! Clarified the title.
Thanks!