Why I use git aliases

Leveraging Git aliases is great for streamlining workflows and boosting productivity during coding sessions. Essentially serving as Git shortcuts, they allow you to execute Git commands with minimal keystrokes, helpingg you become more efficient in your coding task.

Configuration

To configure git aliases, go to your .gitconfig file. Typically, this file lives in C:\Users\username\ for Windows or /Users/username on Mac.

Alternatively, you can pinpoint its location by running the following command in your terminal: git config --list --show-origin.

To add your aliases:

[alias]
  your-alias = git-command

Git aliases I use

[alias]
  a = add .
  br = branch -vv
  cb = checkout -b
  c = checkout # [branch name]
  cm = commit -v -m # [commit message]
  cam = commit -v -a -m # [commit message]
  cp = cherry-pick --no-commit
  db = branch -D # [branch name]
  f = fetch --verbose --prune
  l = log --graph --oneline --decorate
  la = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"
  pf = push --force-with-lease origin HEAD
  pro = pull --rebase origin
  r = rebase
  ra = rebase --abort
  rc = rebase --continue
  rh = reset --hard # [commit hash]
  rs = rebase --interactive --autosquash # [@~number-ofcommits]
  s = status -b -s --untracked-files=all
  st = stash
  sta = stash apply 0

Usage examples

  • View your git aliases: git la
  • Checkout to a new branch: git cb new-branch
  • Delete a branch: git db old-branch
  • Pick a commit from a different branch: git cp commit-hash
  • Add changes to staging and commit them: git cam 'commit message'
  • Squash current commit and five commits before it: git rs @~4

Conclusion

Utilizing aliases is a game changer, particularly when navigating multiple branches, committing often, and employing rebase for a cleaner project history (remember, use with caution as it alters Git history). If you’re a fan of shortcuts and enhanced productivity, setting up your aliases is a must.

Thanks for tuning in! ๐Ÿ‘‹