KEMBAR78
Learning Git with Workflows | PDF
Learning Git with Workflows
Mosky
This slides

2
This slides
won't
explain every options of Git commands;
and the internal of Git.

2
This slides
won't
explain every options of Git commands;
and the internal of Git.
will
let you start to use Git immediately;
and learn the common Git workflows.
2
Mosky

3
Mosky
A Python engineer at Pinkoi

3
Mosky
A Python engineer at Pinkoi
An author of some Python packages
MoSQL, Clime, ...

3
Mosky
A Python engineer at Pinkoi
An author of some Python packages
MoSQL, Clime, ...
A speaker at several conferences
PyCon APAC 2013, COSCUP 2013,
PyCon TW 2013, ...

3
Mosky
A Python engineer at Pinkoi
An author of some Python packages
MoSQL, Clime, ...
A speaker at several conferences
PyCon APAC 2013, COSCUP 2013,
PyCon TW 2013, ...
A Python trainer

3
Mosky
A Python engineer at Pinkoi
An author of some Python packages
MoSQL, Clime, ...
A speaker at several conferences
PyCon APAC 2013, COSCUP 2013,
PyCon TW 2013, ...
A Python trainer
http://mosky.tw/
3
Outline

6
Outline
Setup Git

6
Outline
Setup Git
Routine
Core
Secondary

6
Outline
Setup Git
Routine
Core
Secondary
Branching

6
Outline
Setup Git
Routine
Core
Secondary
Branching
Remote Repository

6
Outline
Setup Git
Routine
Core
Secondary
Branching
Remote Repository
A Co-working Workflow
6
Setup Git
Get Git!

8
Get Git!
Ubuntu, Debian or any APT-based Linux
$ sudo apt-get install git-core

8
Get Git!
Ubuntu, Debian or any APT-based Linux
$ sudo apt-get install git-core
Mac
$ brew install git
http://brew.sh/

8
Get Git!
Ubuntu, Debian or any APT-based Linux
$ sudo apt-get install git-core
Mac
$ brew install git
http://brew.sh/
Windows
http://git-scm.com/download/win
8
Is Git there?

9
GUIs are available

10
GUIs are available
Thanks GitHub!

10
GUIs are available
Thanks GitHub!
"Github for Mac"
http://mac.github.com/

10
GUIs are available
Thanks GitHub!
"Github for Mac"
http://mac.github.com/
"Github for Windows"
http://windows.github.com/

10
GUIs are available
Thanks GitHub!
"Github for Mac"
http://mac.github.com/
"Github for Windows"
http://windows.github.com/
Other
http://git-scm.com/downloads/guis
10
Tell Git who you are

11
Tell Git who you are

$ git config --global user.name "Mosky Liu"

11
Tell Git who you are

$ git config --global user.name "Mosky Liu"
$ git config --global user.email mosky.tw@gmail.com

11
Other Git configs

12
Other Git configs

$ git config --global core.editor emacs

12
Other Git configs

$ git config --global core.editor emacs
$ git config --global merge.tool vimdiff

12
Other Git configs

$ git config --global core.editor emacs
$ git config --global merge.tool vimdiff
$ git config --list

12
Other Git configs

$ git config --global core.editor emacs
$ git config --global merge.tool vimdiff
$ git config --list
$ vim ~/.gitconfig

12
Other Git configs

$ git config --global core.editor emacs
$ git config --global merge.tool vimdiff
$ git config --list
$ vim ~/.gitconfig

http://j.mp/mosky-gitconfig

12
Core Routine
Create a repository

14
Create a repository

$ git init [<directory>]

14
Commit changes

15
Commit changes

$ git add <file> ...

15
Commit changes

$ git add <file> ...
$ git commit

15
Simplest Workflow

16
Simplest Workflow
(1) $ git init <directory>

16
Simplest Workflow
(1) $ git init <directory>
(2)

(modify file)

16
Simplest Workflow
(1) $ git init <directory>
(2)

(modify file)

(3) $ git add <file> ...

16
Simplest Workflow
(1) $ git init <directory>
(2)

(modify file)

(3) $ git add <file> ...
(4) $ git commit

16
Simplest Workflow
(1) $ git init <directory>
(2)

(modify file)

(3) $ git add <file> ...
(4) $ git commit
# Back 2

16
Simplest Workflow
(1) $ git init <directory>
(2)

(modify file)

(3) $ git add <file> ...
(4) $ git commit
# Back 2
The end of the core --- it's super easy!
16
Secondary Routine
Check status of files

18
Check status of files

$ git status

18
Check status of files

$ git status

18
Check what you changed

19
Check what you changed

$ git diff

19
Check what you changed

$ git diff

19
Check commits

20
Check commits

$ git log

20
Check commits

$ git log

20
Move between commits

21
Move between commits
$ git checkout <commit>

21
Move between commits
$ git checkout <commit>
<commit>
599d439fd3813298da16f12ed40f3a0
716872c30
599d439
HEAD
21
Name commit

22
Name commit

$ git tag <tagname>
$ git checkout 599d439
$ git tag v0.1
$ git checkout v0.1

22
Reset to a commit

23
Reset to a commit

$ git reset <commit>
Reset HEAD to <commit>.

23
Make a "reverse" commit

24
Make a "reverse" commit

$ git revert <commit>
Apply a "reverse" commit.

24
Reset to a commit

25
Reset to a commit

26
Make a "reverse" commit

27
Make a "reverse" commit

28
Branching
master
master

topic
HEAD
master

topic
HEAD
master

topic
master

topic
HEAD
master

topic
HEAD
master

topic
HEAD
Create a branch

32
Create a branch

$ git branch <branchname>

32
Create a branch

$ git branch <branchname>
$ git checkout <branchname>

32
Create a branch

$ git branch <branchname>
$ git checkout <branchname>
or

32
Create a branch

$ git branch <branchname>
$ git checkout <branchname>
or
$ git checkout -b <branchname>

32
Delete and list branch(es)

33
Delete and list branch(es)

$ git branch -d <branchname>
Delete branch.

33
Delete and list branch(es)

$ git branch -d <branchname>
Delete branch.
$ git branch
List branches.

33
Move between branches

34
Move between branches

$ git checkout <branchname>

34
Merge a branch back

35
Merge a branch back

$ git checkout <base-branch>

35
Merge a branch back

$ git checkout <base-branch>
$ git merge <topic-branch>

35
Branching Workflow

37
Branching Workflow
...

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)
...

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)
...

(4)

$ git commit

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)
...

(4)

$ git commit
# Back 3 until finish "topic"

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)
...

(4)

$ git commit
# Back 3 until finish "topic"

(5)

$ git checkout <base-branch>

37
Branching Workflow
...
(1)

$ git branch <topic-branch>

(2)

$ git checkout <topic-branch>

(3)

(modify file)
...

(4)

$ git commit
# Back 3 until finish "topic"

(5)

$ git checkout <base-branch>

(6)

$ git merge <topic-branch>
37
Remote Repository
Clone a remote repository

39
Clone a remote repository

$ git clone <repos> <dir>

39
Clone a remote repository

$ git clone <repos> <dir>
<repos>
https://
github.com/torvalds/linux.git

39
Create a remote repository

40
Create a remote repository

$ git init --bare

40
Create a remote repository

$ git init --bare
or

40
Create a remote repository

$ git init --bare
or
https://github.com/repositories/new

40
Add, remove and list remotes

41
Add, remove and list remotes

$ git remote add <name> <url>
<url> can be a local path.

41
Add, remove and list remotes

$ git remote add <name> <url>
<url> can be a local path.
$ git remote remove <name>

41
Add, remove and list remotes

$ git remote add <name> <url>
<url> can be a local path.
$ git remote remove <name>
$ git remote

41
Push commits to remote

42
Push commits to remote
$ git push <repos> <refspec>...

42
Push commits to remote
$ git push <repos> <refspec>...
<refspec>
usually is branch or tag name
[+]<src>[:<dst>]
:<dst> to delete remote reference.
42
Pull commits from remote

43
Pull commits from remote

$ git pull <repos> <refspec>...

43
A Co-working Workflow
Three Principles

45
Three Principles

1. master is production.

45
Three Principles

1. master is production.
2. dev only includes stable and reviewed code.

45
Three Principles

1. master is production.
2. dev only includes stable and reviewed code.
3. Create topic branch to resolve issue all the time.

45
Three Phases

46
Three Phases
1. Resolving
Create a topic branch to resolve issue.

46
Three Phases
1. Resolving
Create a topic branch to resolve issue.
2. Reviewing
Review the code.

46
Three Phases
1. Resolving
Create a topic branch to resolve issue.
2. Reviewing
Review the code.
3. Cleanup
Merge into dev and remove topic branch.
46
47
Assigner: "We need to resolve this issue."

47
Assigner: "We need to resolve this issue."
Assignee: "Yes, sir!"

47
Assigner: "We need to resolve this issue."
Assignee: "Yes, sir!"
Assignee (dev) $ git checkout -b topic

47
Assigner: "We need to resolve this issue."
Assignee: "Yes, sir!"
Assignee (dev) $ git checkout -b topic
Assignee (topic) $ (commit...)

47
Assigner: "We need to resolve this issue."
Assignee: "Yes, sir!"
Assignee (dev) $ git checkout -b topic
Assignee (topic) $ (commit...)
Assignee (topic) $ git push origin topic

47
Assigner: "We need to resolve this issue."
Assignee: "Yes, sir!"
Assignee (dev) $ git checkout -b topic
Assignee (topic) $ (commit...)
Assignee (topic) $ git push origin topic

Until resolve, call assigner for review.
47
48
Assignee: "I resolved!"

48
Assignee: "I resolved!"
Assigner: "Let me review."

48
Assignee: "I resolved!"
Assigner: "Let me review."
Assigner (dev) $ git checkout -b topic

48
Assignee: "I resolved!"
Assigner: "Let me review."
Assigner (dev) $ git checkout -b topic
Assigner (topic) $ git pull origin topic

48
Assignee: "I resolved!"
Assigner: "Let me review."
Assigner (dev) $ git checkout -b topic
Assigner (topic) $ git pull origin topic
Assigner (topic) $ git diff ...dev

48
Assignee: "I resolved!"
Assigner: "Let me review."
Assigner (dev) $ git checkout -b topic
Assigner (topic) $ git pull origin topic
Assigner (topic) $ git diff ...dev

If it is not good enough, call assignee to fix.
48
49
Assigner (topic) $ git checkout dev

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic
Assigner (dev) $ git push origin dev

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic
Assigner (dev) $ git push origin dev

Assigner: "Good job!"

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic
Assigner (dev) $ git push origin dev

Assigner: "Good job!"
Assignee (dev) $ git branch -d topic

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic
Assigner (dev) $ git push origin dev

Assigner: "Good job!"
Assignee (dev) $ git branch -d topic
Assignee (dev) $ git push origin :topic

49
Assigner (topic) $ git checkout dev
Assigner (dev) $ git merge topic
Assigner (dev) $ git push origin dev

Assigner: "Good job!"
Assignee (dev) $ git branch -d topic
Assignee (dev) $ git push origin :topic
Assignee (dev) $ git pull origin dev
49
End
End

52
End
Use branch!

52
End
Use branch!
Workflow does matter.

52
End
Use branch!
Workflow does matter.
Git still has many magics.

52
End
Use branch!
Workflow does matter.
Git still has many magics.
Tips: http://j.mp/mosky-gitconfig

52
End
Use branch!
Workflow does matter.
Git still has many magics.
Tips: http://j.mp/mosky-gitconfig
Q&A
52

Learning Git with Workflows