Readit News logoReadit News
CodeIsTheEnd · 2 years ago
My favorite line in my .gitconfig is an alias that prints out the commit history as a tidy graph. (The default `git log --graph` uses 6 whole lines per commit!)

    [alias]
        lg = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all -n 15
Which I took from this Stack Overflow post: https://stackoverflow.com/questions/1057564/pretty-git-branc...

mprime1 · 2 years ago
Simplified version I can type from memory (I.e. on a system where I don’t have my gitconfig aliases):

  -—graph -—oneline -—color

szszrk · 2 years ago
You just gave me a lesson "why one should not just paste things he found online into his terminal". Your parameters have Em dash instead of a hyphen... I reinstalled git from dedicated official repositories before I realized my git is not old... the param is indeed incorrect.

Deleted Comment

ComputerGuru · 2 years ago
In case you didn't know about it, `tig` does just that by default (and a whole lot more).
divbzero · 2 years ago
I like how --all reveals forgotten refs.
OJFord · 2 years ago
Good list, definitely concur with the top ones and can't think of anything major to me that's not listed.

rebase.autosquash combined with an alias I have (called 'fixup') to make that commit and then do the rebase is probably my top one by an absolute long shot, at least in terms of frequency of use. pull.rebase is important to me, but I fixup so frequently, I really don't know how people get by without it.

