Git教程

Git教程【譯】


原文出處:http://www.vogella.com/articles/Git/article.html

翻譯說明:個人出於興趣愛好翻譯這篇Git教程(google 搜索git tutorial排名第一的文章)。學習git最初的原因是爲了瞭解GitHub,譯者水平有限,有不足之處歡迎指正。希望這邊譯文對你能有幫助。轉載請鏈接出處。


Git教程

本教程通過命令行來闡述分佈版本控制系統Git的使用。演示系統選取的是Linux(Ubuntu),但是在其他系統上也能功能,例如Windows系統。

內容索引

1. Git
1.1. Git是何方神聖?
1.2. 重要的術語
1.3. 索引(stage)
2. 安裝
3. 配置
3.1. 用戶信息
3.2. 高亮顯示
3.3. 忽略特定的文件
3.4. 使用.gitkeep來追蹤空的文件夾
4. 開始操作Git
4.1. 創建內容
4.2. 創建倉庫、添加文件和提交更改
4.3. diff命令和commit修改
4.4. Status, Diff和Commit Log
4.5. 更正提交的信息 - git amend
4.6. 刪除文件
5. 遠端倉庫(remote repositories)
5.1. 設置一個遠端的Git倉庫
5.2. 推送修改到其他的倉庫
5.3. 添加遠端倉庫
5.4. 顯示已有的遠端倉庫
5.5. 克隆倉庫
5.6. 拉取(Pull)修改
6. 還原更改
7. 標記
8. 分支和合並
8.1. 分支
8.2. 合併
8.3. 刪除分支
8.4. 推送(Push)一個分支到遠端倉庫
9. 解決合併衝突
10. 變基(Rebase)
10.1. 在同一分支中應用Rebase Commit
10.2. Rebasing 多個分支
10.3. Rebase最佳實踐
11. 創建和應用補丁
12. 定義同名命令
13. 放棄跟蹤文件
14. 其他有用的命令
15. 安裝Git服務
16. 在線的遠端倉庫
16.1. 克隆遠端倉庫
16.2. 添加遠端倉庫
16.3. 通過http和代理服務器進行遠端操作
17. Git服務提供商
17.1. GitHub
17.2. Bitbucket
18. Git的圖形接口
19. Kindle版本教程
20. 問題與討論
21. 鏈接和文章

1. Git

1.1. Git是何方神聖?

Git是用C語言開發的分佈版本控制系統。版本控制系統可以保留一個文件集合的歷史記錄,並能回滾文件集合到另一個狀態(歷史記錄狀態)。另一個狀態可以是不同的文件,也可以是不同的文件內容。舉個例子,你可以將文件集合轉換到兩天之前的狀態,或者你可以在生產代碼和實驗性質的代碼之間進行切換。文件集合往往被稱作是“源代碼”。在一個分佈版本控制系統中,每個人都有一份完整的源代碼(包括源代碼所有的歷史記錄信息),而且可以對這個本地的數據進行操作。分佈版本控制系統不需要一個集中式的代碼倉庫。

當你對本地的源代碼進行了修改,你可以標註他們跟下一個版本相關(將他們加到index中),然後提交到倉庫中來(commit)。Git保存了所有的版本信息,所以你可以轉換你的源代碼到任何的歷史版本。你可以對本地的倉庫進行代碼的提交,然後與其他的倉庫進行同步。你可以使用Git來進行倉庫的克隆(clone)操作,完整的複製一個已有的倉庫。倉庫的所有者可以通過push操作(推送變更到別處的倉庫)或者Pull操作(從別處的倉庫拉取變更)來同步變更。

Git支持分支功能(branch)。如果你想開發一個新的產品功能,你可以建立一個分支,對這個分支的進行修改,而不至於會影響到主支上的代碼。

Git提供了命令行工具;這個教程會使用命令行。你也可以找到圖形工具,譬如與Eclipse配套的EGit工具,但是這些都不會在這個教程中進行描述。

1.2. 重要的術語

表 1. Git 術語

術語定義

倉庫(Repository)

一個倉庫包括了所有的版本信息、所有的分支和標記信息。在Git中倉庫的每份拷貝都是完整的。倉庫讓你可以從中取得你的工作副本。
分支(Branches)一個分支意味着一個獨立的、擁有自己歷史信息的代碼線(code line)。你可以從已有的代碼中生成一個新的分支,這個分支與剩餘的分支完全獨立。默認的分支往往是叫master。用戶可以選擇一個分支,選擇一個分支叫做checkout.

標記(Tags)

一個標記指的是某個分支某個特定時間點的狀態。通過標記,可以很方便的切換到標記時的狀態,例如2009年1月25號在testing分支上的代碼狀態

提交(Commit)

提交代碼後,倉庫會創建一個新的版本。這個版本可以在後續被重新獲得。每次提交都包括作者和提交者,作者和提交者可以是不同的人
URLURl用來標識一個倉庫的位置

修訂(Revision)

用來表示代碼的一個版本狀態。Git通過用SHA1 hash算法表示的id來標識不同的版本。每一個 SHA1 id都是160位長,16進制標識的字符串.。最新的版本可以通過HEAD來獲取。之前的版本可以通過"HEAD~1"來獲取,以此類推。

1.3. 索引

Git 需要將代碼的變化顯示的與下一次提交進行關聯。舉個例子,如果你對一個文件繼續了修改,然後想將這些修改提交到下一次提交中,你必須將這個文件提交到索引中,通過git add file命令。這樣索引可以保存所有變化的快照。

新增的文件總是要顯示的添加到索引中來。對於那些之前已經提交過的文件,可以在commit命令中使用-a 選項達到提交到索引的目的。

2. 安裝

在Ubuntu上,你可以通過apt來安裝git命令行工具


sudo apt-get install git-core

對於其他的Linux版本,請查看相關的軟件包安裝工具使用方法

msysgit項目提供了Windows版本的Git,地址是http://code.google.com/p/msysgit/

3. 配置

你可以在.gitconfig文件中防止git的全局配置。文件位於用戶的home目錄。 上述已經提到每次提交都會保存作者和提交者的信息,這些信息都可以保存在全局配置中。

後續將會介紹配置用戶信息、高亮顯示和忽略特定的文件

3.1. 用戶信息

通過如下命令來配置用戶名和Email

複製代碼
# Configure the user which will be used by git# Of course you should use your namegit config --global user.name "Example Surname"# Same for the email addressgit config --global user.email "[email protected]"# Set default so that all changes are always pushed to the repositorygit config --global push.default "matching"
複製代碼


獲取Git配置信息,執行以下命令:


git config --list


3.2. 高亮顯示

以下命令會爲終端配置高亮


git config --global color.status autogit config --global color.branch auto


3.3. 忽略特定的文件

可以配置Git忽略特定的文件或者是文件夾。這些配置都放在.gitignore文件中。這個文件可以存在於不同的文件夾中,可以包含不同的文件匹配模式。爲了讓Git忽略bin文件夾,在主目錄下放置.gitignore文件,其中內容爲bin。

同時Git也提供了全局的配置,core.excludesfile

3.4. 使用.gitkeep來追蹤空的文件夾

Git會忽略空的文件夾。如果你想版本控制包括空文件夾,根據慣例會在空文件夾下放置.gitkeep文件。其實對文件名沒有特定的要求。一旦一個空文件夾下有文件後,這個文件夾就會在版本控制範圍內。

4. 開始操作Git

