Skip to content

Instantly share code, notes, and snippets.

@ozomer
Last active March 1, 2022 07:56
Show Gist options
  • Save ozomer/cd1674271eff3b8ee7aeeaa4f9d7844b to your computer and use it in GitHub Desktop.
Save ozomer/cd1674271eff3b8ee7aeeaa4f9d7844b to your computer and use it in GitHub Desktop.
Useful Gitconfig Aliases
# ISC License
# Copyright 2022 Hysolate
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
edit-first-pick = "!f() { sed -i \"0,/^pick /s/^pick /edit /\" \"$1\"; }; f"
rebase-ifirst = "!f() { git -c core.editor=\"git edit-first-pick\" rebase -i \"$1\"; }; f"
config-user = "!f() { git config --global user.name \"$1\" ; git config --global user.email \"$2\"; }; f"
encoded-branch-name = "!f() { node.exe -e "console.log\\(encodeURIComponent\\(process.argv\\[1\\]\\)\\)" $(git branch --show-current); }; f"
brci = "!f() { cmd.exe /C start "https://app.circleci.com/pipelines/github/$(git config --get remote.origin.url | sed 's/git@github.com:\\(.*\\).git/\\1/')?branch=$(git encoded-branch-name)"; }; f"
pbfc = "!f() { git push origin \"$1\":refs/heads/bfc-\"$1\" ; }; f"
pl = pull --rebase
st = status
br = branch
co = checkout
sw = switch
ld = log --pretty=format:'%C(auto)%h %C(magenta)%cr %C(cyan)%an %C(auto)%d%C(reset) %s' --graph
pr = "!f() { cmd.exe /C start "https://github.com/$(git config --get remote.origin.url | sed 's/git@github.com:\\(.*\\).git/\\1/')/compare/$(git branch --show-current)?expand=1"; }; f"
cpx = cherry-pick -x
cb = rev-parse --abbrev-ref HEAD
caane = commit -a --amend --no-edit
cafrias = "!f() { git commit -a --fixup \"$1\" && git -c core.editor=\"cat\" rebase -i \"$1\"^ --autosquash ; }; f"
discard-changes-at = "!f() { if [ $(git cb) = $1 ] ; then git reset --hard origin/$1; else git br -f $1 origin/$1; fi; }; f"
discard-changes = "!git discard-changes-at $(git cb)"
force-sync-of = "!f() { git fetch; git discard-changes-at $1; }; f"
discard-changes-and-update = "!git force-sync-of $(git cb)"
force-sync-and-checkout = "!f() { git force-sync-of $1; git co $2; }; f"
start-branch = "!f() { git force-sync-and-checkout $1; git co -b $2; git push -u; }; f"
backup-current-branch = push -f
sync-rebase = "!f() { git force-sync-of $1; git rebase $1; }; f"
sync-final-rebase = "!f() { CB=$(git cb); git sync-rebase $1; git co $1; git br -d $CB; }; f"
srm = sync-rebase master
retrigger = "!f() { git push origin :$(git cb) && git push -u; }; f"
ps = "!f() { git fetch; if [ x\"$(git rev-parse master)\" = x\"$(git rev-parse origin/master)\" ] ; then echo pushing ; git push \"$@\" ; else echo \"push prevented - sync-rebase may be required\"; fi }; f"
# How to Apply a Hotfix in Production
# 1. Update the production branch:
# git switch production && git pl
# 2. Develop the hotfix on a branch named production-<hotfix-description> on top of the production branch.
# In the Pull Request, set production as base.
# 3. When the Pull Request is merged, production is automatically updated with the fix.
# 4. Update production branch locally the development branch of the hotfix:
# git sync-final-rebase production
# 5. Block merges to staging (via GitHub or a message in Slack)
# 6. Update the staging branch:
# git switch staging && git pl
# 7. Start merge
# git switch -c staging-merge-production && git merge --log=1000 -m "Merge Production" production
# 8. Resolve merge conflicts, if there are any
# 9. Push the new branch and create Pull Request
# git push -u
# 10. Wait for Pull Request to be approved (and CI to run)
# Do not merge via GitHub UI!
# 11. Manually push branch
# git switch staging-merge-production && git branch -f staging @ && git switch staging && git push && git branch -D staging-merge-production
# 12. Unblock merges to staging from step 5
# 13. Follow the instructions for Staging hotfix, from its step 6
#
# How to Apply a Hotfix in Staging
# 1. Update the staging branch:
# git switch staging && git pl
# 2. Develop the hotfix on a branch named staging-<hotfix-description> on top of the staging branch
# In the Pull Request, set staging as base
# 3. Merge from github web interface.
# 4. When the Pull Request is merged, staging is automatically updated with the fix
# 5. Update staging branch locally and remove the development branch of the hotfix:
# git sync-final-rebase staging
# 6. Block merges to master (via GitHub or a message in Slack)
# 7. Update the master branch:
# git switch master && git pl
# 8. Start merge
# git switch -c merge-staging && git merge --log=1000 -m "Merge Staging" staging
# 9. Resolve merge conflicts, if there are any
# 10. Push the new branch and create Pull Request
# git push -u
# 11. Wait for Pull Request to be approved (and CI to run)
# Do not merge via GitHub UI!
# 12. Manually push branch
# git branch -f master @ && git switch master && git push && git branch -D merge-staging
# 13. Unblock merges to master from step 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment