Skip to content

Instantly share code, notes, and snippets.

@ajitid
Last active June 15, 2022 02:34
Show Gist options
  • Save ajitid/83827f1c6fba189e092b1fe0e90009ed to your computer and use it in GitHub Desktop.
Save ajitid/83827f1c6fba189e092b1fe0e90009ed to your computer and use it in GitHub Desktop.
Get notification on Git push when using Windows CLIs/Sublime Merge/Fork

Setup notification in git (required)

  1. Open Git Bash shell. Put this in ~/.bashrc
export PATH=$PATH:$HOME/Downloads/notify-send

gpn() {
  if git push --force-with-lease origin "$@" ; then
    notify-send.exe "Push Success" "Keep pushin'! ✔"
  else
    notify-send.exe -i error "Push failure" "Is it lint or test?"
  fi
}
  1. Download and extract notify-send equivalent for Windows. Rename the extracted folder to notify-send.

  2. Create a git alias by typing git config --global alias.gpn '!source ~/.bashrc && gpn' in shell.

Setup Sublime Merge

Open Sublime Merge > Preferences > Browse Packages...

Open or create User/Default.sublime-commands there and fill it with following content:

[
  {
    "caption": "Push to origin and notify",
    "command": "git",
    "args": { "argv": ["gpn", "$select_local_branch"] }
  }
]

Close and re-open Sublime Merge.

You are done! Use the command name given in above caption.

Setup Fork

Open File > Preferences > Custom Commands > Click on + (Add) > Add branch custom command

  1. Only keep Local Branch as ticked
  2. Change name to "Push to origin and notify"
  3. Choose Action option from UI/Action radio buttons
  4. Click on Action button
    1. In Action button choose Start Process
    2. For Path use "$git" (without quotes)
    3. For parameters use "gpn $name" (without quotes)
    4. Tick on Wait for Exit and untick Always Show Output
    5. Save the fields using Edit button
  5. Close Preferences

Now if you right click on a local branch a new option that we just created will appear.

Other ways

  • In Windows CLI (Powershell/Command Prompt), navigate to a git repo and use git gpn branch-name.
  • If you prefer Git Bash and don't want Sublime Merge or Fork integration, use git config --global alias.gpn '!gpn' instead.
  • If you just use Powershell and don't want all this fuss then try to find something like this for it.

Interesting features to add

  • Only show notification when Sublime Merge window is not in focus. Clicking on notification brings focus to the app.
  • If you have in your PATH a shell file or a script that prefixed with git-, you can run it like a git subcommand. Eg. given git-muse in PATH, one can run git muse in command line. We can use this over git alias.
[
{
"caption": "Create an Empty Commit...",
"command": "git",
"args": { "argv": ["commit", "--allow-empty", "-m", "$text"] }
},
{
"caption": "Delete Remote Tag (origin)...",
"command": "git",
"args": { "argv": ["push", "--delete", "origin", "$text"] }
},
{
"caption": "Force Push with Lease + No Verify (origin)",
"command": "git",
"args": {
"argv": ["push", "origin", "HEAD", "--no-verify", "--force-with-lease"]
}
},
{
"caption": "Create Branch from Remote Branch…",
"command": "checkout_branch",
"args": { "local_refs": false, "create_branch": true }
},
{
"caption": "Push + Notify (origin)",
"command": "git",
"args": { "argv": ["gpn", "$select_local_branch"] }
},
{
"caption": "Commit w/o hooks",
"command": "git",
"args": { "argv": ["commit", "--no-verify", "-m", "$text"] }
},
{
"caption": "Copy current branch name",
"command": "git",
"args": { "argv": ["ccb"] }
}
]
@ajitid
Copy link
Author

ajitid commented May 11, 2021

You can also use BurntToast to display a notification. This is useful if you don't like notifications generated from notify-send blocking the push in Fork/Sublime Merge until you click on the notification (or until it moves itself to action center). Here is a rough draft on how BurntToast can be used:

  1. Install BurntToast by typing this command in PowerShell Install-Module -Name BurntToast -Scope CurrentUser
  2. In .bashrc, instead of using the PATH defined above, use export PATH=$PATH:/c/Windows/System32/WindowsPowerShell/v1.0
  3. Replace notify-send.exe ... lines in .bashrc with powershell.exe -Command "& {New-BurntToastNotification -Text 'Yay pushed!'}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment