Git

Post on 13-Apr-2017

56 views 0 download

Transcript of Git

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.

SVN (Centralized VCS) overview

Git (Distributed VCS) overview

Git (Distributed VCS) overview (cont)

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, ...

• 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

General workflow

File status lifecycle

Repository commits

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)

Terms (cont.)

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

git initNew

git clone <url>Existing

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

Initializing

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}

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

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)

Stashing

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

git stash branch <branchname>

Making a release (tagging)

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

git push --tagsPush tags to remote

git taglist all tags

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

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]

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

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)

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)

Getting Started

InstallConfigurationIgnores

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/

Configurationgit config --global user.name "Sean Lynch"git config --global user.email "sean.lynch@sbcs.com"

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

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

Ignores

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

Collection of .gitignore templates by language / framework

https://github.com/github/gitignore

Review

git initgit clonegit addgit statusgit commitgit log

git branchgit checkoutgit mergegit pushgit fetchgit pull

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

Extras

git submodule

git subtree

Finding regressions

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

Best practices

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

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/