Skip to content

Instantly share code, notes, and snippets.

@Teino1978-Corp
Created November 23, 2015 05:45
Show Gist options
  • Save Teino1978-Corp/9dafbbc2c2759a09e41a to your computer and use it in GitHub Desktop.
Save Teino1978-Corp/9dafbbc2c2759a09e41a to your computer and use it in GitHub Desktop.
'Git' is not a git-command. See 'git --help'.

Make git do stuff

Let's get some help from the shell.

$ cat >> ~/.bash_aliases
alias get = 'git'
alias gi = 'git'
alias got = 'git'
alias gti = 'git'
<ctrl+d>
$ . ~/.bash_aliases

Just plain handy

$ cat ~/.gitconfig
...
[alias]
    br = branch -a
    st = status
    stat = status
    co = checkout
    ci = commit
    ls = ls-files -v

Add/remove aliases like this...

$ git config --global alias.foo bar
$ git config --global alias.dir '!ls -la'
$ git foo <output>
$ git dir <output>
$ git config --global --unset alias.foo
$ git config --global --unset alias.dir

Or go deeply meta with an alias for aliases

$ cat ~/.gitconfig
...
[alias]
    alias = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -"
    aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /'
...

Now you can create a new alias directly! If you are using a shell command, make sure it's fully quoted with singles or doubles.

$ git alias foo bar
$ git alias dir '!ls -la'

Check those logs

Some examples and ideas from

lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
lg2 = log --pretty=format:'%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]%Creset' --decorate
lg3 = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]%Creset" --decorate --numstat

Name it and claim it

If you put a script in your path with a name like git-something you can use it as a git command.

$ ls -1 ~/bin/git-*
$ git-rank   <<< ruby script
$ git-thanks <<< shell script

Script demos go here!

$ git thanks . <output>
$ git rank
$ git rank -o
$ git rank -h
$ git rank -v
$ emacs ~/bin/git-thanks
$ emacs ~/bin/git-rank
$...

Special bonus tip - ruby hates it when you have a seemingly world-writable directory in the script path, e.g.

$ ll -d /afs/umich.edu/user/t
drwxrwxrwx. 29 root root 2048 Oct 28  2011 /afs/umich.edu/user/t
$ .
$ fs la /afs/umich.edu/user/t
Access list for /afs/umich.edu/user/t is
Normal rights:
  system:administrators rlidwka
  system:anyuser rl

But you can belay that ruby noise:

## Suppress world-writable directory warning due to AFS nonsense.
##                      $VERBOSE
## -W0  NO Warnings     nil
## -W1  Quiet           false
## -W2  Verbose         true

BEGIN { $VERBOSE = nil }

Ignoring changed files

There are several reasons you might want to ignore a file, or ignore local changes to a file. The interesting case goes like this:

  • A file is tracked in git, and needs to be, so you can't add it to .gitignore.
  • Devs make local changes to that file, but should never commit/push them.
  • Counting on devs to add the file to their global core.excludesfile is error-prone.
  • Same thing for adding the file pattern to the local repo's .git/info/exclude
  • If that file is intentionally updated upstream, devs should get the updates, nicely merged.

Think about app configs with db credentials needed for your work.

Introducing...

  • git update-index --[no-]assume-unchanged <file>
  • git update-index --[no-]skip-worktree <file>

Read the docs and get the skinny. More to come...

Interesting git enhancements on github

Your turn

What makes you happy and productive with git?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment