Hacker News new | ask | show | jobs
Aliasing Your Git Commands for Maximum Developer Efficiency (tutorialedge.net)
18 points by emforce 673 days ago
14 comments

If you're interested, Oh my zsh already has git aliases and much more for dev productivity. It also has a really large number of supported plugins.

https://github.com/ohmyzsh/ohmyzsh?tab=readme-ov-file

https://kapeli.com/cheat_sheets/Oh-My-Zsh_Git.docset/Content...

oh-my-zsh also checks for updates.

Batteries included can be nice.

When your batteries start to run their own webservers and phone home, someone mistook convenience for batteries.

Updates can be easily disabled: https://github.com/ohmyzsh/ohmyzsh?tab=readme-ov-file#gettin...

I'm pretty sure that omz is not running their own webserver (correct me if I'm wrong here). It is just doing a git pull to update. [1]

[1]: https://github.com/ohmyzsh/ohmyzsh/blob/master/tools/upgrade...

I have several git alias that I live by:

ga = git add -A

gc = git commit

gcm = git commit -m

gp = git push

All of these are defined in my zshrc rather than doing it through git config.

Same thing - I put them in my shell. Here is the list I use:

alias ga="git add"

alias gaw="git add -A && git diff --cached -w | git apply --cached -R"

alias gb="git branch"

alias gbl="git branch -l"

alias gbD="git branch -D"

alias gbu="git branch -u"

alias gc="git commit"

alias gca="git commit -a"

alias gcaa="git commit -a --amend"

alias gcam="git commit -a -m"

alias gce="git commit -e"

alias gcfu="git commit --fixup"

alias gcm="git commit -m"

alias gco="git checkout"

alias gcob="git checkout -b"

alias gcoB="git checkout -B"

alias gcp="git cherry-pick"

alias gcpc="git cherry-pick --continue"

alias gd="git diff"

alias gd^="git diff HEAD^ HEAD"

alias gds="git diff --staged"

alias gl="git lg" # TODO - make these options defaults for "git log" itself

alias glg="git log --graph --decorate --all" # TODO - make these options defaults for "git log" itself

alias gdc="git diff --cached"

alias gpom="git push origin master"

alias gr="git remote"

alias gra="git rebase --abort"

alias grb="git rebase --committer-date-is-author-date"

alias grbom="grb --onto master"

alias grbasi="git rebase --autosquash --interactive"

alias grc="git rebase --continue"

alias grs="git restore --staged"

alias grv="git remote -v"

alias grh="git reset --hard"

alias grH="git reset HEAD"

alias grH^="git reset HEAD^"

alias gs="git status -sb"

alias gsd="git stash drop"

alias gsl="git stash list --date=relative"

alias gsp="git stash pop"

alias gss="git stash show"

alias gst="git status"

alias gstn="git status -uno"

alias gsu="git standup"

alias gforgotrecursive="git submodule update --init --recursive --remote"

I would guess a list this long is self defeating. You wouldn't remember the ones you don't use often but would have a better chance remembering the full command because the language of the command is more intuitive.
Memory is pretty weird with these - I find that my hands do most of the remembering, and my mind doesn't have to do much work. It must be a very individual thing - hope everyone is doing what works for them. And you're right that there's always a safe fallback to default commands. I will admit that seeing the list in its entirety when posting it, I was surprised at its length, but I estimate I use at least 50% of these often!

Looking at the other posts in the thread, it looks like this set of aliases is very similar (maybe I should switch to cut some fat out of my dotfiles): https://kapeli.com/cheat_sheets/Oh-My-Zsh_Git.docset/Content...

> It must be a very individual thing

I suppose that is true because I can't remember more than 3 of my shell git aliases but I'm the most basic git user you can get and I don't use them for work.

I recall seeing a omz (IIRC) plugin that reminds you of aliases that match the commands that you just ran. I had this turned on, but it got too annoying :)
I do similarly but my favorite git log ("gl") variant (in a bash script rather than an alias) is:

set -eux

git log --notes --decorate=full --source --stat --summary -C -M --pretty=fuller $*|less

...so that I can see if there was any revision to a commit after it was first committed, which files were changed and how much, and pass extra parameters if needed.

Maybe I'm not a rockstar ninja 10x dev, but I've never found typing speed to be a hindrance. I'll usually only go out of my way to alias/script things if they would otherwise involve some clicking around or chaining commands.
This is my motivation: https://keysleft.com/
Somehow my brain doesn't work like that (short aliases).

I usually use long form commands and parameters. I use ZShell history + FZF to get at commands with minimal keystrokes.

Don't really have to remember anything. Some vague snippets will narrow it down really fast.

Using lazygit is probably the most efficient way to do git IMO. https://github.com/jesseduffield/lazygit
Im partial to git ui
I like to keep it pretty simple for git aliases, all defined in my shell alias set:

    ```
    s="git status"
    d="git diff"
    c="git diff --cached"
    ```
I have one other, `rb`, which uses a lot of intermediate work to print all the recent branches I've committed to with nice conditional formatting.
One time I argued with my junior developer for like 30 minutes convinced that `git cp` was a built in command and not an alias. Turns out I had been carrying that alias through 4 different dot files managers and had completely forgotten that I wrote it.
Great example of premature optimization. Save seconds per day typing "gc" instead of "git commit"
No it's called nesting, and it is the most holy of practices.

Your axe doesnt need to be extra sharp so you can cut one extra tree in a day, it needs to be extra sharp so it feels good to chop down trees.

how is it a premature optimization? what makes it premature? are you waiting for the thing that comes after git and then you're going to optimize? you don't have to add these aliases if you don't want, but git, and it's usage is fairly mature by this point.
One of my favorites is gsw=“git switch -“. It switches back to whatever branch you were last on.
I learned about git switch through comments for this article on reddit. It's definitely something I'm actively trying to adopt at the moment over my old workflow but it's soo hard to overcome years of muscle memory
Next up: not even using git, for maximum developer efficiency.
Eh. I've only ever aliased "git status -s | awk '$1 == "M" { print $2 }' | xargs -t git add" as "git_add_all": branch switching works well enough in VSCode, "git diff" shows you your commit preview perfectly fine, and shortening "git push" and "git commit" never really felt like a pressing concern.

Actually, no, there is also "git_show_branch" because every time I needed that in the shell, I could never remember the proper incantation. Something to do with rev-parse or something?

Is that different then git add -u
I type rather quickly and feel better having fresh knowledge of the actual commands I use.
thanks, I hate it.

For me, the seconds this saves in typing don't outweigh the annoyance of trying to pair with another developer who only uses their own special aliases.

When you pair-program you’re reading their git prompts?