Git Basics (Professionals)

245
Version Control with Git DevCon Git Code Camp for Professionals 15 March 2014 http://devcon.ph/events/git-code-camp-for-professionals

description

Slides from DevCon Git Code Camp for Professionals, 15 March 2014 http://devcon.ph/events/git-code-camp-for-professionals

Transcript of Git Basics (Professionals)

Page 1: Git Basics (Professionals)

Version Controlwith Git

DevCon Git Code Camp for Professionals15 March 2014

http://devcon.ph/events/git-code-camp-for-professionals

Page 2: Git Basics (Professionals)

Coverage

This workshop will cover the basics of Git and how to integrate Git and Github into

your day-to-day work.

Slides: https://speakerdeck.com/bryanbibat/git-basics-professionals

Page 3: Git Basics (Professionals)

InstallationWindows: http://msysgit.github.io/

Mac: http://code.google.com/p/git-osx-installer

Linux (Debian/Ubuntu):apt-get install git-core gitk

Linux (Fedora):yum install git gitk

Page 4: Git Basics (Professionals)

Verify installationWindows: Open "Git Bash"

Mac/Linux: Open your terminal

To verify, run:$ git --version

Page 5: Git Basics (Professionals)

Yes, we are going to use the command line today

Page 6: Git Basics (Professionals)

Yes, we are going to use the command line today

But I'll also demo how you can use GUI clients.

Page 7: Git Basics (Professionals)

GUI InstallationTortoiseGit:

https://code.google.com/p/tortoisegit/

Github for Windows:http://windows.github.com/

Github for Mac:http://mac.github.com/

Others:http://git-scm.com/downloads/guis

Page 8: Git Basics (Professionals)

Yes, we are going to use the command line today

Git features not available in some GUI clients are marked

!

Page 9: Git Basics (Professionals)

Initial Setup$ git config --global user.name "Your Name"$ git config --global user.email "[email protected]"

Windows:

$ git config --global core.autocrlf true$ git config --global core.safecrlf true

Linux/Mac:

$ git config --global core.autocrlf input$ git config --global core.safecrlf true

Page 10: Git Basics (Professionals)

Review...

Page 11: Git Basics (Professionals)

Version Control

Page 12: Git Basics (Professionals)

Version Controlaka Revision Control

Page 13: Git Basics (Professionals)

Version Controlaka Revision Control

aka How we do things in the Real World

Page 14: Git Basics (Professionals)

Sharing Code

Page 15: Git Basics (Professionals)

Working as a Team

Page 16: Git Basics (Professionals)
Page 17: Git Basics (Professionals)

Version Control

Page 18: Git Basics (Professionals)

Why Version Control?

Page 19: Git Basics (Professionals)

Reason #1:"Versioning"

Page 20: Git Basics (Professionals)
Page 21: Git Basics (Professionals)
Page 22: Git Basics (Professionals)

Wait a minute...

Page 23: Git Basics (Professionals)
Page 24: Git Basics (Professionals)

Finer-grained Control

Page 25: Git Basics (Professionals)
Page 26: Git Basics (Professionals)

Enough talk. Let's begin...

Page 27: Git Basics (Professionals)

Create your first repository

$ mkdir devcon-git101$ cd devcon-git101$ git init

Page 28: Git Basics (Professionals)

Create your first repository

$ mkdir devcon-git101$ cd devcon-git101$ git init

Page 29: Git Basics (Professionals)

Create your first repository

$ mkdir devcon-git101$ cd devcon-git101$ git init

Here we create a project folder

Page 30: Git Basics (Professionals)

Create your first repository

$ mkdir devcon-git101$ cd devcon-git101$ git init … Then we initialize it as a git repository.

Page 31: Git Basics (Professionals)

Create your first repository

$ mkdir devcon-git101$ cd devcon-git101$ git init … Then we initialize it as a git repository.

Git can now track the changes inside ourproject folder.

Page 32: Git Basics (Professionals)

Create your first commit

First create a file "hello.txt" containing:

Hello

Then run the following commands:

$ git add hello.txt$ git commit -m "Initial Commit"

Page 33: Git Basics (Professionals)

View the repository history

$ git log

(press q to exit)

Page 34: Git Basics (Professionals)

View the pretty repo history

$ git log --graph --pretty=oneline

(press q to exit)

Page 35: Git Basics (Professionals)

Ah, what the hell...

Windows/Linux:

$ gitk

Mac:

$ gitx

Page 36: Git Basics (Professionals)

Create your second commit

Modify "hello.txt" to add "world":

Hello World!

Then run the following commands:

$ git add hello.txt$ git commit -m "Make hello.txt more exciting"

Page 37: Git Basics (Professionals)

View the updated history

Windows/Linux:

$ gitk

Mac:

$ gitx

Page 38: Git Basics (Professionals)

What just happened?

Page 39: Git Basics (Professionals)

http://git-scm.com/book/en/Getting-Started-Git-Basics

Page 40: Git Basics (Professionals)

http://git-scm.com/book/en/Getting-Started-Git-Basics

git add

git commit

Page 41: Git Basics (Professionals)

Initial Commit6fba518

Page 42: Git Basics (Professionals)

Initial Commit6fba518

object nameaka object id, commit hash

● SHA-1 hash / checksum for verifying the integrity of the contents of the commit● Calculated based on file contents and metadata like last updated date i.e. yours

will be different

Page 43: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Page 44: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

No, this is not a mistake;commits refer to their parent(s), not the other way around.

Page 45: Git Basics (Professionals)

Commit multiple files

Create a file "names.txt" containing:

Alice Bob Cindy

Page 46: Git Basics (Professionals)

Commit multiple files

Create a file "numbers.txt" containing:

3 9 16 12 8.2 4

Page 47: Git Basics (Professionals)

Commit multiple files

Run the following commands:

$ git add names.txt numbers.txt$ git commit -m "Create 2 files in a single commit"

Page 48: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Page 49: Git Basics (Professionals)

Initial Commit1 file created

6fba518

Make hello.txt more exciting1 file modified

e642771

Create 2 files in a single commit2 files created

7c57165

Each commit deals with a set of files

Page 50: Git Basics (Professionals)

We've covered "Save", but before we move on to

"Load"...

Page 51: Git Basics (Professionals)

http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository

Page 52: Git Basics (Professionals)

File Status(all unmodified)

$ git status# On branch masternothing to commit, working directory clean

Page 53: Git Basics (Professionals)

File Status(untracked)

Create a file "animals.txt" containing:

Dogs Cats Mice

Page 54: Git Basics (Professionals)

File Status(untracked)

$ git status# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## animals.txtnothing added to commit but untracked files present (use "git add" to track)

Page 55: Git Basics (Professionals)

File Status(untracked and modified)

Modify "names.txt" to add "Janet":

Alice Bob Janet Cindy

Page 56: Git Basics (Professionals)

File Status(untracked and modified)

$ git status# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: names.txt## Untracked files:# (use "git add <file>..." to include in what will be committed)## animals.txtno changes added to commit (use "git add" and/or "git commit -a")

Page 57: Git Basics (Professionals)

File Status(untracked and staged)

$ git add names.txt$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: names.txt## Untracked files:# (use "git add <file>..." to include in what will be committed)## animals.txt

!

Page 58: Git Basics (Professionals)

File Status(commit staged)

$ git commit -m "Add Janet"[master 5e545ed] Add Janet 1 file changed, 1 insertion(+)

$ git status# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## animals.txt

Page 59: Git Basics (Professionals)

Shortcuts

Page 60: Git Basics (Professionals)

Stage a folder

Modify "names.txt" to add "Ramon":

Alice Ramon Bob Janet Cindy

Page 61: Git Basics (Professionals)

Stage a folder

$ git add .$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## new file: animals.txt# modified: names.txt#

!

Page 62: Git Basics (Professionals)

Unstage a file

$ git reset HEAD names.txtUnstaged changes after reset:M names.txt

$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## new file: animals.txt## Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: names.txt#

!

Page 63: Git Basics (Professionals)

Unmodify a file

$ git checkout -- names.txt

$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## new file: animals.txt#

Page 64: Git Basics (Professionals)

do it again...

Modify "names.txt" to add "Ramon":

Alice Ramon Bob Janet Cindy

Page 65: Git Basics (Professionals)

Stage and commit EVERYTHING

(except untracked files)

$ git commit -a -m "Commit unrelated changes.. DON'T DO THIS"[master 61f1cd8] Commit unrelated changes.. DON'T DO THIS 2 files changed, 4 insertions(+) create mode 100644 animals.txt

$ git status# On branch masternothing to commit, working directory clean

Note: using "-a" will also stage moved and renamed files.

Page 66: Git Basics (Professionals)

Amend last commit

$ git commit -m "Commit unrelated changes... DON'T DO THIS" --amend[master 3a0eac3] Commit unrelated changes... DON'T DO THIS 2 files changed, 4 insertions(+) create mode 100644 animals.txt

!

Page 67: Git Basics (Professionals)

On to "Load"...

Page 68: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 69: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Commit unrelated changes... DON'T DO THIS3a0eac3

HEAD

Page 70: Git Basics (Professionals)

Create a reverting commit

$ git revert HEAD --no-edit[master 2a1b52e] Revert "Commit unrelated changes... DON'T DO THIS" 2 files changed, 4 deletions(-) delete mode 100644 animals.txt

Page 71: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Commit unrelated changes... DON'T DO THIS3a0eac3

HEAD

Page 72: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 73: Git Basics (Professionals)

Move HEAD to a commitdiscarding changes

$ git reset --hard 7c57165 HEAD is now at 7c57165 Create 2 files in a single commit

(You can use the first few characters of the object ID instead of the 40 characters)

Page 74: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 75: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 76: Git Basics (Professionals)

View history

$ git log --graph --pretty=format:'%h %s%d' --all* 7c57165 Create 2 files in a single commit (HEAD, master)* e642771 Make hello.txt more exciting* 6fba518 Initial Commit

or

$ gitk --all

Page 77: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

HEAD

Page 78: Git Basics (Professionals)

Move HEAD to a commit

$ git reset --hard 2a1b52e HEAD is now at 2a1b52e Revert "Commit unrelated changes... DON'T DO THIS"

!

Page 79: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

HEAD

Page 80: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 81: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 82: Git Basics (Professionals)

Unreferenced commits will not show up in the history.

Coincidentally, we can use Tags to refer to a commit.

Page 83: Git Basics (Professionals)

Tagging

$ git tag tagging-demo

!

Page 84: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

Page 85: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

tagging-demo

Page 86: Git Basics (Professionals)

Going back...

$ git reset --hard 7c57165 HEAD is now at 7c57165 Create 2 files in a single commit

!

Page 87: Git Basics (Professionals)

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

Add Janet5e545ed

Revert "Commit unrelated changes... DON'T DO THIS"

2a1b52e

HEAD

Commit unrelated changes... DON'T DO THIS3a0eac3

tagging-demo

Page 88: Git Basics (Professionals)

View history

$ git log --graph --pretty=format:'%h %s%d' --all* 2a1b52e Revert "Commit unrelated changes... DON'T DO THIS" (tagging-demo)* 3a0eac3 Commit unrelated changes... DON'T DO THIS* 5e545ed Add Janet* 7c57165 Create 2 files in a single commit (HEAD, master)* e642771 Make hello.txt more exciting* 6fba518 Initial Commit

or

$ gitk --all

Page 89: Git Basics (Professionals)

Wrapping up, tag current...

$ git tag end-part1

!

Page 90: Git Basics (Professionals)

...and unreference PDAF

$ git tag -d tagging-demoDeleted tag 'tagging-demo' (was 2a1b52e)

Page 91: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

HEAD end-part1

Page 92: Git Basics (Professionals)

Best Practices

Page 93: Git Basics (Professionals)

Write good commit messages

You should be able to get an idea what changed and why just by looking at the commit messages

Page 94: Git Basics (Professionals)

Commit related changes

This would make it easier to roll back changes

Page 95: Git Basics (Professionals)

Commit often

Page 96: Git Basics (Professionals)

Do not commit generated files

E.g. compiled bytecode or executables, log files, temporary files, etc.

Page 97: Git Basics (Professionals)

Do not commit sensitive information

E.g. passwords, settings.You can, however, commit templates.

Page 98: Git Basics (Professionals)

Helpful Stuffthat we will not explain in detail...

Page 99: Git Basics (Professionals)

.gitignore

https://github.com/github/gitignore

Page 100: Git Basics (Professionals)

git stash

Page 101: Git Basics (Professionals)

git blame

Page 102: Git Basics (Professionals)

Summary of Commands

git init - initialize repository

git add - add files/folders to staging

git commit - commit files in staging

git status - view status of repository

git log / gitk - view history of repository

git revert - unstage files

git reset - move HEAD to another commit

git tag - tag a commit

Page 103: Git Basics (Professionals)

Summary of Command Variations

git commit -a - auto-stage all deleted, moved, modified

gitk --all / git log --all - include all branches, tags

git tag -d - delete tag

Page 104: Git Basics (Professionals)

Why Version Control?

Page 105: Git Basics (Professionals)

Reason #2:Backup

Page 106: Git Basics (Professionals)

Reason #2:Backup

Page 107: Git Basics (Professionals)

Reason #2:Collaboration

Page 108: Git Basics (Professionals)

If we were pressed for time...

● Register at GitHub● Learn git clone● Learn git push / git pull● Learn how to fix merge conflicts

Page 109: Git Basics (Professionals)

...but we're not, so let's first discuss something that will

help us later on.

Page 110: Git Basics (Professionals)

Branching

Page 111: Git Basics (Professionals)

File Status(all unmodified)

$ git status# On branch masternothing to commit, working directory clean

Page 112: Git Basics (Professionals)

File Status(all unmodified)

$ git status# On branch masternothing to commit, working directory clean

Page 113: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

HEAD end-part1

Page 114: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

master end-part1

HEAD

Page 115: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

master end-part1

HEAD

in other version controlsystems, master is called

trunk

Page 116: Git Basics (Professionals)

Create a branch

$ git branch testing

Page 117: Git Basics (Professionals)

Initial Commit6fba518

Make hello.txt more excitinge642771

Create 2 files in a single commit7c57165

master end-part1

HEAD

Page 118: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testing

Page 119: Git Basics (Professionals)

Switch to branch

$ git checkout testingSwitched to branch 'testing'

$ git status# On branch testingnothing to commit, working directory clean

Page 120: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testing

Page 121: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testing

Page 122: Git Basics (Professionals)

Commit to new branch

Modify "names.txt" to add "Eve":

Alice Bob Cindy Eve

Page 123: Git Basics (Professionals)

Commit to new branch

$ git commit -am "Add Eve"[testing cdd47c2] Add Eve 1 file changed, 1 insertion(+)

$ gitk

Page 124: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testing

Page 125: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testingcdd47c2

Page 126: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testingcdd47c2

Page 127: Git Basics (Professionals)

Switch back to master

$ git checkout masterSwitched to branch 'master'

Page 128: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

testingcdd47c2

Page 129: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2

HEAD

Page 130: Git Basics (Professionals)

Difference between log --all vs normal log

$ gitk

Page 131: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

HEAD

Page 132: Git Basics (Professionals)

Difference between log --all vs normal log

$ gitk --all

Page 133: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2

HEAD

Page 134: Git Basics (Professionals)

Commit to master

Modify "names.txt" to add "Billy":

Alice Billy Bob Cindy

$ git commit -am "Add Billy"[master cc3044c] Add Billy 1 file changed, 1 insertion(+)

Page 135: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2

HEAD

Page 136: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2

HEAD

cc3044c

Page 137: Git Basics (Professionals)

Shortcut: create and switch

$ git checkout -b testing2Switched to branch 'testing2'

Page 138: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2

HEAD

cc3044c

Page 139: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEAD

testing2

Page 140: Git Basics (Professionals)

Commit to testing2

Modify "names.txt" to add "Dave":

Alice Billy Bob Cindy Dave

$ git commit -am "Add Dave"[testing2 80414cf] Add Dave 1 file changed, 1 insertion(+)

Page 141: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEAD

testing2

Page 142: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEADtesting2

80414cf

Page 143: Git Basics (Professionals)

yay, mukha nang puno

Page 144: Git Basics (Professionals)

Go back to master

$ git checkout masterSwitched to branch 'master'

$ gitk --all

Page 145: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEADtesting2

80414cf

Page 146: Git Basics (Professionals)

Merge another branch

$ git merge testingAuto-merging names.txtMerge made by the 'recursive' strategy. names.txt | 1 + 1 file changed, 1 insertion(+)

Page 147: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEADtesting2

80414cf

Page 148: Git Basics (Professionals)

6fba518

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEADtesting2

80414cff56f4fa Merge branch 'testing'

Page 149: Git Basics (Professionals)

Merge Conflict

$ git merge testing2Auto-merging names.txtCONFLICT (content): Merge conflict in names.txtAutomatic merge failed; fix conflicts and then commit the result.

Page 150: Git Basics (Professionals)

Merge Conflict

Open names.txt:

Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2

Page 151: Git Basics (Professionals)

Merge Conflict

Open names.txt:

Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2

version from HEAD i.e master

Page 152: Git Basics (Professionals)

Merge Conflict

Open names.txt:

Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2

version from testing2

Page 153: Git Basics (Professionals)

Resolving Merge Conflict

Edit names.txt removing the markers:

Alice Billy Bob Cindy Dave Eve

Page 154: Git Basics (Professionals)

Resolving Merge Conflict

Commit the resolved merge conflict

$ git commit -am "Merge branch 'testing2' and fix conflict"[master 07e83b3] Merge branch 'testing2' and fix conflict

Page 155: Git Basics (Professionals)

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEADtesting2

80414cff56f4fa Merge branch 'testing'

Page 156: Git Basics (Professionals)

e642771

7c57165

master

end-part1

testingcdd47c2cc3044c

HEAD

testing2

80414cff56f4fa Merge branch 'testing'

07e83b3 Merge branch 'testing2' and fix conflict

Page 157: Git Basics (Professionals)

Another way to merge:Rebasing

Page 158: Git Basics (Professionals)

Working on two branches

$ git checkout -b merging-demoSwitched to a new branch 'merging-demo'

Page 159: Git Basics (Professionals)

Commit to merging-demo

Modify "names.txt" to remove "Alice":

Billy Bob Cindy Dave Eve

$ git commit -am "Remove Alice"[merging-demo 00b26cb] Remove Alice 1 file changed, 1 deletion(-)

Page 160: Git Basics (Professionals)

Commit to merging-demo

Modify "names.txt" to remove "Cindy":

Billy Bob Dave Eve

$ git commit -am "Remove Cindy"[merging-demo b115e79] Remove Cindy 1 file changed, 1 deletion(-)

Page 161: Git Basics (Professionals)

Switch back to master

$ git checkout masterSwitched to branch 'master'

Page 162: Git Basics (Professionals)

Commit to master

Modify "numbers.txt" to remove "8.2":

3 9 16 12 4

$ git commit -am "Remove 8.2"[master 0c1f192] Remove 8.2 1 file changed, 1 deletion(-)

Page 163: Git Basics (Professionals)

Commit to master

Modify "numbers.txt" to remove "9":

3 16 12 4

$ git commit -am "Remove 9"[master bc3583d] Remove 9 1 file changed, 1 deletion(-)

Page 164: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb 0c1f192

bc3583d

Page 165: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb 0c1f192

bc3583d

Merging

aff102e

Page 166: Git Basics (Professionals)

Rebasing

$ git rebase merging-demoFirst, rewinding head to replay your work on top of it...Applying: Remove 8.2Applying: Remove 9

!

Page 167: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb 0c1f192

bc3583d

Page 168: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb 0c1f192

bc3583d

Remove 8.2

Remove 9

Page 169: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb

67b81ce

73dd819

Remove 8.2

Remove 9

Page 170: Git Basics (Professionals)

master

b115e79

07e83b3

HEAD

merging-demo

00b26cb

67b81ce

73dd819

Remove 8.2

Remove 9

Page 171: Git Basics (Professionals)

Later na lang yungmerge vs rebase

Page 172: Git Basics (Professionals)

