Hacker News new | past | comments | ask | show | jobs | submit login

I have these Git shortcuts stored in my .zshrc file:

# https://stackoverflow.com/questions/4298960/git-add-a-git-co...

git config --global alias.ac '!git add -A && git commit'

git config --global alias.acm '!git add -A && git commit -m'

git config --global alias.ll '!git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"'

git config --global alias.gst '!git status'

git config --global alias.gca '!git commit -a --amend'

git config --global alias.gp '!git push origin HEAD'

git config --global alias.bd '!git branch -d'

git config --global alias.bdd '!git branch -D'

git config --global alias.mc '!git diff --name-only --diff-filter=U'

git config --global alias.co '!git checkout'

git config --global alias.po '!git push origin'

git config --global alias.cp '!git add -A && git commit -m "Content" && git push'

Out of those, I only – but often – use `git acm` and `git co` and `git po`.

Edit: formatting (oh dear).




I went to the other extreme. I have a somewhat elaborate fish function which detects any use of -a and refuses to call Git with it.

I had gotten lazy about good commits, basically, `git commit -a` was in my muscle memory. Forcing myself to treat staging as a step before committing, and getting in the habit of using `git add -p` when called for, has made a meaningful difference in the quality of my commit history.

If one is disciplined about making changes in the first place, I suppose this matters less. But I'm not, and also don't want to be: if I spot something else which needs doing while I'm in flow mode, I'm not going to put it off just to keep commits nice, not when there are better options for doing that part.


I make one elaborate commit where I carefully stage things etc. But invariably I miss something, find 3 typos and so on.

that’s when the aliases are handy. The alias for fixing a typo is using all, amend and no-edit. Super useful. But obviously needs to be used with care when you only do such a fix. I’d be perfectly happy for my git oops to refuse running if there is more than one changed line to commit, for example.


My git status is `git s` aliased to `git status -sb`, don't need all the extra clutter.

I also have a few .zshrc aliases, like `gap` which is `git add -p` or `gam` which is `git commit --amend --no-edit`.

But also some typo fixing ones that I end up doing a lot, some because I type in half a command and get distracted / check something, then start over with a partially filled in command:

    alias gigit="git"
    alias gitgit="git"
    alias ggit="git"
    alias qgit="git"
    alias ght="git"
    alias got="git"
    alias cleear="clear"
    alias gits="git s"
Another trick, in your .gitconfig, add this [alias]:

    git = !git
That way, if you accidentally do `git git commit` or whatever (like above) it just works. Also, some typo fixes there:

    commmit = commit
    comit = commit


I have aliases like

  - `ls` for `ls-files`
  - `lo` for `git log --oneline`, then add a variety of letters in multiple combinations like
    - `r` for `--reverse`,
    - `s` for `--stat`,
    - `om` for `origin/master..`,
    - `1` for `-n1`, `2` for `-n2`, `3` for `-n3` (maybe up to 5),

  - `rbi` for `rebase -i`,
  - `rbiom` for `rebase -i origin/master`,
  - `rbc` for `rebase --continue`
  - `rba` for `rebase --abort`

  - `cp` for `cherry-pick`,
  - `cpa` for `cherry-pick --abort`,
  - `cpc` for `cherry-pick --continue`
I also have _shell_ aliases like `gits` that works out to `git status -uno`, and `gita` that lets me add tracked files. I need a `gitca` for `EDITOR=true git commit --amend`.

Notice I don't have aliases for merging. I use strictly rebase and cherry-pick workflows.


I found these online and added them to my gitconfig at one point... I can't take credit for them. Integrating fzf with git makes working with branches even better (with fuzzy matches for checking out as well as deleting branches)...

cob = !BRANCH=`git recent --no-color | fzf` && git checkout ${BRANCH}

db = !BRANCH=`git branch --no-color | fzf` && git branch -d ${BRANCH}

dbf = !BRANCH=`git branch --no-color | fzf` && git branch -D ${BRANCH}


There’s fzf-git.sh [1], which is a collection of bindings to invoke fzf for things like branches, commits, tags, etc. I use it pretty often.

[1] https://github.com/junegunn/fzf-git.sh


I so want to do this, but I am always scared that I will accidentally put in a wrong character and do something that will cause me hours of rework.

Sure question, why have it in zshrc and load it every time a new shell starts? It’s all in the .gitconfig after the first run anyways right?


Don't live in fear. Embrace the chaos.


It’s strange that git aliases can’t accept multiple git commands. It gives a strange divide between single command aliases which go in your git config and multi command aliases which go in a shell config. Shells vary and some don’t even have aliases (cmd is probably the most used shell of them all and it - almost - doesn’t have aliases at all).

Being able to define an && alias in .gitconfig would be great for being able to share snippets like these across shells and OS:es.

My most used alias is by far “git oops” for git commmit —all —-amend —-no-edit


You can absolutely put multiple commands in a single git alias. The comment you replied to even shows it.

There's no reason any of those couldn't be in a .gitconfig file.

Prepend the alias with ! it's executed via a shell. I use it to embed shell functions (which generally call git)

Eg an alias to show commits missing (useful when using cherry pick)

    show-missing-commits = "!show_missing_commits() { local target=\"${2:-$(git rev-parse --abbrev-ref HEAD)}\"; printf -- 'Showing commits present in \"%s\" but not \"%s\"\n' "$1" "$target"; git log --no-merges \"$1\" ^\"${target}\"; }; show_missing_commits"


I’m pretty sure that it’s shell specific whether it works precisely because it defers it to the shell. For windows cmd in particular it’s… not great.

What I mean is I’d like git to do it natively. But git was written assuming a posix shell and it shows up here and there sadly.

Example:

   git command1 —-flag1 <some_separator> command2 —-flag2
Should be possible for git to simply interpret sequentially.


> But git was written assuming a posix shell and it shows up here and there sadly.

Given that the entire point of POSIX is cross platform compatibility, this is one of the few times when I will say they (Linus/git) got it 100% right.

> Should be possible for git to simply interpret sequentially.

Simply? What should it do if the first command fails? Should it exit early? What if you want it to run the second only if the first returns error? What if you want to pipe the first to the second? Or use it as an argument?

Aliases have a "simple" mode where they call exactly one thing. Anything beyond that can't be simple, without also being useless.

If Microsoft can't ship a POSIX compatible shell that's on them, and if you insist on using an OS that doesn't have a POSIX compatible shell, that's on you.

I don't start Linux and ask why it doesn't run whatever people use Windows for... malware I assume?


> Simply? What should it do if the first command fails? Should it exit early? What if you want it to run the second only if the first returns error?

You’d eventually reinvent a shell. But for just these 2 cases you’d just need 2 separators analogous to the & and && of the shell. But yeah it gets messy if you wanted to run a non git command in the middle.

The thing is, all those !a && b are perfectly valid in both posix and cmd. It just refuses to do the whole “if it starts with ! call the shell”. I guess all that’s missing is that it just needs to execute it. That would probably be the less complicated change. That at least doesn’t seem like a big controversial addition (maybe this is already done now it was a couple of years since I tried and hit a brick wall with aliases under cmd)


More than that, it can run arbitrary non-git commands too: prefix with `!` to run in the shell instead of as arguments to `git`.


you should add a dog for pretty logging

    git config --global alias.adog "log --all --decorate --oneline --graph"
https://stackoverflow.com/a/35075021




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: