SlideShare a Scribd company logo
Git Introduction
1
為什麼要做版本控制??
2
LocalVersion Control Systems
• 在自己電腦裡,建立⼀一
個版本資料庫
• 最簡單的作法
Problem:如何協作??
3
CentralizedVersion Control Systems
• Server上,會儲存所有的
版本及記錄
• Checkout & Commit
4
DistributedVersion Control Systems
• 大家都有完整的資料庫
• 大家都能獨立工作
• Server壞了只要拿到⼀一份
完整的資料庫便可復原
• Git, Mecurial(hg),
bazaar(bzr)
Problem:相較於Centralized
混亂。
提供好的Branch機制解決
此問題。
5
https://help.github.com/articles/set-up-git
6
Set up Git
https://help.github.com/articles/set-up-git
7
$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
$ git config --global user.email "your_email@youremail.com"
# Sets the default email for git to use when you commit
$ git credential-osxkeychain
# Test for the cred helper
Usage: git credential-osxkeychain <get|store|erase>
8
$ git credential-osxkeychain
# Test for the cred helper
git: 'credential-osxkeychain' is not a git command. See 'git
--help'.
$ curl -s -O http://github-media-downloads.s3.amazonaws.com/
osx/git-credential-osxkeychain
# Download the helper
$ chmod u+x git-credential-osxkeychain
# Fix the permissions on the file so it can be run
$ sudo mv git-credential-osxkeychain /usr/local/git/bin
# Move the file so git can access it
# Password: [enter your password]
9
$ git config --global credential.helper osxkeychain
# Set git to use the osxkeychain credential helper
10
Get a Github account
https://github.com/
11
https://help.github.com/articles/generating-ssh-keys
Generate SSH Keys
12
$ cd ~/.ssh
$ ls
config id_rsa id_rsa.pub known_hosts
$ mkdir key_backup
$ cp id_rsa* key_backup
$ rm id_rsa*
$ ssh-keygen -t rsa -C "your_email@youremail.com"
# Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa):
[Press enter]
Enter passphrase (empty for no passphrase): [Type a
passphrase]
Enter same passphrase again: [Type passphrase again]
Your identification has been saved in /Users/you/.ssh/id_rsa.
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db
your_email@youremail.com
13
$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
14
$ ssh -T git@github.com
# Attempts to ssh to github
The authenticity of host 'github.com (207.97.227.239)' can't
be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:
56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?
Hi username! You've successfully authenticated, but GitHub
does not provide shell access.
15
Create a Repo
16
17
18
$ mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World"
in your user directory
$ cd ~/Hello-World
# Changes the current working directory to your newly
created directory
$ git init
# Sets up the necessary Git files
Initialized empty Git repository in /Users/you/Hello-
World/.git/
$ touch README
# Creates a file called "README" in your Hello-World
directory
19
$ git add README
# Stages your README file, adding it to the list of files to
be committed
$ git commit -m 'first commit'
# Commits your files, adding the message "first commit"
$ git remote add origin https://github.com/username/Hello-
World.git
# Creates a remote named "origin" pointing at your GitHub
repo
$ git push origin master
# Sends your commits in the "master" branch to GitHub
20
Fork A Repo
$ git clone git@github.com:oboegrace/git-101.git
https://help.github.com/articles/fork-a-repo
21
Git Basics
22
Snapshots, Not Differences
SVN
Git
23
Nearly Every Operation is Local
• 不只有⼀一份完整的資料
庫
• 閱讀版本歷史、提交
變更這些動作都可以
在本機進行
• 不需網路連線也可以工
作
24
Git Has Integrity
• 使用 Checksum 來確保檔案的完整性
• 設計上只會增加資料,因此可以輕鬆復
原
• 在本機的檔案管理中,增添了Staging的
觀念
25
TheThree States of Git
• Committed
• Data is safely stored in your local database.
• Modified
• You have changed the file but have not committed it to your
database yet.
• Staged
• You have marked a modified file in its current version to go
into your next commit snapshot.
26
The basic workflow of Git
1. You modify files in your
working directory.
2. You stage the files,
adding snapshots of
them to your staging
area.
3. You do a commit, which
takes the files as they
are in the staging area
and stores that snapshot
permanently to your Git
directory.
27
Using Git
28
Installing Git
• Installing on Mac
• http://code.google.com/p/git-osx-installer
• Installing on Windows
• http://code.google.com/p/msysgit
29
Initializing a Repository in an Existing Directory
• Repository就是⼀一份版本控制中心的版本
資料庫
• $ git init
• 資訊會放在.git資料夾裡
30
Initial Commit
• Add files
• And commit it!
$ touch README
$ git add .
$ git commit -m 'Initial commit'
31
Cloning an Existing Repository
$ git clone git://github.com/schacon/grit.git
Checking the Status ofYour Files
$ git status
# On branch master
nothing to commit (working directory clean)
Ignoring Files
$ cat .gitignore
*.[oa]
*~
32
CommittingYour Changes
$ git commit
Removing Files
$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
# On branch master
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: grit.gemspec
#
33
ChangingYour Last Commit
$ git commit --amend
Unstaging a Staged File
$ git reset HEAD aFile
Unmodifying a Modified File
$ git checkout -- aFile
34
Working with Remotes
• Branch 和 Remote 之間的互動
• 預設的 Branch 叫 Master
• 預設的 Remote 叫 Origin
• $ git pull origin :
$ git fetch origin
$ git merge origin/master
• $ git push origin master
push
35
What a Branch Is
36
Single commit repository data.
37
Git object data for multiple commits.
Branch pointing into the commit data’s history.
38
$ git branch testing
git branch <new_branch_name> 建立本地 local branch
git branch 列出目前有那些 branch 以及目前在那個 branch
39
$ git checkout testing
This moves HEAD to point to the testing branch
此時如果commit...
git checkout <branch_name> 切換 branch
40
41
$ git checkout master
42
$ git commit -a -m 'made other changes'
43
http://learn.github.com/p/branching.html
Practice Branching and Merging
44
如何重新變回⼀一條?
• Merge
• 把岔開來的分支在往後合起來
• 通常的建議方式
• Rebase
• 把岔開來的分支裝回去主線
• 還沒Push出去的東西才可以Rebase!
45
Basic Branching and Merging
46
有緊急的Bug,開了一個 hotfix branch 處理好了。
現在要怎麼處理 master?
47
$ git checkout master
$ git merge hotfix
48
此時處理完iss53,那該如何Merge?
49
$ git checkout master
$ git merge iss53
50
Basic Merge Conflicts
• git status ⼀一下看是哪個檔案出問題
• 到出問題的檔案找問題,手動解決
• 再檢查 git status 後,用 git commit 手動
Merge (會自動生成 Merge 的 Commit Message)
51
Rebase
52
除了Merge以外,我有沒有其他的辦法可以處理
掉 experiment branch?
53
$ git checkout experiment
$ git rebase master
54
$ git checkout master
$ git merge experiment
55
$ git clone git@github.com:oboegrace/git-101.git
Let’s practice it!
1. Set up git https://help.github.com/articles/set-up-git
2. Generate SSH Keys https://help.github.com/articles/generating-ssh-keys
3. Create a repo https://help.github.com/articles/create-a-repo
4. Fork a repo https://help.github.com/articles/fork-a-repo
5. Be social https://help.github.com/articles/be-social
6. Normal Workflow http://learn.github.com/p/normal.html
7. Branching and merging http://learn.github.com/p/branching.html
8. Distributed Git http://learn.github.com/p/remotes.html
9. Git history http://learn.github.com/p/log.html
10.Git reference http://gitref.org/
Reference
56

