talk to git

71
Talk to Git Jim Chen

Upload: yen-ting-chen

Post on 07-May-2015

330 views

Category:

Self Improvement


0 download

DESCRIPTION

A summarize and re-categorize of the git tutorial.

TRANSCRIPT

Page 1: Talk to git

Talk to Git

Jim Chen

Page 2: Talk to git

VCS and Git

Page 3: Talk to git

Why Version Control?

•History is important.

•Time travel is possible!o Roll back to old version

•Collaborationo Contributors can work at the same time.

Page 4: Talk to git

Local Version Control System

•Only on your PC

Page 5: Talk to git

Centralized Version Control System

•Server owns all versions.

•Clients only have one version.

Page 6: Talk to git

Distributed Version Control System

•Every one has a full copy

•Recovery friendly

Page 7: Talk to git

History

In 2005, relationship between Linux Kernel community and commercial company of BitKeeper broken down for reasons.

Page 8: Talk to git

Goals of Git

•Speed

•Simple design

•Strong support for non-linear development (branches over the world)

•Fully distributed

•Able to handle large projects efficiently (Linux kernel is really huge)

Page 9: Talk to git

Git Basics

Page 10: Talk to git

Snapshots, Not Differences

Page 11: Talk to git

Nearly Operations are local

•Full copy in local computer

•Make changes but can keep update with remote

•Network independent

Page 12: Talk to git

Integrity

•Check-sum based tracking

•SHA-1 Hash function

•40-characters

Page 13: Talk to git

Generally Only Adds Data

•Everything committed will be a snapshot in Git history

•Even another commit remove the file.

Page 14: Talk to git

Three States

•Committed

•Modified

•Staged

Page 15: Talk to git

Welcome Aboard

Page 16: Talk to git

Get Repository

• Initialize in existing directory$ git init$ git add *.sh$ git commit -m "Initial project version"

•Clone from existing repository$ git clone <url>

Page 17: Talk to git

File Status

untrackedunmodifie

dmodified staged

add the file

remove the file

git add <file>

git rm <file> commit

stage the file

edit the filegit add <file>

git commit

git reset HEAD <file>

git checkout <file>

Page 18: Talk to git

Checking Status

$ git status

Page 19: Talk to git

Tracking New File

$ touch README$ git status

Page 20: Talk to git

Tracking New File

$ git add README$ git status

Page 21: Talk to git

Staging modfied

Make some change in hello.sh$ git status

Page 22: Talk to git

Staging modfied

$ git add hello.sh$ git status

Page 23: Talk to git

Viewing Unstaged Changes

$ vim hello.sh$ git diff

Page 24: Talk to git

Viewing Staged Changes

$ git diff --cached

Page 25: Talk to git

Discard modified

$ git status$ git checkout hello.sh$ git status

Page 26: Talk to git

Unstage staged

$ git reset HEAD hello.sh

Page 27: Talk to git

Commit It

$ git commit

Page 28: Talk to git

Guidelines to Commit

No whitespaces$ git diff --check

• Try to make each commit a logically separate change set.

• Getting in the habit of creating quality commit messages.o This would help others to review.

Page 29: Talk to git

More commands

•Removing files$ git rm <file>

•Moving files$ git mv <file>

Page 30: Talk to git

Viewing History in CLI

$ git log

Page 31: Talk to git

Viewing History in GUI

$ gitk

Page 32: Talk to git

Oops!Something missed.

But committed already...

Page 33: Talk to git

Change Last Commit

$ git commit --amend

Page 34: Talk to git

Working with Remote

Page 35: Talk to git

Clone Repository

$ git clone <url> <folder>

Page 36: Talk to git

Showing Remotes

$ git remote -v

Page 37: Talk to git

Adding and Removing Remotes

$ git remote add <remote-name> <url>$ git remote rm <remote-name>

Page 38: Talk to git

Fetching and Pulling from Remotes

$ git fetch <remote-name>$ git pull <remote-name>

= git fetch + git merge

Page 39: Talk to git

Pushing changes to Remote