Malapit na tayo mag-Github, but first...

Page 173: Git Basics (Professionals)

Remote Repositories

Page 174: Git Basics (Professionals)

Github for Windows only supports Github

Page 175: Git Basics (Professionals)

Clone into another folder

$ cd ..

$ git clone devcon-git101 git101Cloning into 'git101'...done.

Page 176: Git Basics (Professionals)

Check the clone repo

$ cd git101

$ gitk

Page 177: Git Basics (Professionals)

merging-demo

master

HEAD

/devcon-git101

merging-demo

master

HEAD

/git101

Page 178: Git Basics (Professionals)

merging-demo

master

HEAD

/devcon-git101

merging-demo

master HEAD

/git101

remotes/origin/merging-demo

remotes/origin/master

Page 179: Git Basics (Professionals)

Show remote repos

$ git remoteorigin

Page 180: Git Basics (Professionals)

"origin" remote repo(default repo referring to the repo's origin)

$ git remote show origin* remote origin Fetch URL: c:/Users/user/devcon-git101 Push URL: c:/Users/user/devcon-git101 HEAD branch: master Remote branches: master tracked merging-demo tracked testing tracked testing2 tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)

Page 181: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 182: Git Basics (Professionals)

Synchronizing withRemote Repos

Page 183: Git Basics (Professionals)

Fetching demo

Go back to /devcon-git101 (either open a new terminal/Git Bash window) and modify "names.txt" to add Greg:

Billy Bob Dave Eve Greg $ git commit -am "Add Greg"[master cf5f902] Add Greg 1 file changed, 1 insertion(+)

Page 184: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 185: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 186: Git Basics (Professionals)

Fetching demo

Go back to git101 and fetch the changes:

$ cd ../git101

$ git fetchremote: Counting objects: 5, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From c:/Users/user/devcon-git101 73dd819..cf5f902 master -> origin/master

Page 187: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 188: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 189: Git Basics (Professionals)

Merge the fetched branch

$ git merge origin/masterUpdating 73dd819..cf5f902Fast-forward names.txt | 1 + 1 file changed, 1 insertion(+)

Page 190: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

Page 191: Git Basics (Professionals)

master

HEAD

/devcon-git101

master HEAD

/git101

remotes/origin/master

A fast-forward occurs when you merge a branch which has HEAD as an ancestor.In this case, only the references are affected.

Page 192: Git Basics (Professionals)

Shortcut

$ git fetch$ git merge origin/master

is equivalent to

$ git pull

if you're in master branch. We'll discuss more of this later.

Page 193: Git Basics (Professionals)

Now that we understand the basic idea behind remote repos,

let's proceed to Github...

Sign-up at https://github.com

Page 194: Git Basics (Professionals)

Create a repo in Github fordevcon-git101

https://github.com/new

Page 195: Git Basics (Professionals)

Pushing devcon-git101Go back to devcon-git101 and push it to your new repo (this uses SSH url, you can use HTTPS)

$ git remote add origin [email protected]:user/devcon-git101.git

$ git push -u origin masterWarning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.Enter passphrase for key '/c/Users/user/.ssh/id_rsa':Counting objects: 40, done.Delta compression using up to 4 threads.Compressing objects: 100% (24/24), done.Writing objects: 100% (40/40), 3.41 KiB, done.Total 40 (delta 3), reused 0 (delta 0)To [email protected]:user/devcon-git101.git * [new branch] master -> masterBranch master set up to track remote branch master from origin.

Page 196: Git Basics (Professionals)

git pushNote that "git push" only pushes a single branch.

If you want to push all branches you can use:

$ git push REMOTE --all

If you want to push all tags you can use:

$ git push REMOTE --tags

Page 197: Git Basics (Professionals)

Clone into another folder

$ cd ..

$ git clone [email protected]:user/devcon-git101.git github-git101Cloning into 'github-git101'...remote: Counting objects: 40, done.remote: Compressing objects: 100% (21/21), done.remote: Total 40 (delta 3), reused 40 (delta 3)Receiving objects: 100% (40/40), done.Resolving deltas: 100% (3/3), done.done.