More Related Content

PPTX
Git - Basic Crash Course
PPT
Learn Git Basics
PDF
Git: An introduction of plumbing and porcelain commands
PDF
Git & GitHub for Beginners
PPTX
Gitting out of trouble
KEY
Basic Git
KEY
Git Basics at Rails Underground
PDF
Collaborative development with Git | Workshop
Git - Basic Crash Course
Learn Git Basics
Git: An introduction of plumbing and porcelain commands
Git & GitHub for Beginners
Gitting out of trouble
Basic Git
Git Basics at Rails Underground
Collaborative development with Git | Workshop

What's hot (20)

PDF
Git Real
PDF
Deep dark-side of git: How git works internally
PDF
Git Basics (Professionals)
PDF
Git - Get Ready To Use It
PPTX
Git One Day Training Notes
PDF
Git: basic to advanced
PPTX
PDF
Git real slides
PPT
Git training
PDF
Nicola Iarocci - Git stories from the front line - Codemotion Milan 2017
PDF
Git & Github for beginners
DOCX
Bitbucket
PDF
Github - Git Training Slides: Foundations
PDF
Git for beginners
PDF
Git internals
ODP
The Fundamentals of Git
PPTX
Git tutorial
PPTX
From svn to git
PPTX
Introduction to Git / Github
DOCX
Git github
Git Real
Deep dark-side of git: How git works internally
Git Basics (Professionals)
Git - Get Ready To Use It
Git One Day Training Notes
Git: basic to advanced
Git real slides
Git training
Nicola Iarocci - Git stories from the front line - Codemotion Milan 2017
Git & Github for beginners
Bitbucket
Github - Git Training Slides: Foundations
Git for beginners
Git internals
The Fundamentals of Git
Git tutorial
From svn to git
Introduction to Git / Github
Git github
Ad

