Git

Tips & Tricks

 

Description : Tips & Tricks Git

Audience : beginner (with a first experience with Git)

Duration : ~ 20 min

Getting started

git <command> -h

Advice 1 : learn to use Git with command line interface

Advice 2 : RTFM

Configure Git

git config --list
user.name=Yann Moisan
user.email=xxx@gmail.com

Check your identity

Common options

color.ui=true
push.default=simple
merge.tool=vimdiff
merge.stat=true
url.https://.insteadof=git://
mergetool.keepbackup=false

saved into file ~/.gitconfig

alias.st=status -sb
alias.ci=commit
alias.br=branch
alias.co=checkout
alias.df=diff
alias.lg=log -p
alias.lg1=log --pretty=format:'%h - %an, %ar : %s'
alias.lg2=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
alias.who=shortlog -sne
alias.oneline=log --pretty=oneline --abbrev-commit --graph
alias.changes=diff --name-status
alias.dic=diff --cached
alias.diffstat=diff --stat
alias.latest=for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short) [%(committername)]'
alias.ds=diff --staged

Git allow to configure aliases :

prefer Git aliases to shell aliases

branch.master.rebase=true
branch.develop.rebase=true

Rebase auto for some branches

Shell

Check that completion works well

git co<Tab>

Use Zsh

Use a hipster prompt (ex: with prezto)

branch name and status indicator on the right

It works also on options

✭ stash
◼ untracked
✱ not staged
⬆ need to push
✚ staged to commit
═ pending merge

Tig

tig is a tool in text mode, keyboard-friendly, based on ncurses, to interact with Git

h : display help

 

At startup, tig show the history view

j/k navigate between commits

<Enter> display the detail of the current commit

 

Status view (press 's') to prepare commit

 

stage / unstage

simpler than the equivalent git add <file> / git reset HEAD <file>

stage a single line

simpler than the equivalent git add -p

 

revert a modification (git checkout -- <file>)

u
1
!

Configure tig

As other Unix tools, tig is highly configurable

❯❯❯ cat ~/.tigrc
 bind stash D !?git stash drop %(stash)
 bind stash A !?git stash apply %(stash)
tig blame <file>

Blame

Branches

Compare 2 branches

Compare 2 branches (better)

no need to remember order

 

git log --oneline --left-right master...experiment
git log --oneline master..experiment

display commit of experiment that are not reachable by master

BREAKING NEWS

change branches

git checkout -

change directory

cd -
git branch --merged
git branch --no-merged

Merge

Use Vim as a merge tool

:diffg RE  " get from REMOTE
:diffg BA  " get from BASE
:diffg LO  " get from LOCAL
git mergetool

And also…

git-flow : a famous Git workflow

hub : interact with GitHub from the CLI

git bisect

Pro Git : excellent book, available freely online

git-extras : some extra commands

git-up : pull all branch at one time

Made with Slides.com