Git使用教程(二)-- Git常用命令

Github使用教程(一)--搭建Github環境中,介紹瞭如果搭建github的環境,並示例如何進行簡單的代碼提交。這裏我們接着說說幾個基本Github命令的使用。

1.git clone

          用於克隆代碼到本地。     
          git cloneurl ——克隆url對應的項目到本地。
          git clone url folderName ——將url對應的項目克隆島folderName文件夾

2. git pull 

           Github支持協作代碼開發管理,會經常遇到需要更新別人的代碼或者在不同的電腦上更新自己的代碼。那麼使用git pull命令即可更新代碼。git pull 可以接受很多中參數,詳見常見具體的用法爲:

           git pull—— 直接從遠程主分支更新代碼 , git pull 相當於git pull origin master

           git pull forkName branchName —— 從forkName對應的url更新branchName分支。

3. git remote

    用於管理遠程倉庫。

          git remote —— 顯示已經添加的遠程倉庫名稱列表,當從遠程地址上clone了一個項目時,會默認添加一個origin名字的倉庫,對應clone時的url。等同於git remote show

        git remote show name —— 顯示具體名字對應的倉庫的信息。具體如下:      

* 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 —— 添加遠程倉庫。如:

git remote add antstudio [email protected]:AntStudio/test.git

    git remote rm name——刪除遠程倉庫在本地的映射。 如:

git remote rm antstudio

4. git fetch

      用於更新代碼,和git pull 功能類似,但是有一些區別。git pull 更新完代碼後會自動合併到當前分支,而git fetch不會合並。常見用法如下:

      git fetch origin master ——— 將分支代碼更新到origin/master分支上
      git fetch forkName remoteBranchName:branchName ——— 將分支代碼更新到branchName分支上

5. git merge

處理分支的合併。

       git merge master —— 將主分支合併到當前分支。如果沒有任何衝突則使用此命令即可。 

這裏說下有衝突的情況,現在在兩個分支都對同一個文件進行修改,在同一個文件中,如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的分支
      

7.  git checkout

         git checkout用於分支的切換,如:
         git checkout test —— 如果test分支存在,則切換到test分支
        git checkout -b test —— 用於test分支不存在的情況,會先創建test分支再切換到test分支。相當於git branch test , git checkout test兩條命令



這裏主要介紹了一些常用的命令,git的命令還有很多,每一個命令的用法也有很多。

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