後續將通過一個典型的Git工作流來學習。在這個過程中,你會創建一些文件、創建一個本地的Git倉庫、提交你的文件到這個倉庫中。這之後,你會克隆一個倉庫、在倉庫之間通過pull和push操作來交換代碼的修改。註釋(以#開頭)解釋了命令的具體含義

讓我們打開命令行開始操作吧

4.1. 創建內容

下面創建一些文件,它們會被放到版本控制之中


複製代碼
#Switch to homecd ~/# Create a directorymkdir ~/repo01# Switch into itcd repo01# Create a new directorymkdir datafiles# Create a few filestouch test01touch test02touch test03touch datafiles/data.txt# Put a little text into the first filels >test01
複製代碼


4.2. 創建倉庫、添加文件和提交更改

每個Git倉庫都是放置在.git文件夾下.這個目錄包含了倉庫的所有歷史記錄,.git/config文件包含了倉庫的本地配置。

以下將會創建一個Git倉庫,添加文件倒倉庫的索引中,提交更改。


複製代碼
# Initialize the local Git repositorygit init# Add all (files and directories) to the Git repositorygit add .# Make a commit of your file to the local repositorygit commit -m "Initial commit"# Show the log filegit log
複製代碼


4.3. diff命令與commit更改

通過git diff命令,用戶可以查看更改。通過改變一個文件的內容,看看git diff命令輸出什麼,然後提交這個更改到倉庫中


複製代碼
# Make some changes to the fileecho"This is a change" > test01echo"and this is another change" > test02# Check the changes via the diff command git diff# Commit the changes, -a will commit changes for modified files# but will not add automatically new filesgit commit -a -m "These are new changes"
複製代碼


4.4. Status, Diff 和 Commit Log

下面會向你展示倉庫現有的狀態以及過往的提交歷史

複製代碼
# Make some changes in the fileecho"This is a new change" > test01echo"and this is another new change" > test02# See the current status of your repository # (which files are changed / new / deleted)git status# Show the differences between the uncommitted files # and the last commit in the current branchgit diff# Add the changes to the index and commitgit add . && git commit -m "More chaanges - typo in the commit message"# Show the history of commits in the current branchgit log# This starts a nice graphical view of the changesgitk --all
複製代碼


4.5. 更正提交的信息 - git amend

通過git amend命令,我們可以修改最後提交的的信息

上述的提交信息中存在錯誤,下面會修改這個錯誤

git commit --amend -m "More changes - now correct"


4.6. 刪除文件

如果你刪除了一個在版本控制之下的文件,那麼使用git add .不會在索引中刪除這個文件。需要通過帶-a選項的git commit命令和-A選項的git add命令來完成

複製代碼
# Create a file and put it under version controltouch nonsense.txtgit add . && git commit -m "a new file has been created"# Remove the filerm nonsense.txt# Try standard way of committing -> will not work git add . && git commit -m "a new file has been created"# Now commit with the -a flaggit commit -a -m "File nonsense.txt is now removed"# Alternatively you could add deleted files to the staging index viagit add -A . git commit -m "File nonsense.txt is now removed"
複製代碼


5. 遠端倉庫(remote repositories)

5.1. 設置一個遠端的Git倉庫

我們將創建一個遠端的Git倉庫。這個倉庫可以存儲在本地或者是網絡上。

遠端Git倉庫和標準的Git倉庫有如下差別:一個標準的Git倉庫包括了源代碼和歷史信息記錄。我們可以直接在這個基礎上修改代碼,因爲它已經包含了一個工作副本。但是遠端倉庫沒有包括工作副本,只包括了歷史信息。可以使用--bare選項來創建一個這樣的倉庫。

爲了方便起見,示例中的倉庫創建在本地文件系統上

複製代碼
# Switch to the first repositorycd ~/repo01# git clone --bare . ../remote-repository.git# Check the content, it is identical to the .git directory in repo01ls ~/remote-repository.git
複製代碼


5.2. 推送更改到其他的倉庫

做一些更改,然後將這些更改從你的第一個倉庫推送到一個遠端倉庫

複製代碼
# Make some changes in the first repositorycd ~/repo01# Make some changes in the fileecho"Hello, hello. Turn your radio on" > test01echo"Bye, bye. Turn your radio off" > test02# Commit the changes, -a will commit changes for modified files# but will not add automatically new filesgit commit -a -m "Some changes"# Push the changesgit push ../remote-repository.git
複製代碼


5.3. 添加遠端倉庫

除了通過完整的URL來訪問Git倉庫外,還可以通過git remote add命令爲倉庫添加一個短名稱。當你克隆了一個倉庫以後,origin表示所克隆的原始倉庫。即使我們從零開始,這個名稱也存在。

複製代碼
# Add ../remote-repository.git with the name origingit remote add origin ../remote-repository.git # Again some changesecho"I added a remote repo" > test02# Commitgit commit -a -m "This is a test for the new remote origin"# If you do not label a repository it will push to origingit push origin
複製代碼


5.4. 顯示已有的遠端倉庫

通過以下命令查看已經存在的遠端倉庫

# Show the existing defined remote repositoriesgit remote


5.5. 克隆倉庫

通過以下命令在新的目錄下創建一個新的倉庫

複製代碼
# Switch to homecd ~# Make new directorymkdir repo02# Switch to new directorycd ~/repo02# Clonegit clone ../remote-repository.git .
複製代碼


5.6. 拉取(Pull)更改

通過拉取,可以從其他的倉庫中獲取最新的更改。在第二個倉庫中,做一些更改,然後將更改推送到遠端的倉庫中。然後第一個倉庫拉取這些更改

複製代碼
# Switch to homecd ~# Switch to second directorycd ~/repo02# Make changesecho"A change" > test01# Commitgit commit -a -m "A change"# Push changes to remote repository# Origin is automatically maintained as we cloned from this repositorygit push origin# Switch to the first repository and pull in the changescd ~/repo01git pull ../remote-repository.git/# Check the changesless test01
複製代碼



6. 還原更改

如果在你的工作副本中,你創建了不想被提交的文件,你可以丟棄它。

複製代碼
# Create a new file with contenttouch test04echo"this is trash" > test04# Make a dry-run to see what would happen# -n is the same as --dry-run git clean -n# Now deletegit clean -f
複製代碼


你可以提取老版本的代碼,通過提交的ID。git log命令可以查看提交ID

複製代碼
# Switch to homecd ~/repo01# Get the loggit log# Copy one of the older commits and checkout the older revision via  譯者注:checkout 後加commit id就是把commit的內容複製到index和工作副本中 git checkout commit_name
複製代碼


如果你還未把更改加入到索引中,你也可以直接還原所有的更改

複製代碼
#Some nonsense changeecho"nonsense change" > test01# Not added to the staging index. Therefore we can # just checkout the old version#譯者注:checkout後如果沒有commit id號,就是從index中拷貝數據到工作副本,不涉及commit部分的改變
git checkout test01# Check the result
cat test01# Another nonsense changeecho"another nonsense change" > test01# We add the file to the staging indexgit add test01# Restore the filein the staging index#譯者注:複製HEAD所指commit的test01文件到index中
git reset HEAD test01# Get the old version from the staging index
#譯者注:複製index中test01到工作副本中git checkout test01
#譯者注,以上兩條命令可以合併爲git checkout HEAD test01
複製代碼


也可以通過revert命令進行還原操作

# Revert a commitgit revert commit_name


即使你刪除了一個未添加到索引和提交的文件,你也可以還原出這個文件

# Delete a filerm test01# Revert the deletiongit checkout test01


如果你已經添加一個文件到索引中,但是未提交。可以通過git reset file 命令將這個文件從索引中刪除

複製代碼
// Create a filetouch incorrect.txt// Accidently add it to the indexgit add .// Remove it from the indexgit reset incorrect.txt// Delete the filerm incorrect.txt
複製代碼


如果你刪除了文件夾且尚未提交,可以通過以下命令來恢復這個文件夾 。譯者注:即使已經提交,也可以還原

git checkout HEAD -- your_dir_to_restore

譯者注:checkout和reset這兩個命令的含義是不同的,可以參閱這篇文章http://marklodato.github.com/visual-git-guide/index-en.html


7. 標記

Git可以使用對歷史記錄中的任一版本進行標記。這樣在後續的版本中就能輕鬆的找到。一般來說,被用來標記某個發行的版本

可以通過git tag命令列出所有的標記,通過如下命令來創建一個標記和恢復到一個標記


git tag version1.6 -m 'version 1.6'      git checkout <tag_name>


8. 分支、合併

8.1. 分支

通過分支,可以創造獨立的代碼副本。默認的分支叫master。Git消耗很少的資源就能創建分支。Git鼓勵開發人員多使用分支

下面的命令列出了所有的本地分支,當前所在的分支前帶有*號

git branch 


如果你還想看到遠端倉庫的分支,可以使用下面的命令

git branch -a


可以通過下面的命令來創建一個新的分支

複製代碼
# Syntax: git branch <name> <hash># <hash> in the above is optional # if not specified the last commit will be used# If specified the corresponding commit will be usedgit branch testing# Switch to your new branchgit checkout testing# Some changesecho"Cool new feature in this branch" > test01git commit -a -m "new feature"# Switch to the master branchgit checkout master# Check that the content of test01 is the old onecat test01
複製代碼


8.2. 合併

通過Merge我們可以合併兩個不同分支的結果。Merge通過所謂的三路合併來完成。分別來自兩個分支的最新commit和兩個分支的最新公共commit

可以通過如下的命令進行合併

# Syntax: git merge <branch-name>git merge testing

一旦合併發生了衝突,Git會標誌出來,開發人員需要手工的去解決這些衝突。解決衝突以後,就可以將文件添加到索引中,然後提交更改

8.3. 刪除分支

刪除分支的命令如下:

#Delete branch testinggit branch -d testing# Check if branch has been deletedgit branch


8.4. 推送(push)一個分支到遠端倉庫

默認的,Git只會推送匹配的分支的遠端倉庫。這意味在使用git push命令默認推送你的分支之前,需要手工的推送一次這個分支。

複製代碼
# Push testing branch to remote repositorygit push origin testing# Switch to the testing branchgit checkout testing# Some changesecho"News for you" > test01git commit -a -m "new feature in branch"# Push all including branchgit push
複製代碼

通過這種方式,你可以確定哪些分支對於其他倉庫是可見的,而哪些只是本地的分支  

9. 解決合併衝突

如果兩個不同的開發人員對同一個文件進行了修改,那麼合併衝突就會發生。而Git沒有智能到自動解決合併兩個修改

在這一節中,我們會首先製造一個合併衝突,然後解決它,並應用到Git倉庫中

下面會產生一個合併衝突

複製代碼
# Switch to the first directorycd ~/repo01# Make changestouch mergeconflict.txtecho"Change in the first repository" > mergeconflict.txt# Stage and commitgit add . && git commit -a -m "Will create merge conflict 1"# Switch to the second directorycd ~/repo02# Make changestouch mergeconflict.txtecho"Change in the second repository" > mergeconflict.txt# Stage and commitgit add . && git commit -a -m "Will create merge conflict 2"# Push to the master repositorygit push# Now try to push from the first directory# Switch to the first directorycd ~/repo01# Try to push --> you will get an error messagegit push# Get the changesgit pull origin master
複製代碼


Git將衝突放在收到影響的文件中,文件內容如下:

<<<<<<< HEADChange in the first repository=======Change in the second repository>>>>>>> b29196692f5ebfd10d8a9ca1911c8b08127c85f8

上面部分是你的本地倉庫,下面部分是遠端倉庫。現在編輯這個文件,然後commit更改。另外的,你可以使用git mergetool命令


# Either edit the file manually or use git mergetool# You will be prompted to selectwhich merge tool you want to use# For example on Ubuntu you can use the tool "meld"# After  merging the changes manually, commit themgit commit -m "merged changes"



10. 變基(Rebase)

10.1. 在同一分支中應用Rebase Commit

通過rebase命令可以合併多個commit爲一個。這樣用戶push更改到遠端倉庫的時候就可以先修改commit歷史

接下來我們將創建多個commit,然後再將它們rebase成一個commit

複製代碼
# Create a new filetouch rebase.txt# Add it to gitgit add . && git commit -m "rebase.txt added to index"# Do some silly changes and commitecho"content" >> rebase.txtgit add . && git commit -m "added content"echo" more content" >> rebase.txtgit add . && git commit -m "added more content"echo" more content" >> rebase.txtgit add . && git commit -m "added more content"echo" more content" >> rebase.txtgit add . && git commit -m "added more content"echo" more content" >> rebase.txtgit add . && git commit -m "added more content"echo" more content" >> rebase.txtgit add . && git commit -m "added more content"# Check the git log messagegit log
複製代碼


我們合併最後的七個commit。你可以通過如下的命令交互的完成

git rebase -i HEAD~7

這個命令會打開編輯器讓你修改commit的信息或者 squash/ fixup最後一個信息

Squash會合並commit信息而fixup會忽略commit信息(待理解)

10.2. Rebasing多個分支

你也可以對兩個分支進行rebase操作。如下所述,merge命令合併兩個分支的更改。rebase命令爲一個分支的更改生成一個補丁,然後應用這個補丁到另一分支中

使用merge和rebase,最後的源代碼是一樣的,但是使用rebase產生的commit歷史更加的少,而且歷史記錄看上去更加的線性


複製代碼
# Create new branch git branch testing# Checkout the branchgit checkout testing# Make some changesecho"This will be rebased to master" > test01# Commit into testing branchgit commit -a -m "New feature in branch"# Rebase the mastergit rebase master
複製代碼


10.3.Rebase最佳實踐

在push更改到其他的Git倉庫之前,我們需要仔細檢查本地分支的commit歷史

在Git中,你可以使用本地的commit。開發人員可以利用這個功能方便的回滾本地的開發歷史。但是在push之前,需要觀察你的本地分支歷史,是否其中有些commit歷史對其他用戶來說是無關的

如果所有的commit歷史都跟同一個功能有關,很多情況下,你需要rebase這些commit歷史爲一個commit歷史。

交互性的rebase主要就是做重寫commit歷史的任務。這樣做是安全的,因爲commit還沒有被push到其它的倉庫。這意味着commit歷史只有在被push之前被修改

如果你修改然後push了一個已經在目標倉庫中存在的commit歷史,這看起來就像是你實現了一些別人已經實現的功能

11. 創建和應用補丁

一個補丁指的是一個包含對源代碼進行修改的文本文件。你可以將這個文件發送給某人,然後他就可以應用這個補丁到他的本地倉庫

下面會創建一個分支,對這個分支所一些修改,然後創建一個補丁,並應用這個補丁到master分支

複製代碼
# Create a new branchgit branch mybranch# Use this new branchgit checkout mybranch# Make some changestouch test05# Change some content in an existing fileecho"New content for test01" >test01# Commit this to the branchgit add .git commit -a -m "First commit in the branch"# Create a patch --> git format-patch mastergit format-patch origin/master# This created patch0001-First-commit-in-the-branch.patch# Switch to the mastergit checkout master# Apply the patchgit apply 0001-First-commit-in-the-branch.patch# Do your normal commit in the master git add .git commit -a -m "Applied patch"# Delete the patchrm0001-First-commit-in-the-branch.patch
複製代碼


12. 定義同名命令

Git允許你設定你自己的Git命令。你可以給你自己常用的命令起一個縮寫命令,或者合併幾條命令道一個命令上來。

下面的例子中,定義了git add-commit 命令,這個命令合併了git add . -Agit commit -m 命令。定義這個命令後,就可以使用git add-commit -m "message" 了.

git config --global alias.add-commit '!git add . -A && git commit'

但是非常不幸,截止寫這篇文章之前,定義同名命令在msysGit中還沒有支持。同名命令不能以!開始。

13. 放棄跟蹤文件

有時候,你不希望某些文件或者文件夾被包含在Git倉庫中。但是如果你把它們加到.gitignore文件中以後,Git會停止跟蹤這個文件。但是它不會將這個文件從倉庫中刪除。這導致了文件或者文件夾的最後一個版本還是存在於倉庫中。爲了取消跟蹤這些文件或者文件夾,你可以使用如下的命令


# Remove directory .metadata from git repogit rm -r --cached .metadata# Remove file test.txt from repogit rm --cached test.txt

這樣做不會將這些文件從commit歷史中去掉。如果你想將這些文件從commit歷史中去掉,可以參考git filter-branch命令

14. 其他有用的命令

下面列出了在日常工作中非常有用的Git命令

Table 2. 有用的Git命令

命令描述
git blame filename誰創建了或者是修改了這個文件
git checkout -b mybranch master~1以上上個commit信息爲起點,創建一條新的分支

15. 安裝Git服務

如上所述,我們的操作不需要Git服務。我可以只使用文件系統或者是Git倉庫的提供者,像Github或Bitbucket。但是,有時候,擁有一個自己的服務是比較方便的,在ubuntu下安裝一個服務相對來說是比較容易的

確定你已經安裝了ssh

apt-get installssh


如果你還沒有安裝Git服務,安裝它

sudo apt-get install git-core


添加一個名爲git的用戶

sudo adduser git


然後使用git用戶進行登陸,創建一個空的倉庫

# Login to server# to test use localhostssh git@IP_ADDRESS_OF_SERVER# Create repositorygit init --bare example.git



現在你就可以向遠端的倉庫提交變更了

複製代碼
mkdir gitexamplecd gitexamplegit inittouch READMEgit add READMEgit commit -m 'first commit'git remote add origin git@IP_ADDRESS_OF_SERVER:example.gitgit push origin master
複製代碼


16. 在線的遠端倉庫

16.1. 克隆遠端倉庫

Git支持遠端的操作。Git支持多種的傳輸類型,Git自帶的協議就叫做git。下面的的命令通過git協議從克隆一個倉庫

git clone [email protected]:vogella/gitbook.git

同樣的,你可以通過http協議來克隆倉庫

# The following will clone via HTTP git clone http://[email protected]/vogella/gitbook.git


16.2. 添加遠端倉庫

如果你克隆了一個遠端倉庫,那麼原先的倉庫就叫做origin

你可以push修改到origin中,通過 git push origin 命令. 當然,push到一個遠端的倉庫需要對倉庫的寫權限

你可以通過git remote add name gitrepo 命令添加多個倉庫。例如,你可以通過http協議再次添加之前clone過來的倉庫:

// Add the https protocol 
git remote add githttp https://[email protected]/vogella/gitbook.git



16.3. 通過http和代理服務器進行遠端操作

如果你的防火牆屏蔽了出http以外的所有協議,那麼使用http協議來獲取倉庫是非常好的方法。.

Git同樣支持通過代理服務器使用http協議。下面的Git命令會展示這一點。你可以爲所有的程序設置代理服務器或者只是爲Git服務提供。

下面的例子用到了環境變量

複製代碼
# Linuxexport http_proxy=http://proxy:8080# On Windows# Set http_proxy=http://proxy:8080 
git clone http://dev.eclipse.org/git/org.eclipse.jface/org.eclipse.jface.snippets.git# Push back to the origin using httpgit push origin
複製代碼


下面的例子只是用到了Git的配置

// Set proxy for git globally
 git config --global http.proxy http://proxy:8080// To check the proxy settings
git config --get http.proxy// Just in case you need to you can also revoke the proxy settings
git config --global --unset http.proxy



17. Git服務提供商

除了假設自己的服務,你也可以使用Git服務提供商提供的服務。最流行的Git服務提供網站是GitHub和Bitbucket。它們都提供了有限制的免費服務

17.1. GitHub

可以通過 https://github.com/ 訪問GitHub. GitHub上所有的公開倉庫都是免費的。如果你想在上面使用私有的倉庫,那麼就需要付費給GitHub

GitHub需要你創建ssh的公鑰私鑰。生成一份Ubuntu的公鑰私鑰可以訪問 ssh key creation in Ubuntu ,Windows環境可以訪問msysgit ssh key generation .

在GitHub上創建一個賬戶和一個倉庫以後。你會收到如何將你的項目上傳到GitHUb的指南,其中的命令大致如下:

複製代碼
Global setup: Set up git  git config --global user.name "Your Name"  git config --global user.email [email protected]      Next steps:  mkdir gitbook   cd gitbook  git init  touch README  git add README  git commit -m 'first commit'  git remote add origin [email protected]:vogella/gitbook.git  git push -u origin master      Existing Git Repo?  cd existing_git_repo  git remote add origin [email protected]:vogella/gitbook.git  git push -u origin master  
複製代碼



17.2. Bitbucket

可以通過 https://bitbucket.org/ 訪問Bitbucket. Bitbucket 提供了無限制了公共倉庫和只能有五個人訪問的私有倉庫。如果你需要超過五個人訪問私有倉庫,就需要付費給Bitbucket

18. Git的圖形接口

這個教程主要說明Git命令行的使用。完成了這個教程以後,你可能想要找到一個Git的圖形工具

Git提供了兩個圖形工具。 gitk能夠展示倉庫的歷史信息、git gui 讓你可以通過編輯器來完成Git操作

Eclipse EGit 項目提供了Git與Eclipse的集成,在最新的Eclipse版本中可以找到


19. Kindle版本教程

這個教程提供了Kindle版本

Kindle Edition

20. 問題與討論

在提出問題之前,請先查看 vogella FAQ. 如果你有任何的問題或者是從文章中找到錯誤,那麼可以使用www.vogella.com Google Group.  我自己寫了一個簡短的列表 how to create good questions 可能會對你有用.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章