Write notes with Helipad. Register →
| Font Size | Body Font | Header Font | Export | ||||
|---|---|---|---|---|---|---|---|
| TXT | HTML | ![]() |
|||||
GitLondon Notes – Scott Chacon and Charlie Smurthwaite
Help
git help [command]
git [command] —help
Remote management
git remote
Show what is staged (what am I going to commit?):
git diff —staged or —cached
Add all modified files:
git add -u
Stage and commit all modified files:
git commit -u
got commit -a
Interactive add:
git add -i
add -p
~/.gitconfig
git config alias — alias commands that will get stored in .gitconfig
git config —list
Unstage
git status shows command
git reset HEAD
LOGS
Forward forever (things I’m about to commit): git log origin/master..
Back forever: git log ..origin/master
BRANCHES
git branchDefault branch name is ‘master’: any name can be used
HEAD – Different to subversion. Means the branch you’re currently on. The branch that new commits will go to.
Make a new branch: git branch experiment
Switch to branch: git checkout experiment — changes HEAD and updates files in working directory to reflect branch
Commit: git commit — will move HEAD forward for this branch
Show remote branches: git branch -r
Show all: git branch -a
Show last commit on everyone’s branch: git branch -v
Range Forever: ..origin/master or origin/master.. (forward, backward) — equivalent to origin/master..HEAD
git checkout b - creates a new branch and switches to it
Can’t switch branches when there’s dirty files unless they can be merged
What is a branch? find .git/refs — show the heads — all branching does is writes SHA hash into these files
Branches usually change or get removed — i.e., a branch for each release
TAGs
Useful for fixing a branch in time: i.e., a particular version of a project so people can be sure it’s the right one in the future.
Technically the same as a branch but doesn’t move.
MERGING
Merge branch into master: git checkout master ; git merge experiment
Common workflows: master branch is usually in production, a development branch might be used for development then merged into master when ready.
Use branches to work on long running projects that affect the entire project, whilst still working on the older code without upsetting it. Example: converting site to postgres from mysql, meanwhile working on the production version that’s still mysql.
MERGE CONFLICTS
git diff —merge
git mergetool
remote and local branches are identical to git
GIT STASH
Move current work onto a temporary stack of stashes: git stash
Makes working directory clean again so you can continue working on another branch
Good for work that isn’t ready to commit yet
git stash save
git stash list
Go back to the stash: git stash apply
GIT INTERNALS
Optimise local files: git gc
Similar to delta compression (i.e. svn), but each git version might have different algorithms
Writes binary packed files to store
Just a storage format rather than how git usually deals with files
git stash apply
INTERNALS: INTEGERITY
alex@alex ~/Documents/Code/furnish[master*]$ git-fsck
dangling blob 012e795be91e2be1945880455f4cdd631f4a313c
git show 012e795be91e2be1945880455f4cdd631f4a313c
git cat-file -p 012e795be91e2be1945880455f4cdd631f4a313c
ADVANCED
REVISION SELECTION
Ways to refer to revisions:
Examples:
git reflog
git show HEAD@{3}:README
Show patch alongside log: git log -p
Visualise lots of commits: git log —pretty=oneline
Visualise lots of commits with branches in graphs: git log —pretty=oneline —graph
How do I make A look like B? git diff A B — not usually what you want, not what a merge would do
Instead, compare two branches or commits using: git diff A…B — compares against common ancestor — different to triple dot syntax in revision selection
ANNOTATION
view who changed the file: git blame file
Detect lines copied from other files: git blame -C file
git commit —amend
merging with rebase: git rebase — goes back to common ancestor, takes all commits, saves them as patch files, tries to reapply them to another branch. This makes the branch look like it’s got a linear history, because you can’t tell they’ve been merged after a rebase.
Rebase can make it easy to:
Generally useful to clear up your local commits before pushing to server, not a great idea to rebase after push.
(best at staging)
FILTER BRANCH
Nuclear option for changing entire history. Let people know the HEAD is changing and they need to rebase.
git filter-branch —tree-filter ‘rm -f filename’ HEAD
i.e. changing email address across entire history (—env-filter) – dangerous
works with ranges
CHERRY PICKING
git cherry-pick sha1
UNDO
single file: git checkout sha1/commit/HEAD file — there’s no undoing this undo
whole project: git reset -hard - changes staging, index and pointer
git branch will keep things how they are — you can always get back to it. (drops a pointer). This makes the following possible:
Let’s say you’ve made changes but want to put the master to how it was and branch off the new stuff:
git branch experiment
git reset —hard origin/master
git checkout experiment
By default, git reset changes the staging area.
BISECT
Find a commit if you have a way of searching for a yes/no value. Usually used to find where a bug was introduced. Moves the pointer around and lets you merge in the last known good state.
git bisect start
git bisect bad (HEAD is bad)
git bisect good (SHA1 of bad)
git bisect reset — stop
GIT CLEAN
git clean will remove untracked files
GIT PROTOCOLS
ssh
git clone alex@dev.helicoid.net:/absolute/path/File.git
http/s
push can work but it’s a lot of work (dav)
pull is nice, simple and uses low resources, expensive at the client side
needs git update-server-info
git
daemon
efficient for clients: server creates packfiles for each client
INSTALLING ON OUR SERVER
1. server needs git
2. git —bare init
3. mkdir Tiktrac.git
4. (locally) git init
5. git remote add helicoid alex@dev.helicoid.net:/usr/share/git/Tiktrac.git (will prompt for passwords if no key is set up)
6. git push helicoid
gitosis makes this easier (could work with ldap)
tortoise-git for Windows
TextMate git
GARBAGE COLLECTION
git gc -auto - check to see if there are too many loose objects
gc.aggressiveWindow — useful after subversion import to reduce the size
RECOVERY
git branch D branch_name - deletes branch
Finding it again: Figure out what the SHA was. Try git reflog (before it gets GC’d)
git fsck —lost-found
puts dangling trees and blobs in lost-found
really needs a script to find them
BEST PRACTICES
develop integrate
using lots of branches – before you start doing anything unusual, or a risky merge, start a branch
topic branches –
long commit messages – style for company or open source project, passive voice, non-english users for open source – summarise on first line because some sites or tools only show first line
commit often (not necessarily push)
push when it seems ok to share (if you want to share a lot maybe push to a remote branch)
talk to people: explain why you created remote branches
tools, protocol, structure: decided on within the company
difference between version control and backups — some companies would need to make it company policy to do local backups, or use multiple remotes so developers can have their own remotes
HOOKS
local
server-side: per-receive, post-receive, update, post-update
return 0 will halt commit – this could be tied to your commits
INTERFACE STUFF
Colour in UI:
git config —global color.diff auto
git config —global color.status auto
git config —global color.branch auto
Bash prompt:
http://gist.github.com/31631
Web view:
git instaweb -d webrick