Similar to 簡單介紹git (20)

PPT
390a gitintro 12au
PPTX
sample.pptx
PPTX
Git and Github workshop GDSC MLRITM
PPTX
Intro to Git DevOps Tally Presentation 101615
PPTX
Introduction to Git and Github
PDF
Git training v10
PDF
Git and github 101
PDF
Learning git
PDF
Gitting It Under (Version) Control
PPTX
Introduction To Git Workshop
PPTX
git.ppt.pptx power point presentation got Google internet
PPTX
Understanding about git
PDF
Git the Docs: A fun, hands-on introduction to version control
PPTX
github ppt git ppt on git hub to know ab
PPTX
Working in Team using Git in Unity
PPTX
Git session Dropsolid.com
PDF
PPTX
Get Good With Git
PPTX
Git walkthrough
390a gitintro 12au
sample.pptx
Git and Github workshop GDSC MLRITM
Intro to Git DevOps Tally Presentation 101615
Introduction to Git and Github
Git training v10
Git and github 101
Learning git
Gitting It Under (Version) Control
Introduction To Git Workshop
git.ppt.pptx power point presentation got Google internet
Understanding about git
Git the Docs: A fun, hands-on introduction to version control
github ppt git ppt on git hub to know ab
Working in Team using Git in Unity
Git session Dropsolid.com
Get Good With Git
Git walkthrough
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Cloud computing and distributed systems.

簡單介紹git

  • 3. LocalVersion Control Systems • 在自己電腦裡,建立⼀一 個版本資料庫 • 最簡單的作法 Problem:如何協作?? 3
  • 4. CentralizedVersion Control Systems • Server上,會儲存所有的 版本及記錄 • Checkout & Commit 4
  • 5. DistributedVersion Control Systems • 大家都有完整的資料庫 • 大家都能獨立工作 • Server壞了只要拿到⼀一份 完整的資料庫便可復原 • Git, Mecurial(hg), bazaar(bzr) Problem:相較於Centralized 混亂。 提供好的Branch機制解決 此問題。 5
  • 8. $ git config --global user.name "Your Name Here" # Sets the default name for git to use when you commit $ git config --global user.email "your_email@youremail.com" # Sets the default email for git to use when you commit $ git credential-osxkeychain # Test for the cred helper Usage: git credential-osxkeychain <get|store|erase> 8
  • 9. $ git credential-osxkeychain # Test for the cred helper git: 'credential-osxkeychain' is not a git command. See 'git --help'. $ curl -s -O http://github-media-downloads.s3.amazonaws.com/ osx/git-credential-osxkeychain # Download the helper $ chmod u+x git-credential-osxkeychain # Fix the permissions on the file so it can be run $ sudo mv git-credential-osxkeychain /usr/local/git/bin # Move the file so git can access it # Password: [enter your password] 9
  • 10. $ git config --global credential.helper osxkeychain # Set git to use the osxkeychain credential helper 10
  • 11. Get a Github account https://github.com/ 11
  • 13. $ cd ~/.ssh $ ls config id_rsa id_rsa.pub known_hosts $ mkdir key_backup $ cp id_rsa* key_backup $ rm id_rsa* $ ssh-keygen -t rsa -C "your_email@youremail.com" # Creates a new ssh key using the provided email Generating public/private rsa key pair. Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again] Your identification has been saved in /Users/you/.ssh/id_rsa. Your public key has been saved in /Users/you/.ssh/id_rsa.pub. The key fingerprint is: 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com 13
  • 14. $ pbcopy < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard 14
  • 15. $ ssh -T git@github.com # Attempts to ssh to github The authenticity of host 'github.com (207.97.227.239)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b: 56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? Hi username! You've successfully authenticated, but GitHub does not provide shell access. 15
  • 17. 17
  • 18. 18
  • 19. $ mkdir ~/Hello-World # Creates a directory for your project called "Hello-World" in your user directory $ cd ~/Hello-World # Changes the current working directory to your newly created directory $ git init # Sets up the necessary Git files Initialized empty Git repository in /Users/you/Hello- World/.git/ $ touch README # Creates a file called "README" in your Hello-World directory 19
  • 20. $ git add README # Stages your README file, adding it to the list of files to be committed $ git commit -m 'first commit' # Commits your files, adding the message "first commit" $ git remote add origin https://github.com/username/Hello- World.git # Creates a remote named "origin" pointing at your GitHub repo $ git push origin master # Sends your commits in the "master" branch to GitHub 20
  • 21. Fork A Repo $ git clone git@github.com:oboegrace/git-101.git https://help.github.com/articles/fork-a-repo 21
  • 24. Nearly Every Operation is Local • 不只有⼀一份完整的資料 庫 • 閱讀版本歷史、提交 變更這些動作都可以 在本機進行 • 不需網路連線也可以工 作 24
  • 25. Git Has Integrity • 使用 Checksum 來確保檔案的完整性 • 設計上只會增加資料,因此可以輕鬆復 原 • 在本機的檔案管理中,增添了Staging的 觀念 25
  • 26. TheThree States of Git • Committed • Data is safely stored in your local database. • Modified • You have changed the file but have not committed it to your database yet. • Staged • You have marked a modified file in its current version to go into your next commit snapshot. 26
  • 27. The basic workflow of Git 1. You modify files in your working directory. 2. You stage the files, adding snapshots of them to your staging area. 3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory. 27
  • 29. Installing Git • Installing on Mac • http://code.google.com/p/git-osx-installer • Installing on Windows • http://code.google.com/p/msysgit 29
  • 30. Initializing a Repository in an Existing Directory • Repository就是⼀一份版本控制中心的版本 資料庫 • $ git init • 資訊會放在.git資料夾裡 30
  • 31. Initial Commit • Add files • And commit it! $ touch README $ git add . $ git commit -m 'Initial commit' 31
  • 32. Cloning an Existing Repository $ git clone git://github.com/schacon/grit.git Checking the Status ofYour Files $ git status # On branch master nothing to commit (working directory clean) Ignoring Files $ cat .gitignore *.[oa] *~ 32
  • 33. CommittingYour Changes $ git commit Removing Files $ git rm grit.gemspec rm 'grit.gemspec' $ git status # On branch master # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: grit.gemspec # 33
  • 34. ChangingYour Last Commit $ git commit --amend Unstaging a Staged File $ git reset HEAD aFile Unmodifying a Modified File $ git checkout -- aFile 34
  • 35. Working with Remotes • Branch 和 Remote 之間的互動 • 預設的 Branch 叫 Master • 預設的 Remote 叫 Origin • $ git pull origin : $ git fetch origin $ git merge origin/master • $ git push origin master push 35
  • 36. What a Branch Is 36
  • 38. Git object data for multiple commits. Branch pointing into the commit data’s history. 38
  • 39. $ git branch testing git branch <new_branch_name> 建立本地 local branch git branch 列出目前有那些 branch 以及目前在那個 branch 39
  • 40. $ git checkout testing This moves HEAD to point to the testing branch 此時如果commit... git checkout <branch_name> 切換 branch 40
  • 41. 41
  • 42. $ git checkout master 42
  • 43. $ git commit -a -m 'made other changes' 43
  • 45. 如何重新變回⼀一條? • Merge • 把岔開來的分支在往後合起來 • 通常的建議方式 • Rebase • 把岔開來的分支裝回去主線 • 還沒Push出去的東西才可以Rebase! 45
  • 46. Basic Branching and Merging 46
  • 47. 有緊急的Bug,開了一個 hotfix branch 處理好了。 現在要怎麼處理 master? 47
  • 48. $ git checkout master $ git merge hotfix 48
  • 50. $ git checkout master $ git merge iss53 50
  • 51. Basic Merge Conflicts • git status ⼀一下看是哪個檔案出問題 • 到出問題的檔案找問題,手動解決 • 再檢查 git status 後,用 git commit 手動 Merge (會自動生成 Merge 的 Commit Message) 51
  • 54. $ git checkout experiment $ git rebase master 54
  • 55. $ git checkout master $ git merge experiment 55
  • 56. $ git clone git@github.com:oboegrace/git-101.git Let’s practice it! 1. Set up git https://help.github.com/articles/set-up-git 2. Generate SSH Keys https://help.github.com/articles/generating-ssh-keys 3. Create a repo https://help.github.com/articles/create-a-repo 4. Fork a repo https://help.github.com/articles/fork-a-repo 5. Be social https://help.github.com/articles/be-social 6. Normal Workflow http://learn.github.com/p/normal.html 7. Branching and merging http://learn.github.com/p/branching.html 8. Distributed Git http://learn.github.com/p/remotes.html 9. Git history http://learn.github.com/p/log.html 10.Git reference http://gitref.org/ Reference 56