hands-on introduction to git niceties jan urbanski jan ... · gitting things done hands-on...
TRANSCRIPT
Gitting things doneHands-on introduction to git niceties
Ducksboard
Atlassian Git Party, Madrid, September 25, 2012
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 1 / 37
For those following at home
Getting the slides
http://bit.ly/git-party
Git version used
$ git --version
git version 1.7.10.4
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 2 / 37
1 What makes a distributed VCS
2 Common operations
3 Workflows
4 Fun with git
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 3 / 37
What makes a distributed VCS
Outline
1 What makes a distributed VCS
2 Common operations
3 Workflows
4 Fun with git
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 4 / 37
What makes a distributed VCS
Indepentent instances
I every copy of the repository is standalone
I no central server, no “one true version”
I democracy at its best!
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 5 / 37
What makes a distributed VCS
No revision numbers
I each repository is independent
I no central authority to assign revision numbers
I everything is identified by hashes (commits, files, everything)
I each hash is built from the object’s data and its parent’s hash
I both a consistency and a security measure
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 6 / 37
What makes a distributed VCS
Full control over the repository
I you have access to everything
I you can change past versions of files
I you can change the ownership of commits
I it’s free for all, until you start sharing the repo
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 7 / 37
What makes a distributed VCS
Initial setup
Configuring Git
$ git config --global user.name "Joe H. Hacker"
$ git config --global user.email "[email protected]"
$ git config --global color.diff true
$ git config --global color.status true
$ git config --global color.branch true
$ git config --global color.ui true
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 8 / 37
Common operations
Outline
1 What makes a distributed VCS
2 Common operations
3 Workflows
4 Fun with git
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 9 / 37
Common operations
Checking in changes
Modify files, register changes
$ sed -i ’s/advanced/awesome/’ README
$ git status
...
# modified: README
...
$ git diff --word-diff
$ git add README
$ git commit -m "fix the README"
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 10 / 37
Common operations
Working area, index, repository
I working area is what’s on your filesystem
I index is what you are indenting to commit
I repository is what’s committed
I sounds complicated, but is quite useful
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 11 / 37
Common operations
Using the index
Moving changes between the index and the working area
$ git add -p
$ git status
$ git reset
$ git status
$ git checkout README
$ git status
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 12 / 37
Common operations
Using the stash
Stashing and unstashing changes
$ sed -i ’s/awesome/superb/’ README
$ git stash
$ git stash list
$ git stash pop
$ git diff
$ git stash
$ git stash apply
$ git stash list
$ git checkout .
$ git stash drop
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 13 / 37
Common operations
Branches and tags
I a branch is a chain of commits
I a tag is a pointer to a single commit
I branches are quick and cheap to create
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 14 / 37
Common operations
Using branches
Creating and switching branches
$ git branch
$ git checkout -b my-tests
$ git branch
$ sed -i ’s/awesome/great/’ README
$ git add .
$ git commit -m ’refix the README’
$ git checkout master
$ cat README
$ git checkout -b my-other-tests
$ sed -i ’s/awesome/cool/’ README
$ git commit -a -m ’different way to fix the README’
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 15 / 37
Common operations
A visual example
a (...) c d
e
e'
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 16 / 37
Common operations
Forensics
I all history is stored locally, of course
I makes consulting it very quick
I powerful ways of consulting the history
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 17 / 37
Common operations
Using git log
Trolling in repo history
$ git log
$ git show
$ git log -p
$ git log --stat
$ git log -- src/pl/plpython
$ time git log --oneline | wc -l
$ git log --grep=’Urba[nn]ski’
$ git log -p -S int64
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 18 / 37
Common operations
Viewing differences
Less and more fancy ways of creating diffs
$ git diff
$ git diff master..my-tests
$ HASH=$(git log --until ’master@{1 year ago}’.. \
-n 1 --pretty=%H)
$ echo $HASH
$ git diff --stat $HASH..
$ git log master~100.. -- src/backend/replication
$ git diff master~100.. -- src/backend/replication
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 19 / 37
Common operations
Bisecting
Tracking down a behaviour change
$ cat src/backend/catalog/sql_features.txt
$ cat cat ~/bisect.sh
[ -f src/backend/catalog/sql_features.txt ] || exit 125
! grep -q ’^T131.*YES’ src/backend/catalog/sql_features.txt
$ git bisect start master 1b3d400cac1
$ git bisect run ~/bisect.sh
$ git show 294e7945
$ git blame src/backend/catalog/sql_features.txt
$ g show 294e7945^:src/backend/catalog/sql_features.txt
$ g show 294e7945:src/backend/catalog/sql_features.txt
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 20 / 37
Workflows
Outline
1 What makes a distributed VCS
2 Common operations
3 Workflows
4 Fun with git
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 21 / 37
Workflows
Remote repositories
I a remote is a pointer to a different repository
I you can push your commits to a remote
I or you can pull commits from a remote to your local repository
I otherwise, it’s just like a branch
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 22 / 37
Workflows
Cloning a repository
Ways of cloning an existing repository
$ cd /tmp
$ git clone pg-clone1 ~/src/pg
$ git clone pg-clone2 ~/src/pg
$ git clone http://git.wulczer.org/repos/git-party.git
$ # git clone [email protected]:pg.git
$ cd pg-clone1
$ git remote
$ git branch -r
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 23 / 37
Workflows
Adding commits
Pushing commits upstream
$ cd /tmp/pg-clone1
$ git checkout --track origin/my-tests
$ git branch
$ git wtf
$ sed -i ’s/PostgreSQL/Postgres/g’ README
$ git add .
$ git commit -m ’change project name’
$ git wtf
$ git push
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 24 / 37
Workflows
Fetching upstream changes
Pulling changes from upstream
$ cd /tmp/pg-clone2
$ git checkout --track origin/my-tests
$ git wtf
$ git fetch
$ git wtf
$ git log origin/my-tests
$ git merge origin/my-tests
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 25 / 37
Workflows
Merging vs rebasing
I if only upstream has new commits, straightforward
I if both upstream and you have commits, needs a decision
I either perform a merge or rebase your commits
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 26 / 37
Workflows
Merging vs rebasing - before merging
a b c
d
d'
e
e'
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 27 / 37
Workflows
Merging vs rebasing - after merging
a b c
d
d'
e
f
e'
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 28 / 37
Workflows
Merging vs rebasing - after rebasing
a b c d e d' e'
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 29 / 37
Workflows
Merging and conflicts
Creating a conflict
$ cd /tmp/pg-clone1
$ sed -i ’s/http/https/g’ README
$ git add .
$ git commit -m ’improving security’
$ git push
$ cd /tmp/pg-clone2
$ sed -i ’s/information/info/g’ README
$ git add .
$ git commit -m ’be more concise’
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 30 / 37
Workflows
Merging changes
Resolving a conflict when merging
$ git push
! [rejected] HEAD -> my-tests (non-fast-forward)
$ git log origin/my-tests
$ git fetch
$ git log origin/my-tests
$ git wtf
$ git pull
CONFLICT (content): Merge conflict in README
$ $EDITOR README
$ git add README
$ git commit
$ git log --graph
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 31 / 37
Workflows
Merging changes cont.
Resolving a conflict when rebasing
$ git reset ORIG_HEAD
$ git checkout README
$ git rebase origin/my-tests
CONFLICT (content): Merge conflict in README
$ $EDITOR README
$ git add README
$ git rebase --continue
$ git log --graph
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 32 / 37
Workflows
Merging changes cont.
Autoresolved conflicts
$ git reset --hard origin/my-tests^
$ sed -i ’s/Postgres/pg/g’ README
$ git add .
$ git commit -m ’be more concise’
$ git pull --rebase
$ git push
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 33 / 37
Fun with git
Outline
1 What makes a distributed VCS
2 Common operations
3 Workflows
4 Fun with git
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 34 / 37
Fun with git
Custom commands
Aliases, manual config editing
$ git config --global alias.st status
$ git config --global alias.ci commit
$ git config --global alias.cdiff ’diff --cached’
$ git wtf
$ cat ~/.gitconfig
$ cat ~/src/pg/.git/config
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 35 / 37
Fun with git
Rewriting history
Fixing old commits before pushing
$ git ci --amend
$ sed -i ’s/supported/usefull/’ README
$ git add README
$ git ci -m ’change README’
$ sed -i ’s/Post/post’ README
$ git add README
$ git ci -m ’change case in product name’
$ sed -i ’s/usefull/useful/’ README
$ git add README
$ git ci -m ’fix ortography, oh the shame!’
$ git rebase -i origin/master
$ cat ~/bin/git-fixup
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 36 / 37
Fun with git
Thanks!Questions? Let’s grab a beer.
Jan Urbanski (Ducksboard) Gitting things done Atlassian Git Party 2012 37 / 37