$ git push <remote-name> <branch-name>

Page 40: Talk to git

Branching

Page 41: Talk to git

Why branches?

•Stabilize stables

•Make topics no bother

Page 42: Talk to git

Data Structure

•Blobo Storing file data

•Treeo Structure of project, just like directory

•Commito A pointer to a single tree, with some meta-

data

•Tago Mark a commit as special

Page 43: Talk to git

Commit

Page 44: Talk to git

Versions

Page 45: Talk to git

A lightweight movable pointer to one of the commits.

Branch in Git is

HEAD is a special pointer to the current working branch you are.

Page 46: Talk to git

I want to resolve a topic

Page 47: Talk to git

$ git branch topic1$ git checkout topic1

C0 C1 C2

master

HEAD

topic1

HEAD

Page 48: Talk to git

$ git checkout -b topic1

C0 C1 C2

master

HEAD

topic1

HEAD

Page 49: Talk to git

Commit new change on topic1

C0 C1 C2

master

topic1

C3

topic1

Page 50: Talk to git

Urgent!!Bug fix for master

Page 51: Talk to git

Create hotfix branch from master for fix bug (C4)

$ git checkout master$ git checkout -b hotfixFix bug and commit change

C0 C1 C2

master

topic1

C3

C4

hotfix

Quiz:Where is the HEAD? HEAD

Page 52: Talk to git

Merge branches

hotfix is good for release

Page 53: Talk to git

Merge Mechanism

•Fast-forwardBranches are in the same history flow so Git can

only change the pointer to the new one.

•Three-way merge– Locate the common ancestor– Git automatically calculate how to merge– Create a merge commit recording new

snapshot

Page 54: Talk to git

Fast-forward

$ git checkout master$ git merge hotfixUpdating ...Fast forward

C0 C1 C2

master

topic1

C3

C4

hotfix

master

Page 55: Talk to git

Three-way merge

$ git checkout master$ git merge topic1Merge made by recursive

C0 C1 C2

topic1

C3

C4

master

C5

Snapshot to merge into

Snapshot to merge into

Common Ancestor C6

master

Page 56: Talk to git

Gosh...Conflict happens

Page 57: Talk to git

What will you see when merge conflict

Page 58: Talk to git

Resolve and commit the change

Page 59: Talk to git

Rebasing

Merge is good butI want a clearer history

Page 60: Talk to git

Merge

$ git checkout master$ git merge topic1Merge made by recursive

C0 C1 C2

topic1

C3

C4

C5

C6

master

Page 61: Talk to git

Basic Rebase

$ git checkout topic1$ git rebase master

C0 C1 C2

C4

topic1

C3 C5

master

topic1

C3' C5'

Page 62: Talk to git

Rebasing pushed commits confuses reviewers.

Do not rebase commits have pushed to a public repository!

Page 63: Talk to git

Example of rebasing a pushed commit

Page 64: Talk to git

What's more?

Page 65: Talk to git

Where is the commit lost?

Look into commit updated history

Page 66: Talk to git

$ git reflog

SHA-1 id you can use

Cherry-pick commit d6297c2$ git cherry-pick d6297c2

Page 67: Talk to git

Stashing

New request comes but I am working on something else.

Page 68: Talk to git

$ git stash$ git stash list/pop/apply

Page 69: Talk to git

Interactive Rebase

Change multiple commitsReordering

Page 70: Talk to git

Interactive rebase

C0 C1 C2

master

C3 C4

$ git rebase -i HEAD~2

Successfully rebased and updated refs/heads/master.

master

C4 C3

Page 71: Talk to git

git official sites: http://git-scm.com

commands: http://git-scm.com/book/commands

zh book: http://git-scm.com/book/zh

Git and Linux Kernel: http://en.wikipedia.org/wiki/Linux_kernel#Revision_control

Git Immersion: http://gitimmersion.com (Practices)

Revision Control: http://en.wikipedia.org/wiki/Revision_control

Distributed VCS: http://en.wikipedia.org/wiki/Distributed_revision_control

References