Page 198: Git Basics (Professionals)

play time

With the cloned folder, you can now play around with certain collaboration scenarios. For example:

1. Make some changes in /devcon-git1012. Commit it via "git commit"3. Push it to the repo at Github via "git push"4. Go to /github-git101 and pull the changes via "git pull"

Page 199: Git Basics (Professionals)

Want to let someone else push to your repo?

Go to the repo Settings →Collaborators and add your friends.

Page 200: Git Basics (Professionals)

Push/Pull

You'll quickly notice that git push only allows fast-forward changes to be pushed.

In simpler terms, you cannot push until you pull the latest changes from the remote.

Page 201: Git Basics (Professionals)

pull + rebase

Want to get rid of the distracting merge commits whenever you pull when you have pending commits? Tell git to rebase instead of merging:

$ git pull --rebase origin master

Page 202: Git Basics (Professionals)

Github for Windows automatically uses git pull --rebase + git push when

syncing

Page 203: Git Basics (Professionals)

merge vs rebase

The problem with rebase is that you can overwrite commits already on the remote repository and this can affect your team.

That said, people generally use rebase when working together on a single branch (e.g. git pull --rebase origin master) and merge when merging branches.

Page 204: Git Basics (Professionals)

Tired of entering your password?

There are multiple ways to authenticate users in Git.

Let's discuss the more secure alternative: via SSH keys.

Page 205: Git Basics (Professionals)

Cryptography pasakalye...

Page 206: Git Basics (Professionals)

Authentication via Password

Client Server

Page 207: Git Basics (Professionals)

Authentication via Password

Client sendscredentials

Server verifies

username + password

passwordDB

Page 208: Git Basics (Professionals)

Authentication via Password

Client sendscredentials

Server verifies

username + password

passwordDB

can be interceptede.g. FTP

Page 209: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

Client has 2 "keys" Server

private key

public key

Page 210: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

Client has 2 "keys"Server has a list of

authorized keys

private key

public key

Page 211: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

Client has 2 "keys"Server has a list of

authorized keys

private key

public key

First, the public key must be sent to the server securely beforehand.

Page 212: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

ClientServer has a list of

authorized keys

private key

public key

Using the private key, client can create a package that can only be unlocked by the corresponding public key

and only that public key.

Page 213: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

ClientServer has a list of

authorized keys

private key

public key

Using the private key, client can create a package that can only be unlocked by the corresponding public key

and only that public key.

Page 214: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

ClientServer has a list of

authorized keys

private key

public key

Using the private key, client can create a package that can only be unlocked by the corresponding public key

and only that public key.

Page 215: Git Basics (Professionals)

Authentication via SSH Key(oversimplified)

ClientServer has a list of

authorized keys

private key

public key

Using the private key, client can create a package that can only be unlocked by the corresponding public key

and only that public key.

doesn't matter ifintercepted sincethey still need to

crack it.

Page 216: Git Basics (Professionals)

How to generate your SSH Keys:

https://help.github.com/articles/generating-ssh-keys

Also, follow the steps to add your public key to Github.

Page 217: Git Basics (Professionals)

If you want to be secure, use a passphrase.

However, if you're using Windows, you will need to enter it every time you run a git command that connects to Github.

Page 218: Git Basics (Professionals)

Fortunately, there is a workaround that will only require you to enter it

once per session.

http://stackoverflow.com/a/9011152

Page 219: Git Basics (Professionals)

Integrating Git with your Projects

Page 220: Git Basics (Professionals)

Hosting Solutions

Page 221: Git Basics (Professionals)

Free Private ReposSince Github is only free for public repos, you might want to look into the following to keep your source code private:

Assembla (https://www.assembla.com)- 1 private repo, limited to 3 collaborators

BitBucket (https://bitbucket.org/)- Unlimited private repos, limited to 5 collaborators

Github Educational Accounts (https://github.com/edu)- free accounts for students and teachers

Page 222: Git Basics (Professionals)

Self-Hosted ReposIf you've got spare time and spare servers, you can also host your own git repositories:

GitLab (https://www.gitlab.com/)- GitHub clone written in Ruby

GitBucket (https://github.com/takezoe/gitbucket)- GitHub clone written in Scala

You can also read the docs for other self-hosting options

http://git-scm.com/book/en/Git-on-the-Server

Page 223: Git Basics (Professionals)

Project Workflow

Page 224: Git Basics (Professionals)

Project Workflow

Here's the most basic workflow for working with with others through version control systems:

1. Double check if the project work e.g. compiles, pages load, etc.2. Stage and commit your changes.3. Before pushing, pull changes from the remote project repo. If there are no changes, skip to step 5.4. Resolve conflicts, if any, then go back to step 1.5. Push your changes.

Page 225: Git Basics (Professionals)

Branches + Workflow

There are two main camps in using branches for projects using Git:

1. Mainlinee.g. git-flow (http://nvie.com/posts/a-successful-git-branching-model/)

2. Trunk-basede.g. how Github does it (https://gist.github.com/17twenty/6733076)

Page 226: Git Basics (Professionals)

Branches + Workflow

Client - Server VCSvs

Distributed VCS

Page 227: Git Basics (Professionals)

Branches + Workflow

Push-basedvs

Pull-based

Page 228: Git Basics (Professionals)

Communicate!Version control systems aims to improve communication between team members, not replace it.

Always consult with the other person whenever you encounter a merge conflict.

Page 229: Git Basics (Professionals)

Broken Builds

To repeat step 1:

1. Double check if the project work e.g. compiles, pages load, etc.

Broken builds are what we call published commits that have obvious critical, show-stopping bugs.

That said, it's better to delay pushing your commits to spend more time making sure that the project works rather than waste everyone's time.

Page 230: Git Basics (Professionals)

Continuous Integration

You can use CI servers to automatically inform you of broken builds. Most CI servers nowadays support Git and its post-commit hooks to automatically test on a push. Some examples:

● Jenkins Git Plugin https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

● TeamCity Git docs http://confluence.jetbrains.com/display/TCD8/Git+(JetBrains)

Page 231: Git Basics (Professionals)

Work in Progress

Avoid publishing half-done work as it can lead to broken builds.

If you need to push those changes (e.g. you want a backup), put them in a branch then push that branch. Or consider using git stash.

Page 232: Git Basics (Professionals)

Backups

Don't rely on Git as a backup scheme; always have an external backup

Page 233: Git Basics (Professionals)

Deployment

Page 234: Git Basics (Professionals)

Deployment Automation

e.g. Capistranohttp://capistranorb.com/

Page 235: Git Basics (Professionals)

Push-basedPlatform-as-a-Service

e.g. Pagoda Boxhttps://pagodabox.com/,

Herokuhttps://www.heroku.com/

Page 236: Git Basics (Professionals)

Github stuff

Page 237: Git Basics (Professionals)

Github Webhooks

https://developer.github.com/webhooks/

Page 238: Git Basics (Professionals)

GitHub as a Project Management Tool

Page 239: Git Basics (Professionals)

GitHub as a Project Management Tool forOpen Source Projects

Page 240: Git Basics (Professionals)

GitHub as a Portfolio

Page 241: Git Basics (Professionals)

Summary of Commands

git branch - list, create, or delete branches

git checkout - checkout a branch or a path

git merge - merge two or more branches

git rebase - move commits to the end of a branch

git clone - make a local copy of a repository

git remote - list, create, or delete remote repos

git fetch - retrieve objects/changes from a repository

git pull - fetch + merge or rebase

git push - publish local commits to a remote

Page 242: Git Basics (Professionals)

Summary of Command Variations

git checkout -b - create and checkout branch

git remote add - add a new remote repository to track

git remote rm - remove remote repository

git pull --rebase - rebase instead of merge when pulling

Page 243: Git Basics (Professionals)

Commands that I use everyday

git pull --rebase

git add

git commit -am "blah blah"

gitk / git log / git status / git diff

git push

Page 244: Git Basics (Professionals)

Commands that I use less often

git clone

git remote add

git remote -v

git checkout <branch>

git rebase <branch> / git merge <branch>

git checkout -- <path>

git reset --hard <hash>

git revert

Page 245: Git Basics (Professionals)

Thank You For Listening!