(Well, I do, generally they create a snaking history by merging master or whatever into their own branch repeatedly, swapping the parents, and make lots of new commits to fix things in the old ones. But I don't know how you can put up with doing that to yourself, is I suppose the long version of what I mean.)

Oh - semi-missed - insteadOf is more useful than implied: I have 'gh:' mapped to the full thing, so I just `git remote add somefork gh:someforker/the-project`, for example.

Some other minor ones: advice.statusHints=false; include.path (if you need it you need it - I use it to set my signing key according to which Yubikey is plugged in, plugging can write the whole file because it's all that's in there, no parsing required); remote "origin".fetch set to get PRs; oh and interactive.singleKey = true if that was missed - not a minor one at all! Makes staging changes so much faster and easier. Obviously not as fast as `add .` or `commit -a`, but don't do that.

NewJazz · 2 years ago
Another way to get that instead of behavior is with SSH configuration. I have something like this in my ssh config:

    Host github
        Hostname github.com
        User git
Then you can do a "git clone github:microsoft/windows" and you're good to go.

OJFord · 2 years ago
True. If I'm only using it for git though makes sense to keep the config there IMO. But certainly that's handy when you're sshing interactively or doing other things with that host.
hinkley · 2 years ago
This fuckin' guy.

If you've evolved an SSO solution from brownfield, or just merged with another company you can easily end up in a spot where you have 2 user names across the systems, and that's before you get into doing things like pushing bug fixes to Github straight from your work machine.

Make yourself an .ssh/config file. Set your user, and your ssh key for where it matters (I don't use the same key for ssh to servers and for talking to github or bitbucket)

jerpint · 2 years ago
Neat
oliv3r · 2 years ago
I really loved the idea of fixup + rebase, but of course we didn't have an example here :)

So I spend half an hour to learn about git shell commands in aliases (had to learn the hardware that arguments require functions) and came up with the following.

fixup = "!f() { git commit --fixup ${1} && GIT_SEQUENCE_EDITOR=true git rebase --autosquash --interactive --rebase-merges \"${1}~1\"; }; f"

from https://gitlab.com/olliver/dotfiles/-/blob/master/.config/gi...

Hope this helps anybody else :)

chungy · 2 years ago
My config:

    [alias]
            co = checkout
            ci = commit
            st = status
            br = branch
            hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
            type = cat-file -t
            dump = cat-file -p
            dft = difftool
    [tag]
            sort = version:refname
    [tar "tar.xz"]
            command = xz -c
    [tar "tar.zst"]
            command = zstd -T0 -c
    [log]
            date = iso-local
    [pull]
            ff = only
    [diff]
            tool = difftastic
    [difftool]
            prompt = false
    [difftool "difftastic"]
            cmd = difft "$LOCAL" "$REMOTE"
    [pager]
            difftool = true
    [safe]
            directory = *
    [advice]
            detachedHead = false
    [init]
            defaultBranch = master

xyst · 2 years ago
Not a fan of the “main” branch revolution?

Pretty good config though. Personally, I use diff configs depending on oss vs personal vs work. So I combine mine with “[includeIf gitdir:~/oss…]” …

Some companies like G have some weird requirements.

chungy · 2 years ago
No, I'm not a fan of it. It's been master since forever and it's a perfectly good term, fits regular English convention of "master branch" too.

> Personally, I use diff configs depending on oss vs personal vs work. So I combine mine with “[includeIf gitdir:~/oss…]” …

I'd stuff such things into the local repos' .git/config files instead of the global. It's rare that I do it, but sometimes I do work with projects with differing conventions.

imbnwa · 2 years ago
>Not a fan of the “main” branch revolution?

Why not 'canon'?

wooptoo · 2 years ago
Did you use Mercurial before? I miss its abbreviated commands. A few more from me:

  [alias]
    gr = grep -I
    ch = cherry-pick
    ls = ls-files
    tip = log -1 HEAD
    zip = archive -o latest.zip HEAD
    fix = commit --fixup

chungy · 2 years ago
Never used Mercurial in any serious capacity. The abbreviations mostly stem from CVS and Subversion experience (and really glad I haven't touched either in many years).
edavis · 2 years ago
For the "I want to use different emails for different repos" issue, I've seen the `includeIf` technique but it felt fiddly. What I've done is set `user.useConfigOnly = true` and commented out `user.email` in ~/.gitconfig.

Now the first time I commit in a new repo, it errors out with "Author identity unknown" and I punch in "git config user.email ADDR" for the email I want to use and re-run the commit.

Garbage · 2 years ago
During my consulting time, I had to do this a lot. So I wrote a blog post for myself on how to do this effectively - https://www.shirishpadalkar.com/using-separate-git-identitie...
quectophoton · 2 years ago
I also don't like the `includeIf`, but I ended up with a completely different way of doing things.

I just use different accounts depending on the context, usually by either SSH or by starting a Docker container with all the environment already configured.

At least for me, using different identities[1] when I want to use different identities[2], seems less brittle and less likely for my future self to accidentally forget to do something (e.g. change a config when I reinstall an OS).

[1]: OS user, directly (SSH) or indirectly (container).

[2]: Email address, username, etc.

NewJazz · 2 years ago
Huh. How many repos do you have? And how many emails? I have... A lot of repos. And two emails.
thfuran · 2 years ago
Two is still more than one, and it seems reasonable to, for example, keep work separate from personal stuff.
vbezhenar · 2 years ago
One git option that I almost never see in other people configs is

    [core]
      autocrlf = input
      safecrlf = true
It prevents commiting CRLF files and forces you to convert those to LF before commit (you can still override it with gitattributes if necessary). I hate CRLF so much that I spent lots of time digging out this combination of configuration values.

demurgos · 2 years ago
Oh that's great. I always used the following `.gitattributes`:

    # Enforce `lf` for text files (even on Windows)
    * text=auto eol=lf
I'll try your config too.

My current position is that anywhere you can choose between CRLF and LF (git, editor, program output), it should always be LF regardless of the platform. Simple LF just works on Windows, and removing this variation point simplifies so much of code. For example, you can check reproducible outputs or hashing with a single reference value.

JelteF · 2 years ago
My personal favorite additions to my git config are using delta[1] as my pager for much more readable and syntax highlighted diffs. And adding the following alias, which means I no can always check out to the default branch of the repo, no matter if it's called master/main/develop/whatever

    [alias]
    checkout-default = "!git checkout $(git rev-parse --abbrev-ref origin/HEAD | sed 's@^origin/@@')"
[1] https://github.com/dandavison/delta

wooptoo · 2 years ago
A few more:

Change the comment character. This is useful when you want to include a hash in your commit message (e.g. for automatically linking to JIRA tickets).

  [core]
    commentChar = ";"
When starting a new branch/ticket you can create an empty commit where you write a detailed subject and description.

  [alias]
    newtask = commit --allow-empty
Create a zip archive of the current files.

  [alias]
    zip = archive -o latest.zip HEAD
Switch between the last two active branches with:

  git checkout - 
Use short log lines:

  [format]
    pretty = format:%C(yellow)%h %Cblue%>(12)%ad %Cgreen%<(7)%aN%Cred%d %Creset%s
Some delta tweaks:

  [delta]
    features = decorations  # line numbers
    navigate = true  # use n and N to move between diff sections
And finally:

  [alias]
    praise = blame

ComputerGuru · 2 years ago
How to get all your set git options:

    git config --global --get-regexp . | sort -u
A hack I like that makes diffing possibly minified or formatting-mangled source files easier:

    diff.css.textconv css-beautify
(Unminifies and unifies the formatting of css files before diffing them, might need to have the *.css type set in your .gitattributes though)

    core.editor nvim
(Use neovim as the editor for commit messages and other things)

nerdponx · 2 years ago
I use this to get a list of my configured aliases.

  [alias]
  ls-alias = config --global --includes --get-regexp 'alias\..*'

mkhnews · 2 years ago
Same here -

[alias]

    alias = "!git config --get-regexp ^alias. | sed 's/^alias.//'"

NewJazz · 2 years ago
You can set the EDITOR env var instead of using core.editor. or setup symlinks with your package manager.