在Github使用教程(一)--搭建Github環境中,介紹了若是搭建github的環境,並示例如何進行簡單的代碼提交。這裏咱們接着說說幾個基本Github命令的使用。 git
1.git clone
用於克隆代碼到本地。
git clone
url —— 克隆url對應的項目到本地。
git clone url folderName ——將url對應的項目克隆島folderName文件夾
2. git pull
Github支持協做代碼開發管理,會常常遇到須要更新別人的代碼或者在不一樣的電腦上更新本身的代碼。那麼使用git pull命令便可更新代碼。git pull 能夠接受不少中參數,詳見常見具體的用法爲: github
git pull—— 直接從遠程主分支更新代碼 , git pull 至關於git pull origin master app
git pull forkName branchName —— 從forkName對應的url更新branchName分支。 fetch
3. git remote
用於管理遠程倉庫。 this
git remote —— 顯示已經添加的遠程倉庫名稱列表,當從遠程地址上clone了一個項目時,會默認添加一個origin名字的倉庫,對應clone時的url。等同於git remote show url
git remote show name —— 顯示具體名字對應的倉庫的信息。具體以下: spa
- * remote origin
- Fetch URL: https://github.com/gavincook/test.git
- Push URL: https://github.com/gavincook/test.git
- HEAD branch: master
- Remote branch:
- master 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)
git remote add name url —— 添加遠程倉庫。如: .net
- git remote add antstudio git@github.com:AntStudio/test.git
git remote rm name——
刪除遠程倉庫在本地的映射。 如:
4. git fetch
用於更新代碼,和git pull 功能相似,可是有一些區別。git pull 更新完代碼後會自動合併到當前分支,而git fetch不會合並。常見用法以下:
git fetch origin master ——— 將分支代碼更新到origin/master分支上
git fetch forkName remoteBranchName:branchName ——— 將分支代碼更新到branchName分支上 rest
5. git merge
處理分支的合併。
git merge master —— 將主分支合併到當前分支。若是沒有任何衝突則使用此命令便可。 code
這裏說下有衝突的狀況,如今在兩個分支都對同一個文件進行修改,在同一個文件中,如master分支添加「master commit」, test分支添加「test commit」. 而後將兩個分支合併。在test分支合併主分支:git merge master.咱們會看到以下的狀況:
- Auto-merging test.txt
- CONFLICT (content): Merge conflict in test.txt
- Automatic merge failed; fix conflicts and then commit the result.
也就是說兩個分支有衝突,那麼咱們能夠按照如下步驟進行衝突的解決:
(1).首先把改動恢復到merge以前,由於目前的狀態是:
- Unmerged paths:
- (use "git add <file>..." to mark resolution)
-
- both modified: test.txt
咱們先添加改動到暫存區域,git add .,而後用git reset head -- .最後使用git checkout -- .取消改動。到這裏,test就已經恢復到merge以前的狀態
(2).生成test分支相對於master的補丁
git format-patch master
使用這個命令後咱們獲得「0001-.test-commit.patch」,在項目的根目錄下,咱們將這個文件剪切到其餘目錄,好比D:/
(3).切回出分支,git chekcout master。 而後進行補丁修正,
首先進行git am D:\0001-.test-commit.patch打補丁,若是沒有衝突則此補丁修正成功,若是有衝突就會獲得形以下面的結果:
- E:\workspace\github\test [master]> git am D:\0001-.test-commit.patch
- Applying: .test commit
- rror: patch failed: test.txt:1
- error: test.txt: patch does not apply
- Patch failed at 0001 .test commit
-
- e:/workspace/github/test/.git/rebase-apply/patch
- esolved this problem, run "git am --resolved".
- If you prefer to skip this patch, run "git am --skip" instead.
- To restore the original branch and stop patching, run "git am --abort".
接着使用git apply --reject D:\0001-.test-commit.patch生成rej文件,來輔助咱們解決衝突:
咱們能夠獲得以下結果:
- E:\workspace\github\test [master]> git apply --reject D:\0001-.test-commit.patch
-
- Checking patch test.txt...
- error: while searching for:
- Github test!
- Hello Gavin.
- error: patch failed: test.txt:1
- Applying patch test.txt with 1 reject...
- Rejected hunk #1.
- E:\workspace\github\test [master +1 ~0 -0 !]>
此時咱們會發如今衝突文件同級目錄下生成了一個rej文件,咱們打開這個rej文件,能夠看到:
- diff a/test.txt b/test.txt (rejected hunks)
- @@ -1,2 +1,3 @@
- Github test!
- -Hello Gavin.
- \ No newline at end of file
- +Hello Gavin.
- +Test commit!
- \ No newline at end of file
即說明在Hello Gavin的下一行添加了一個Test commit的文本,而咱們主分支呢,也在Hello Gavin的下一行添加了一行文本,"master test!". 咱們能夠直接將"Test commit!" 添加到test.txt便可,好比咱們這裏就添加到"master test!"的下一行,而後保存。(實際解決衝突的時候須要根據具體的狀況來處理)
(4).提交補丁的改動,如今咱們已經解決了衝突的代碼部分,接着咱們應該提交這個改動。首先使用 git rm -f .\test.txt.rej來刪除rej文件,只須要提交改動的代碼文件。
- git add .
-
- git am --resolved
這樣就完成了一個有衝突的補丁的修正,若是有多個補丁能夠重複此步驟進行處理。
6. git branch
用於本地分支的管理
git branch —— 查看當前倉庫的全部本地分支
git branch branchName —— 建立名字爲branchName的分支
git branch -D branchName—— 刪除名字爲branchName的分支
git checkout用於分支的切換,如:
git checkout test —— 若是test分支存在,則切換到test分支
git checkout -b test —— 用於test分支不存在的狀況,會先建立test分支再切換到test分支。至關於
git branch test , git checkout test兩條命令
這裏主要介紹了一些經常使用的命令,git的命令還有不少,每個命令的用法也有不少。