Git

35
Git

Transcript of Git

Page 1: Git

Git

Page 2: Git

What is Git?

Distributed version control system (DVCS)

SVN = Centralized version control system

Mercurial, Bazaar are other DVCS

Initially designed and developed by Linus Torvalds to manage Linux kernel code base when BitKeeper revoked free license.

Page 3: Git

SVN (Centralized VCS) overview

Page 4: Git

Git (Distributed VCS) overview

Page 5: Git

Git (Distributed VCS) overview (cont)

Page 6: Git

Why Git?

Embraced by all major hosting sites:Github, Google Code, CodePlex, BitBucket

Used by major projectsLinux kernel, Android, ASP.NET MVC, Facebook, Eclipse, Twitter Boostrap, ...

Page 7: Git

• Able to work disconnected (ex. VPN / home)o (Almost) everything is local, fasto Log is always availableo Private workspace

Able to commit whenever, create snapshots, push when ready (off VPN, done hacking)

Share when ready, ability to modify previous commits before sharing

• Local branching, easily switch gears when customer calls (isolate work units)

features, bug fixes, proof of concepts, etc

• Submodules / subtree

Why Git? - Benefits

Page 8: Git

General workflow

Page 9: Git

File status lifecycle

Page 10: Git

Repository commits

Page 11: Git

Terms

HEAD last commit of current branchHEAD^

parent of HEADmaster

default development branch (trunk)origin

default upstream repository (name of remote when git clone was used)

Page 12: Git

Terms (cont.)

HEAD^ == HEAD~1 # parent of HEADHEAD^^^ == HEAD~3master^^^ == master ~3

Page 13: Git

git initNew

git clone <url>Existing

run at the project's top-most directorycreates a .git directory

Initializing

Page 14: Git

Review previous commitgit show --statgit show --name-status

info about last commit

git show HEADgit show HEAD^^^git show master~10git show @{yesterday}git show master@{May.16}

Page 15: Git

Loggit loggit log --name-statusgit log --oneline --graph --allgit log --author="Sean Lynch"git log --grep="commit.*message.*text"git log -- some/file

limit by changes to specific file

git timeline / git lolcustom aliases

Page 16: Git

Branchinggit branch <branchname>git checkout <branchname>

git checkout -b <branchname>Create new branch and switch to

git branch [-a -l -r]List branches (all, local, remote)

git merge <branchname>Merge branch into current branch (ex. master)

Page 17: Git

Stashing

git stashgit stash [save message]git stash listgit status applygit stash clear

git stash branch <branchname>

Page 18: Git

Making a release (tagging)

git tag -a <name>Create an annotated tag (with message)

git push --tagsPush tags to remote

git taglist all tags

Page 19: Git

Remotesgit remote -vgit remote add <name> <url>git remote show <name>

git branch --set-upstream master origin/masterSet local branch to track remote branch

git svnhttp://git-scm.com/book/en/Git-and-Other-Systems-Git-and-Subversion

Page 20: Git

Updating (Push/Pull)

git fetch / git mergeFetch latest changes from origin, need to use git merge to apply.

git pullPull latest changes from origin (fetch + merge)

git push [remote_name] [branch_name]

Page 21: Git

Revertinggit checkout .

Revert changes to all files into working directory, overwriting any local changes. This is most similar to "svn revert"

git checkout -- <filename>Revert changes to a file in working directory

git log --diff-filter=D -- <filename>git checkout <deleting_commit>^ -- <filename>

Restore a deleted file

Page 22: Git

Reverting (cont.)git reset HEAD

Unstage something

git reset HEAD^forget about a commit, load it back into the staging area

git reset --hard HEAD / git checkout -fReturn to last committed state (discard all local changes). Can not be undone (without reflog)

Page 23: Git

Reverting (cont. more)git commit --amend

Change last commit (correct the previous commit, with the staged changes)

git revert HEADRevert changes (creates a new commit)

Page 24: Git

Getting Started

InstallConfigurationIgnores

Page 25: Git

InstallWindows

Git for Windows: http://msysgit.github.com/Git Extensions:

http://code.google.com/p/gitextensions/TortoiseGit: http://code.google.com/p/tortoisegit/

MacHomebrew: brew install git bash-completion

SourceTree: http://www.sourcetreeapp.com/GitHub for Mac: http://mac.github.com/

Page 26: Git

Configurationgit config --global user.name "Sean Lynch"git config --global user.email "[email protected]"

git config --global core.autocrlf true #Windowsgit config --global core.autocrlf input #Macs

git config --global color.ui truegit config --global log.decorate true

git config --listcat ~/.gitconfig

Page 27: Git

Configuration (aliases)git config --global alias.st status -sgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commit

git config --global alias.timeline=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --all

Page 28: Git

Ignores

git config --global core.excludesfile~/.gitignore_global

Collection of .gitignore templates by language / framework

https://github.com/github/gitignore

Page 29: Git

Review

git initgit clonegit addgit statusgit commitgit log

git branchgit checkoutgit mergegit pushgit fetchgit pull

Page 30: Git

Extras - Modifying history

git commit --amendModify previous commit. Useful to update message or include missing file without creating a new commit

git rebase -iRewrite history. Squash, reorder, by dropping

Page 31: Git

Extras

git submodule

git subtree

Page 32: Git

Finding regressions

git bisect startgit bisect good <commit>git bisect bad <commit>git bisect visualizegit bisect reset

Page 33: Git

Best practices

Don't develop off master, use develop/feature/issue/etc branches

Page 34: Git
Page 35: Git

Help / Documentation / Referencegit <command> --help

http://gitref.org/http://git-scm.com/bookhttp://help.github.com/http://www.codingdomain.com/git/tricks/http://jonas.nitro.dk/git/quick-reference.htmlhttp://gitimmersion.com/http://marklodato.github.com/visual-git-guide/