jswanner

jswanner

I was about to reply to Why doesn’t Phoenix use Conventional Commit prefixes? - #41 by sodapopcan with some git aliases that I use for things like amending and rebasing commits, but then I thought it would be better to have a separate topic for that discussion, so here we are.

These are some git aliases I use frequently:

amend = commit -v --amend
fixup = !sh -c '_c=$(git rev-parse "$0") && git commit --fixup "$_c" && git rebase --autosquash --autostash --interactive "$_c"^ && unset _c'
force-push = push --force --force-with-lease
intend-to-add = add --intent-to-add
pop = stash pop

Most of these are for saving a few key strokes, for git commands I use extremely frequently (like git status -sb and git commit -v), I have fish shell abbreviations for those that are just 2-3 characters long.

The fixup alias above I find quite useful. It uses git rev-parse so that way the commit to be fixed can be specified by (partial) sha or something like HEAD~3, it’ll then commit what’s staged as a fixup commit and finally start an interactive rebase where most of the time nothing else needs to be done but to confirm.

I have other aliases for things like different formats of git log --all --graph but honestly I don’t use them that much.

Some other settings that I recommend:

[core]
  pager = $(brew --prefix git)/share/git-core/contrib/diff-highlight/diff-highlight | LESSCHARSET='utf-8' less -F -R -X
[branch]
  sort = -committerdate
[rerere]
  enabled = true

First 10 of 22 Posts Switch mode

adamu

adamu

This creates a bash function g, for git, which defaults to git status when there are no arguments.

function g () {
    if [[ $# == 0 ]]
    then
        git status
    else
        git "$@"
    fi
}

sodapopcan

sodapopcan

nit: You don’t need the --force here.

This is a good idea! I feel this should really be the default of git as showing help isn’t that helpful as most people know how to get help (and are probably looking online anyway).

My g is a function that creates a git repo:

g () { mkdir -p "$1" && cd "$1" && git init; }

I otherwise don’t have much to contribute here as I do the vast amount of my gitting in fugitive.vim. I have a couple of pretty uninteresting aliases like gs for git status (which happens also be my vim mapping to open the fugitive status window!) It finally bit me for the first last year when I actually wanted to experiment with ghost script for my job, but then I found out you can bypass aliases by prefixing them with a \ so it worked as a learning moment :slight_smile:

sodapopcan

sodapopcan

Oh! One little trick that it seems a lot of people don’t know (my data is my coworkers) is that instead of HEAD~N, you can just spam ^ which I find easier to type. So HEAD~3 can also be written as HEAD^^^. You can put as many as you want, it’s kinda funny in a way :smiley:

Also, HEAD doesn’t need to be written in caps anymore. It can also be written as @, though I find that more annoying to type on my keyboard than head.

jswanner

jswanner OP

Yeah, I know, it’s a holdover from a time when you actually needed to have both flags, as --force-with-lease did not imply --force.

sodapopcan

sodapopcan

Oh interesting, I did not know that!

matt-savvy

matt-savvy

If you use Oh My Zsh, there’s a gold mine of aliases at your fingertips.

Two personal aliases that I use all the time:
glm - See the commits for the current branch since it branched off from master.

alias glm='git log --oneline master..'

gccp - Copy the current (short) commit hash to the clipboard (pbcopy usually only on OSX, replace with xclip or whatever else you use if needed).

I use this when making changes on a Pull Request. It’s super helpful to respond to a comment and include the commit hash with makes the change being discussed. Github automatically turns this text (if the commit exists on GH) into a link to the commit, making it super easy for followup reviews, etc.

alias gccp="git rev-parse --short HEAD | tr -d '\n' | pbcopy"
sodapopcan

sodapopcan

Sorry I forgot to respond to this—I was too busy having a mini heart attack over the inline types thread :upside_down_face:

I’ve never used ommyzsh but I did used to have that mapping! I got rid of it once I realized I wanted to it across other branches as well. I generally don’t have very many aliases at but I do I have gl for git log which is quite long:

alias gl="git log --pretty=format:'%Cred%H%Creset%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

So now I just do gl main...

jswanner

jswanner OP

Does shell completion work with these functions?

sodapopcan

sodapopcan

I never even thought to try but yes! Well, autocompletion doesn’t make sense for my g alias as I’m always creating a directory that doesn’t exist, but ya, I just tried gl and ya it all works.

Where Next? Top

Trending in Dev Env & Tools Top

dimitarvp
Yeah, feels the same here. :confused: And I made a much bigger investment, the iMac Pro I got was $8000 at the time I bought it (2019). ...
New
mikehostetler
TL;DR; Best practices for shipping an ecosystem of connector packages to Hex in a responsible way? Background I have a project, jido_c...
New
lungarella-raffaele
Hi all, I am writing Elixir using Zed. Often times I like to “Go to definition” on standard libraries code and check out documentation an...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement