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)
P...
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 --glo...
$ git credential-osxkeychain
# Test for the cred helper
git: 'credential-osxkeychain' is not a git command. See 'git
--hel...
$ 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...
$ 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 establ...
Create a Repo
16
17
18
$ mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World"
in your user directory
$ cd ~/Hello-Worl...
$ git add README
# Stages your README file, adding it to the list of files to
be committed
$ git commit -m 'first commit'
...
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 bu...
The basic workflow of Git
1. You modify files in your
working directory.
2. You stage the files,
adding snapshots of
them to...
Using Git
28
Installing Git
• Installing on Mac
• http://code.google.com/p/git-osx-installer
• Installing on Windows
• http://code.goog...
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...
CommittingYour Changes
$ git commit
Removing Files
$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
# On branch master...
ChangingYour Last Commit
$ git commit --amend
Unstaging a Staged File
$ git reset HEAD aFile
Unmodifying a Modified File
$...
Working with Remotes
• Branch 和 Remote 之間的互動
• 預設的 Branch 叫 Master
• 預設的 Remote 叫 Origin
• $ git pull origin :
$ git fetch...
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 的 ...
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-...
Upcoming SlideShare
Loading in …5
×

簡單介紹git

644 views

Published on

簡單介紹git

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
644
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

簡單介紹git

  1. 1. Git Introduction 1
  2. 2. 為什麼要做版本控制?? 2
  3. 3. LocalVersion Control Systems • 在自己電腦裡,建立⼀一 個版本資料庫 • 最簡單的作法 Problem:如何協作?? 3
  4. 4. CentralizedVersion Control Systems • Server上,會儲存所有的 版本及記錄 • Checkout & Commit 4
  5. 5. DistributedVersion Control Systems • 大家都有完整的資料庫 • 大家都能獨立工作 • Server壞了只要拿到⼀一份 完整的資料庫便可復原 • Git, Mecurial(hg), bazaar(bzr) Problem:相較於Centralized 混亂。 提供好的Branch機制解決 此問題。 5
  6. 6. https://help.github.com/articles/set-up-git 6
  7. 7. Set up Git https://help.github.com/articles/set-up-git 7
  8. 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. 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. 10. $ git config --global credential.helper osxkeychain # Set git to use the osxkeychain credential helper 10
  11. 11. Get a Github account https://github.com/ 11
  12. 12. https://help.github.com/articles/generating-ssh-keys Generate SSH Keys 12
  13. 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. 14. $ pbcopy < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard 14
  15. 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
  16. 16. Create a Repo 16
  17. 17. 17
  18. 18. 18
  19. 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. 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. 21. Fork A Repo $ git clone git@github.com:oboegrace/git-101.git https://help.github.com/articles/fork-a-repo 21
  22. 22. Git Basics 22
  23. 23. Snapshots, Not Differences SVN Git 23
  24. 24. Nearly Every Operation is Local • 不只有⼀一份完整的資料 庫 • 閱讀版本歷史、提交 變更這些動作都可以 在本機進行 • 不需網路連線也可以工 作 24
  25. 25. Git Has Integrity • 使用 Checksum 來確保檔案的完整性 • 設計上只會增加資料,因此可以輕鬆復 原 • 在本機的檔案管理中,增添了Staging的 觀念 25
  26. 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. 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
  28. 28. Using Git 28
  29. 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. 30. Initializing a Repository in an Existing Directory • Repository就是⼀一份版本控制中心的版本 資料庫 • $ git init • 資訊會放在.git資料夾裡 30
  31. 31. Initial Commit • Add files • And commit it! $ touch README $ git add . $ git commit -m 'Initial commit' 31
  32. 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. 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. 34. ChangingYour Last Commit $ git commit --amend Unstaging a Staged File $ git reset HEAD aFile Unmodifying a Modified File $ git checkout -- aFile 34
  35. 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. 36. What a Branch Is 36
  37. 37. Single commit repository data. 37
  38. 38. Git object data for multiple commits. Branch pointing into the commit data’s history. 38
  39. 39. $ git branch testing git branch <new_branch_name> 建立本地 local branch git branch 列出目前有那些 branch 以及目前在那個 branch 39
  40. 40. $ git checkout testing This moves HEAD to point to the testing branch 此時如果commit... git checkout <branch_name> 切換 branch 40
  41. 41. 41
  42. 42. $ git checkout master 42
  43. 43. $ git commit -a -m 'made other changes' 43
  44. 44. http://learn.github.com/p/branching.html Practice Branching and Merging 44
  45. 45. 如何重新變回⼀一條? • Merge • 把岔開來的分支在往後合起來 • 通常的建議方式 • Rebase • 把岔開來的分支裝回去主線 • 還沒Push出去的東西才可以Rebase! 45
  46. 46. Basic Branching and Merging 46
  47. 47. 有緊急的Bug,開了一個 hotfix branch 處理好了。 現在要怎麼處理 master? 47
  48. 48. $ git checkout master $ git merge hotfix 48
  49. 49. 此時處理完iss53,那該如何Merge? 49
  50. 50. $ git checkout master $ git merge iss53 50
  51. 51. Basic Merge Conflicts • git status ⼀一下看是哪個檔案出問題 • 到出問題的檔案找問題,手動解決 • 再檢查 git status 後,用 git commit 手動 Merge (會自動生成 Merge 的 Commit Message) 51
  52. 52. Rebase 52
  53. 53. 除了Merge以外,我有沒有其他的辦法可以處理 掉 experiment branch? 53
  54. 54. $ git checkout experiment $ git rebase master 54
  55. 55. $ git checkout master $ git merge experiment 55
  56. 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

×