GitHub具體教程
Table of Contents
1 ?Git具體教程
1.1 ?Git簡(jiǎn)單介紹
1.1.1 ?Git是何方神圣?
Git是用C語言開發(fā)的分布版本號(hào)控制系統(tǒng)。版本號(hào)控制系統(tǒng)能夠保留一個(gè)文件集合的歷史記錄,并能回滾文件集合到還有一個(gè)狀態(tài)(歷史記錄狀態(tài))。還有一個(gè)狀態(tài)能夠是不同的文件,也能夠是不同的文件內(nèi)容。舉個(gè)樣例,你能夠?qū)⑽募限D(zhuǎn)換到兩天之前的狀態(tài),或者你能夠在生產(chǎn)代碼和實(shí)驗(yàn)性質(zhì)的代碼之間進(jìn)行切換。文件集合往往被稱作是“源碼”。在一個(gè)分布版本號(hào)控制系統(tǒng)中,每一個(gè)人都有一份完整的源碼(包含源碼全部的歷史記錄信息),并且能夠?qū)@個(gè)本地的數(shù)據(jù)進(jìn)行操作。分布版本號(hào)控制系統(tǒng)不須要一個(gè)集中式的代碼倉庫。
當(dāng)你對(duì)本地的源碼進(jìn)行了改動(dòng),你能夠標(biāo)注他們跟下一個(gè)版本號(hào)相關(guān)(將他們加到index中),然后提交到倉庫中來(commit)。Git保存了全部的版本號(hào)信息,所以你能夠轉(zhuǎn)換你的源碼到不論什么的歷史版本號(hào)。你能夠?qū)Ρ镜氐膫}庫進(jìn)行代碼的提交,然后與其它的倉庫進(jìn)行同步。你能夠使用Git來進(jìn)行倉庫的克隆(clone)操作,完整的復(fù)制一個(gè)已有的倉庫。倉庫的全部者能夠通過push操作(推送變更到別處的倉庫)或者Pull操作(從別處的倉庫拉取變更)來同步變更。
Git支持分支功能(branch)。假設(shè)你想開發(fā)一個(gè)新的產(chǎn)品功能,你能夠建立一個(gè)分支,對(duì)這個(gè)分支的進(jìn)行改動(dòng),而不至于會(huì)影響到主支上的代碼。
Git提供了命令行工具;這個(gè)教程會(huì)使用命令行。你也能夠找到圖形工具,譬如與Eclipse配套的EGit工具,可是這些都不會(huì)在這個(gè)教程中進(jìn)行描寫敘述。
1.1.2 ?重要的術(shù)語
Git 術(shù)語
| 術(shù)語 | 定義 |
|---|---|
| 倉庫 | 一個(gè)倉庫包含了全部的版本號(hào)信息、全部的分支和標(biāo)記信息. |
| Repository | 在Git中倉庫的每份拷貝都是完整的。倉庫讓你能夠從中 |
| ? | 取得你的工作副本。 |
| ? | 一個(gè)分支意味著一個(gè)獨(dú)立的、擁有自己歷史信息的代碼線 |
| 分支 | (code line)。你能夠從已有的代碼中生成一個(gè)新的分支 |
| Branches | ,這個(gè)分支與剩余的分支全然獨(dú)立。默認(rèn)的分支往往是叫 |
| ? | master。用戶能夠選擇一個(gè)分支,選擇一個(gè)分支叫做 |
| ? | checkout. |
| 標(biāo)記 | 一個(gè)標(biāo)記指的是某個(gè)分支某個(gè)特定時(shí)間點(diǎn)的狀態(tài)。通過標(biāo) |
| Tags | 記,能夠非常方便的切換到標(biāo)記時(shí)的狀態(tài),比如2009年1月25 |
| ? | 號(hào)在testing分支上的代碼狀態(tài) |
| 提交 | 提交代碼后,倉庫會(huì)創(chuàng)建一個(gè)新的版本號(hào)。這個(gè)版本號(hào)能夠在 |
| Commit | 興許被又一次獲得。每次提交都包含作者和提交者,作者和 |
| ? | 提交者能夠是不同的人 |
| URL | URl用來標(biāo)識(shí)一個(gè)倉庫的位置 |
| ? | 用來表示代碼的一個(gè)版本號(hào)狀態(tài)。Git通過用SHA1 hash算法 |
| 修訂 | 表示的id來標(biāo)識(shí)不同的版本號(hào)。每個(gè) SHA1 id都是160位長(zhǎng) |
| Revision | ,16進(jìn)制標(biāo)識(shí)的字符串.最新的版本號(hào)能夠通過HEAD來獲取. |
| ? | 之前的版本號(hào)能夠通過"HEAD~1"來獲取,以此類推。 |
1.2 ?Git安裝
在Ubuntu上,你能夠通過apt來安裝git命令行工具
sudo apt-get install git-core
對(duì)于其它的Linux版本號(hào),請(qǐng)查看相關(guān)的軟件包安裝工具用法。msysgit項(xiàng)目提供了Windows版本號(hào)的Git,地址是 http://code.google.com/p/msysgit/
1.3 ?Git配置
你能夠在.gitconfig文件里防止git的全局配置。文件位于用戶的home文件夾。上述已經(jīng)提到每次提交都會(huì)保存作者和提交者的信息,這些信息都能夠保存在全局配置中。興許將會(huì)介紹配置用戶信息、高亮顯示和忽略特定的文件。
1.3.1 ?用戶信息
通過例如以下命令來配置username和Email
git config --global user.name "Example Surname"
git config --global user.email "your.email@gmail.com"
# Set default so that all changes are always pushed to the repository
git config --global push.default "matching"
獲取Git配置信息,運(yùn)行下面命令:
git config --list
1.3.2 ?高亮顯示
下面命令會(huì)為終端配置高亮
git config --global color.status auto
git config --global color.branch auto
1.4 ?開始操作Git
興許將通過一個(gè)典型的Git工作流來學(xué)習(xí)。在這個(gè)過程中,你會(huì)創(chuàng)建一些文件、創(chuàng)建一個(gè)本地的Git倉庫、提交你的文件到這個(gè)倉庫中。這之后,你會(huì)克隆一個(gè)倉庫、在倉庫之間通過pull和push操作來交換代碼的改動(dòng)。凝視(以#開頭)解釋了命令的詳細(xì)含義,讓我們打開命令行開始操作吧。
1.4.1 ?創(chuàng)建內(nèi)容
以下創(chuàng)建一些文件,它們會(huì)被放到版本號(hào)控制之中
#
Switch to home
cd
~/
#
Create a directory
mkdir ~/repo01
#
Switch into it
cd
repo01
#
Create a new directory
mkdir datafiles
#
Create a few files
touch test01
touch test02
touch test03
touch datafiles/data.txt
#
Put a little text into the first file
ls >test01
1.4.2 ?創(chuàng)建倉庫、加入文件和提交更改
每一個(gè)Git倉庫都是放置在.git文件夾下.這個(gè)文件夾包括了倉庫的全部歷史記錄,.git/config文件包括了倉庫的本地配置。
下面將會(huì)創(chuàng)建一個(gè)Git倉庫,加入文件倒倉庫的索引中,提交更改。
#
Initialize the local Git repository
git init
#
Add all (files and directories) to the Git repository
git add .
#
Make a commit of your file to the local repository
git commit -m
"Initial commit"
#
Show the log file
git log
1.4.3 ?diff命令與commit更改
通過git diff命令,用戶能夠查看更改。通過改變一個(gè)文件的內(nèi)容,看看gitdiff命令輸出什么,然后提交這個(gè)更改到倉庫中
#
Make some changes to the file
echo
"This is a change"
> test01
echo
"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 files
git commit -a -m
"These are new changes"
1.4.4 ?Status, Diff 和 Commit Log
以下會(huì)向你展示倉庫現(xiàn)有的狀態(tài)以及過往的提交歷史
#
Make some changes in the file
echo
"This is a new change"
> test01
echo
"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 branch
git diff
#
Add the changes to the index and commit
git add . && git commit -m
"More chaanges - typo in the commit message"
#
Show the history of commits in the current branch
git log
#
This starts a nice graphical view of the changes
gitk --all
1.4.5 ?更正提交的信息 - git amend
通過git amend命令,我們能夠改動(dòng)最后提交的的信息。上述的提交信息中存在錯(cuò)誤,以下會(huì)改動(dòng)這個(gè)錯(cuò)誤。
git commit --amend -m "More changes - now correct"
1.4.6 ?刪除文件
假設(shè)你刪除了一個(gè)在版本號(hào)控制之下的文件,那么使用git add .不會(huì)在索引中刪除這個(gè)文件。須要通過帶-a選項(xiàng)的git commit命令和-A選項(xiàng)的git add命令來完畢
#
Create a file and put it under version control
touch nonsense.txt
git add . && git commit -m
"a new file has been created"
#
Remove the file
rm 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 flag
git commit -a -m
"File nonsense.txt is now removed"
#
Alternatively you could add deleted files to the staging index via
git add -A .
git commit -m
"File nonsense.txt is now removed"
1.5 ?遠(yuǎn)端倉庫(remote repositories)
1.5.1 ?設(shè)置一個(gè)遠(yuǎn)端的Git倉庫
我們將創(chuàng)建一個(gè)遠(yuǎn)端的Git倉庫。這個(gè)倉庫能夠存儲(chǔ)在本地或者是網(wǎng)絡(luò)上。
遠(yuǎn)端Git倉庫和標(biāo)準(zhǔn)的Git倉庫有例如以下區(qū)別:一個(gè)標(biāo)準(zhǔn)的Git倉庫包含了源碼和歷史信息記錄。我們能夠直接在這個(gè)基礎(chǔ)上改動(dòng)代碼,由于它已經(jīng)包含了一個(gè)工作副本??墒沁h(yuǎn)端倉庫沒有包含工作副本,僅僅包含了歷史信息。能夠使用–bare選項(xiàng)來創(chuàng)建一個(gè)這種倉庫。
為了方便起見,演示樣例中的倉庫創(chuàng)建在本地文件系統(tǒng)上
#
Switch to the first repository
cd
~/repo01
#
git clone --bare . ../remote-repository.git
#
Check the content, it is identical to the .git directory in repo01
ls ~/remote-repository.git
1.5.2 ?推送更改到其它的倉庫
做一些更改,然后將這些更改從你的第一個(gè)倉庫推送到一個(gè)遠(yuǎn)端倉庫?
cd ~/repo01
echo "Hello, hello. Turn your radio on" > test01echo "Bye, bye. Turn your radio off" > test02
git commit -a -m "Some changes"
git push ../remote-repository.git
1.5.3 ?加入遠(yuǎn)端倉庫
除了通過完整的URL來訪問Git倉庫外,還能夠通過git remote add命令為倉庫加入一個(gè)短名稱。當(dāng)你克隆了一個(gè)倉庫以后,origin表示所克隆的原始倉庫。即使我們從零開始,這個(gè)名稱也存在。
#
Add ../remote-repository.git with the name origin
git remote add origin ../remote-repository.git
#
Again some changes
echo
"I added a remote repo"
> test02
#
Commit
git commit -a -m
"This is a test for the new remote origin"
#
If you do not label a repository it will push to origin
git push origin
1.5.4 ?顯示已有的遠(yuǎn)端倉庫
通過下面命令查看已經(jīng)存在的遠(yuǎn)端倉庫
# Show the existing defined remote repositories
git remote
1.5.5 ?克隆倉庫
通過下面命令在新的文件夾下創(chuàng)建一個(gè)新的倉庫
#
Switch to home
cd
~
#
Make new directory
mkdir repo02
#
Switch to new directory
cd
~/repo02
#
Clone
git clone ../remote-repository.git .
1.5.6 ?拉?。≒ull)更改
通過拉取,能夠從其它的倉庫中獲取最新的更改。在第二個(gè)倉庫中,做一些更改,然后將更改推送到遠(yuǎn)端的倉庫中。然后第一個(gè)倉庫拉取這些更改
#
Switch to home
cd
~
#
Switch to second directory
cd
~/repo02
#
Make changes
echo
"A change"
> test01
#
Commit
git commit -a -m
"A change"
#
Push changes to remote repository
#
Origin is automatically maintained as we cloned from this repository
git push origin
#
Switch to the first repository and pull in the changes
cd
~/repo01
git pull ../remote-repository.git/
#
Check the changes
less test01
1.5.7 ?還原更改
假設(shè)在你的工作副本中,你創(chuàng)建了不想被提交的文件,你能夠丟棄它。
# Create a new file with content
touch test04
echo "this is trash" > test04
# Make a dry-run to see what would happen
# -n is the same as --dry-run
git clean -n
# Now delete
git clean -f
你能夠提取老版本號(hào)的代碼,通過提交的ID。git log命令能夠查看提交ID
# Switch to home
cd ~/repo01
# Get the log
git log
# Copy one of the older commits and checkout the older revision via 譯者注:checkout 后加commit id就是把commit的內(nèi)容拷貝到index和工作副本中
git checkout commit_name
假設(shè)你還未把更改增加到索引中,你也能夠直接還原全部的更改
#
Some nonsense change
echo
"nonsense change"
> test01
#
Not added to the staging index. Therefore we can
#
just checkout the old version
#
譯者注:checkout后假設(shè)沒有commit id號(hào),就是從index中拷貝數(shù)據(jù)到工作副本,不涉及commit部分的改變
git checkout test01
#
Check the result
cat test01
#
Another nonsense change
echo
"another nonsense change"
> test01
#
We add the file to the staging index
git add test01
#
Restore the file in the staging index
#
譯者注:復(fù)制HEAD所指commit的test01文件到index中
git reset HEAD test01
#
Get the old version from the staging index
#
譯者注:復(fù)制index中test01到工作副本中
git checkout test01
#
譯者注,以上兩條命令能夠合并為git checkout HEAD test01
也能夠通過revert命令進(jìn)行還原操作
#
Revert a commit
git revert commit_name
即使你刪除了一個(gè)未加入到索引和提交的文件,你也能夠還原出這個(gè)文件
#
Delete a file
rm test01
#
Revert the deletion
git checkout test01
假設(shè)你已經(jīng)加入一個(gè)文件到索引中,可是未提交。能夠通過git resetfile 命令將這個(gè)文件從索引中刪除
// Create a file
touch incorrect.txt
// Accidently add it to the index
git add .
// Remove it from the index
git reset incorrect.txt
// Delete the file
rm incorrect.txt
假設(shè)你刪除了目錄且尚未提交,能夠通過下面命令來恢復(fù)這個(gè)目錄 。譯者注:即使已經(jīng)提交,也能夠還原
git checkout HEAD -- your_dir_to_restore
譯者注:checkout和reset這兩個(gè)命令的含義是不同的,能夠參閱這篇文章 http://marklodato.github.com/visual-git-guide/index-en.html
1.6 ?分支、合并
1.6.1 ?分支
通過分支,能夠創(chuàng)造獨(dú)立的代碼副本。默認(rèn)的分支叫master。Git消耗非常少的資源就能創(chuàng)建分支。Git鼓舞開發(fā)者多使用分支
以下的命令列出了全部的本地分支,當(dāng)前所在的分支前帶有*號(hào)
git branch
假設(shè)你還想看到遠(yuǎn)端倉庫的分支,能夠使用以下的命令
git branch -a
能夠通過以下的命令來創(chuàng)建一個(gè)新的分支
# 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 used
git branch testing
# Switch to your new branch
git checkout testing
# Some changes
echo "Cool new feature in this branch" > test01
git commit -a -m "new feature"
# Switch to the master branch
git checkout master
# Check that the content of test01 is the old one
cat test01
1.6.2 ?合并
通過Merge我們能夠合并兩個(gè)不同分支的結(jié)果。Merge通過所謂的三路合并來完畢。分別來自兩個(gè)分支的最新commit和兩個(gè)分支的最新公共commit.能夠通過例如以下的命令進(jìn)行合并
# Syntax: git merge <branch-name>
git merge testing
一旦合并發(fā)生了沖突,Git會(huì)標(biāo)志出來,開發(fā)者須要手工的去解決這些沖突。解決沖突以后,就能夠?qū)⑽募尤氲剿饕?,然后提交更?
1.6.3 ?刪除分支
刪除分支的命令例如以下:
#Delete branch testing
git branch -d testing
# Check if branch has been deleted
git branch
1.6.4 ?推送(push)一個(gè)分支到遠(yuǎn)端倉庫
默認(rèn)的,Git僅僅會(huì)推送匹配的分支的遠(yuǎn)端倉庫。這意味在使用git push命令默認(rèn)推送你的分支之前,須要手工的推送一次這個(gè)分支。
# Push testing branch to remote repository
git push origin testing
# Switch to the testing branch
git checkout testing
# Some changes
echo "News for you" > test01
git commit -a -m "new feature in branch"
# Push all including branch
git push
通過這樣的方式,你能夠確定哪些分支對(duì)于其它倉庫是可見的,而哪些僅僅是本地的分支
1.7 ?解決合并沖突
假設(shè)兩個(gè)不同的開發(fā)者對(duì)同一個(gè)文件進(jìn)行了改動(dòng),那么合并沖突就會(huì)發(fā)生。而Git沒有智能到自己主動(dòng)解決合并兩個(gè)改動(dòng)。
在這一節(jié)中,我們會(huì)首先制造一個(gè)合并沖突,然后解決它,并應(yīng)用到Git倉庫中。
以下會(huì)產(chǎn)生一個(gè)合并沖突
# Switch to the first directory
cd ~/repo01
# Make changes
touch mergeconflict.txt
echo "Change in the first repository" > mergeconflict.txt
# Stage and commit
git add . && git commit -a -m "Will create merge conflict 1"
# Switch to the second directory
cd ~/repo02
# Make changes
touch mergeconflict.txt
echo "Change in the second repository" > mergeconflict.txt
# Stage and commit
git add . && git commit -a -m "Will create merge conflict 2"
# Push to the master repository
git push
# Now try to push from the first directory
# Switch to the first directory
cd ~/repo01
# Try to push --> you will get an error message
git push
# Get the changes
git pull origin master
Git將沖突放在收到影響的文件里,文件內(nèi)容例如以下:
<<<<<<< HEAD
Change in the first repository
=======
Change in the second repository
>>>>>>> b29196692f5ebfd10d8a9ca1911c8b08127c85f8
上面部分是你的本地倉庫,以下部分是遠(yuǎn)端倉庫。如今編輯這個(gè)文件,然后commit更改。另外的,你能夠使用git mergetool命令
# Either edit the file manually or use
git mergetool
# You will be prompted to select which merge tool you want to use
# For example on Ubuntu you can use the tool "meld"
# After merging the changes manually, commit them
git commit -m "merged changes"
1.8 ?變基(Rebase)
1.8.1 ?在同一分支中應(yīng)用Rebase Commit
通過rebase命令能夠合并多個(gè)commit為一個(gè)。這樣用戶push更改到遠(yuǎn)端倉庫的時(shí)候就能夠先改動(dòng)commit歷史
接下來我們將創(chuàng)建多個(gè)commit,然后再將它們r(jià)ebase成一個(gè)commit
# Create a new file
touch rebase.txt
# Add it to git
git add . && git commit -m "rebase.txt added to index"
# Do some silly changes and commit
echo "content" >> rebase.txt
git add . && git commit -m "added content"
echo " more content" >> rebase.txt
git add . && git commit -m "added more content"
echo " more content" >> rebase.txt
git add . && git commit -m "added more content"
echo " more content" >> rebase.txt
git add . && git commit -m "added more content"
echo " more content" >> rebase.txt
git add . && git commit -m "added more content"
echo " more content" >> rebase.txt
git add . && git commit -m "added more content"
# Check the git log message
git log
我們合并最后的七個(gè)commit。你能夠通過例如以下的命令交互的完畢
git rebase -i HEAD~7
這個(gè)命令會(huì)打開編輯器讓你改動(dòng)commit的信息或者 squash/ fixup最后一個(gè)信息.Squash會(huì)合并commit信息而fixup會(huì)忽略commit信息(待理解)
1.8.2 ?Rebasing多個(gè)分支
你也能夠?qū)蓚€(gè)分支進(jìn)行rebase操作。例如以下所述,merge命令合并兩個(gè)分支的更改。rebase命令為一個(gè)分支的更改生成一個(gè)補(bǔ)丁,然后應(yīng)用這個(gè)補(bǔ)丁到還有一分支中
使用merge和rebase,最后的源碼是一樣的,可是使用rebase產(chǎn)生的commit歷史更加的少,并且歷史記錄看上去更加的線性
# Create new branch
git branch testing
# Checkout the branch
git checkout testing
# Make some changes
echo "This will be rebased to master" > test01
# Commit into testing branch
git commit -a -m "New feature in branch"
# Rebase the master
git rebase master
1.8.3 ?Rebase最佳實(shí)踐
在push更改到其它的Git倉庫之前,我們須要細(xì)致檢查本地分支的commit歷史
在Git中,你能夠使用本地的commit。開發(fā)者能夠利用這個(gè)功能方便的回滾本地的開發(fā)歷史??墒窃趐ush之前,須要觀察你的本地分支歷史,是否當(dāng)中有些commit歷史對(duì)其它用戶來說是無關(guān)的
假設(shè)全部的commit歷史都跟同一個(gè)功能有關(guān),非常多情況下,你須要rebase這些commit歷史為一個(gè)commit歷史。
交互性的rebase主要就是做重寫commit歷史的任務(wù)。這樣做是安全的,由于commit還沒有被push到其他的倉庫。這意味著commit歷史僅僅有在被push之前被改動(dòng)。
假設(shè)你改動(dòng)然后push了一個(gè)已經(jīng)在目標(biāo)倉庫中存在的commit歷史,這看起來就像是你實(shí)現(xiàn)了一些別人已經(jīng)實(shí)現(xiàn)的功能
1.8.4 ?創(chuàng)建和應(yīng)用補(bǔ)丁
一個(gè)補(bǔ)丁指的是一個(gè)包括對(duì)源碼進(jìn)行改動(dòng)的文本文件。你能夠?qū)⑦@個(gè)文件發(fā)送給某人,然后他就能夠應(yīng)用這個(gè)補(bǔ)丁到他的本地倉庫。
以下會(huì)創(chuàng)建一個(gè)分支,對(duì)這個(gè)分支所一些改動(dòng),然后創(chuàng)建一個(gè)補(bǔ)丁,并應(yīng)用這個(gè)補(bǔ)丁到master分支
# Create a new branch
git branch mybranch
# Use this new branch
git checkout mybranch
# Make some changes
touch test05
# Change some content in an existing file
echo "New content for test01" >test01
# Commit this to the branch
git add .
git commit -a -m "First commit in the branch"
# Create a patch --> git format-patch master
git format-patch origin/master
# This created patch 0001-First-commit-in-the-branch.patch
# Switch to the master
git checkout master
# Apply the patch
git 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 patch
rm 0001-First-commit-in-the-branch.patch
1.9 ?定義同名命令
Git同意你設(shè)定你自己的Git命令。你能夠給你自己經(jīng)常使用的命令起一個(gè)縮寫命令,或者合并幾條命令道一個(gè)命令上來。
以下的樣例中,定義了git add-commit 命令,這個(gè)命令合并了git add . -A 和git commit -m 命令。定義這個(gè)命令后,就能夠使用git add-commit -m"message" 了.
git config --global alias.add-commit '!git add . -A && git commit'
可是很不幸,截止寫這篇文章之前,定義同名命令在msysGit中還沒有支持。同名命令不能以!開始。
1.10 ?放棄跟蹤文件
有時(shí)候,你不希望某些文件或者目錄被包括在Git倉庫中??墒羌僭O(shè)你把它們加到.gitignore文件里以后,Git會(huì)停止跟蹤這個(gè)文件??墒撬粫?huì)將這個(gè)文件從倉庫中刪除。這導(dǎo)致了文件或者目錄的最后一個(gè)版本號(hào)還是存在于倉庫中。為了取消跟蹤這些文件或者目錄,你能夠使用例如以下的命令
# Remove directory .metadata from git repo
git rm -r --cached .metadata
# Remove file test.txt from repo
git rm --cached test.txt
這樣做不會(huì)將這些文件從commit歷史中去掉。假設(shè)你想將這些文件從commit歷史中去掉,能夠參考git filter-branch命令
1.11 ?其它實(shí)用的命令
以下列出了在日常工作中很實(shí)用的Git命令
實(shí)用的Git命令
| 命令 | 描寫敘述 |
|---|---|
| git blame filename | 誰創(chuàng)建了或者是改動(dòng)了這個(gè)文件 |
| git checkout -b mybranch | 以上上個(gè)commit信息為起點(diǎn),創(chuàng)建一條 |
| master~1 | 新的分支 |
1.12 ?安裝Git服務(wù)
如上所述,我們的操作不須要Git服務(wù)。我能夠僅僅使用文件系統(tǒng)或者是Git倉庫的提供者,像Github或Bitbucket??墒牵袝r(shí)候,擁有一個(gè)自己的服務(wù)是比較方便的,在ubuntu下安裝一個(gè)服務(wù)相對(duì)來說是比較easy的
確定你已經(jīng)安裝了ssh
apt-get install ssh
假設(shè)你還沒有安裝Git服務(wù),安裝它
sudo apt-get install git-core
加入一個(gè)名為git的用戶
sudo adduser git
然后使用git用戶進(jìn)行登陸,創(chuàng)建一個(gè)空的倉庫
# Login to server
# to test use localhost
ssh git@IP_ADDRESS_OF_SERVER
# Create repository
git init --bare example.git
如今你就能夠向遠(yuǎn)端的倉庫提交變更了
mkdir gitexample
cd gitexample
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@IP_ADDRESS_OF_SERVER:example.git
git push origin master
1.13 ?在線的遠(yuǎn)端倉庫
1.13.1 ?克隆遠(yuǎn)端倉庫
Git支持遠(yuǎn)端的操作。Git支持多種的傳輸類型,Git自帶的協(xié)議就叫做git。以下的的命令通過git協(xié)議從克隆一個(gè)倉庫
git clone git@github.com:vogella/gitbook.git
相同的,你能夠通過http協(xié)議來克隆倉庫
# The following will clone via HTTP
git clone http://vogella@github.com/vogella/gitbook.git
1.13.2 ?加入遠(yuǎn)端倉庫
假設(shè)你克隆了一個(gè)遠(yuǎn)端倉庫,那么原先的倉庫就叫做origin
你能夠push改動(dòng)到origin中,通過 git push origin 命令. 當(dāng)然,push到一個(gè)遠(yuǎn)端的倉庫須要對(duì)倉庫的寫權(quán)限
你能夠通過git remote add name gitrepo 命令加入多個(gè)倉庫。比如,你能夠通過http協(xié)議再次加入之前clone過來的倉庫:
// Add the https protocol
git remote add githttp https://vogella@github.com/vogella/gitbook.git
1.13.3 ?通過http和代理server進(jìn)行遠(yuǎn)端操作
假設(shè)你的防火墻屏蔽了出http以外的全部協(xié)議,那么使用http協(xié)議來獲取倉庫是很好的方法。.
Git相同支持通過代理server使用http協(xié)議。以下的Git命令會(huì)展示這一點(diǎn)。你能夠?yàn)槿康某绦蛟O(shè)置代理server或者僅僅是為Git服務(wù)提供。
以下的樣例用到了環(huán)境變量
# Linux
export 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 http
git 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
1.14 ?Git服務(wù)提供商
除了如果自己的服務(wù),你也能夠使用Git服務(wù)提供商提供的服務(wù)。最流行的Git服務(wù)提供站點(diǎn)是GitHub和Bitbucket。它們都提供了有限制的免費(fèi)服務(wù)
1.14.1 ?GitHub
能夠通過? https://github.com/ ?訪問GitHub. GitHub上全部的公開倉庫都是免費(fèi)的。假設(shè)你想在上面使用私有的倉庫,那么就須要付費(fèi)給GitHub
GitHub須要你創(chuàng)建ssh的公鑰私鑰。生成一份Ubuntu的公鑰私鑰能夠訪問 sshkey creation in Ubuntu ,Windows環(huán)境能夠訪問msysgit ssh key generation.
在GitHub上創(chuàng)建一個(gè)賬戶和一個(gè)倉庫以后。你會(huì)收到怎樣將你的項(xiàng)目上傳到GitHUb的指南,當(dāng)中的命令大致例如以下:
Global setup:
Set up git
git config --global user.name "Your Name"
git config --global user.email your.email@gmail.com
Next steps:
mkdir gitbook
cd gitbook
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:vogella/gitbook.git
git push -u origin master
Existing Git Repo?
cd existing_git_repo
git remote add origin git@github.com:vogella/gitbook.git
git push -u origin master
1.14.2 ?Bitbucket
能夠通過? https://bitbucket.org/ ?訪問Bitbucket. Bitbucket 提供了無限制了公共倉庫和僅僅能有五個(gè)人訪問的私有倉庫。假設(shè)你須要超過五個(gè)人訪問私有倉庫,就須要付費(fèi)給Bitbucket
1.15 ?Git的圖形接口
這個(gè)教程主要說明Git命令行的使用。完畢了這個(gè)教程以后,你可能想要找到一個(gè)Git的圖形工具
Git提供了兩個(gè)圖形工具。 gitk可以展示倉庫的歷史信息、git gui 讓你可以通過編輯器來完畢Git操作
Eclipse EGit 項(xiàng)目提供了Git與Eclipse的集成,在最新的Eclipse版本號(hào)中能夠找到
1.17 ?問題與討論
在提出問題之前,請(qǐng)先查看 vogella FAQ. 假設(shè)你有不論什么的問題或者是從文章中找到錯(cuò)誤,那么能夠使用www.vogella.com Google Group. 我自己寫了一個(gè)簡(jiǎn)短的列表 how to create good questions 可能會(huì)對(duì)你實(shí)用.
1.18 ?鏈接和文章
Git homepage
EGit - Teamprovider for Eclipse
Video with Linus Torwalds on Git
Progit book - Free Git book
Video casts about Git
http://code.google.com/p/msysgit/ ?Git on Windows
http://github.com/guides/git-cheat-sheet ?Git Cheat Sheets
Date: 2012-08-26 日
HTML generated by org-mode 6.33x in emacs 23
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

