Git版本控制與github使用

Git是一個開源的分佈式版本控制系統,用於敏捷高效地管理項目。相較svn,git最大的特點就是分佈式,每個人都擁有一個克隆的版本庫,所以提交代碼、查看日誌、創建分支、合併分支、回退等等操作都直接在本地完成而不需要網絡。當然,本地版本庫需要和遠程共享版本庫同步,這就需要網絡了。

github,作爲一家git服務提供商,可以託管你的git版本庫,而且對於開源項目是可以免費託管,這對於開源項目是一大利好。

github使用

註冊github賬號

首先你要有一個github賬號,註冊地址 https://github.com/join,記住你的用戶名和郵箱,這裏例如mycwq和 [email protected] (郵箱必須是有效的,需要驗證)

創建git版本庫

登錄github賬號,點擊右上角的+號創建版本庫(Create a new repository)。版本庫也叫數據倉庫

可以保持以上選項不修改,點綠色按鈕完成創建。成功後取到git庫地址 https://github.com/mycwq/test.git

克隆版本庫到本地

就是獲取git共享庫的所有內容,要確保系統安裝了git命令工具,沒有的話參考這篇文章
$ git clone https://github.com/mycwq/test.git

提交到共享版本庫
git下這個操作有2個步驟,第一步提交代碼到本地版本庫,第二步是同步本地版本庫到遠程的共享版本庫

如果是第一次使用,需要設置賬號和郵箱
$ git config --global user.name "mycwq"
$ git config --global user.email "[email protected]"

下面以例子說明如何提交內容到版本庫:
$ echo "# test" > README.md
$ git add README.md
$ git commit -m "first commit"
$ git push origin master

然後,按提示輸入賬號和密碼。成功後,可以在github看到剛剛提交的內容。



git版本庫與命令關係圖

在阮一峯博客上找到這個圖片,很有參考價值。文章在這裏,不妨去看看吧

以上,fetch、clone、push、pull都是遠程操作,其他都是本地完成的。


git常用命令

命令
說明
git diff
顯示版本庫所有文件的改動內容,但不包括版本外的文件
git status
樹狀顯示版本庫的大體情況,如哪些文件有改動,哪些還沒加到版本庫
git log
查看提交日誌
git clone <庫地址>
克隆遠程版本庫到本地
git fetch
同步共享版本庫的更新內容到本地版本庫
git add索引記錄要提交的文件,在git commit時提交
git commit提交改動到本地版本庫
git push
同步本地版本庫到共享版本庫
git remote
列出所有遠程主機
git branch <分支名>
新建分支
git checkout <分支名>
切換到分支(設置某個分支爲工作目錄)
git checkout -b <分支名>
檢出分支,等效於 git branch <分支名> && git checkout <分支名>
git merge <分支名>
合併某個分支到工作目錄
git pull
同步共享版本庫到本地版本庫,並且合併到當前工作目錄。等同於 git fetch 加上 git merge <分支名> 
git blame <filename>
獲取文件每一行的詳細修改信息,包括作者、日期和日誌編號

git分支管理

分支的常用命令

建立分支
$ git branch branch1
列出所有分支
$ git branch
  branch1
* master
以上,*表示當前在使用的分支,即工作目錄所在的分支。
如果是遠程倉庫github的分支情況:
$ git branch -r
  origin/branch1
  origin/master
切換分支
$ git checkout branch1
Switched to branch 'branch1'
相當於把branch1作爲當前分支,可以 git branch 看下變化
刪除分支
$ git branch -d branch1
以上命令,git會檢查該分支是否已合併到上游分支,如果沒有,則不能刪除分支。
但如果真的要刪除該分支,使用 -D 參數強行刪除:
$ git branch -D branch1
如果想刪除github上的分支 branch1,
$ git push origin --delete branch1
或者是
$ git push origin :branch1
重命名分支
$ git branch -m branch1 branch2
合併分支
git有三種合併分支方式,straight merge, squashed commits 和 cherry-picking
區別如下:
1、直接合並(straight merge)
   將某分支所有的歷史記錄全部合併到當前分支,原來分支有多少個commit,當前分支就增加多少條日誌
 命令如下:
    $ git merge branch1
2、拼湊合並(squashed commits)
 將某分支上的所有的歷史記錄合成一條日誌提交,這樣的話,當前分支無法看到該分支每次的提交記錄。
 命令如下:
    $ git merge --squash branch1
    $ git commit -m 'branch1 merge'
3、挑選合併(cherry-picking)
    將分支的某些提交日誌合併到當前分支
  命令如下:
     $ git cherry-pick 7654321
   以上,7654321是提交日誌的hash值,可以從git log 查到,本來是40位,但git通常需要前面7位就可以識別了。
   如果需要合併多條日誌,但不想git每次都立刻合併,可以改下這個命令:
     $ git cherry-pick -n 7654321
提交到遠程分支
提交到遠程倉庫github的分支 branch1,分支不存在會自動創建
$ git push origin branch1
導出分支
類似svn export,例如導出master分支:
git archive master -o ../master.zip

分支的使用

這裏以最簡單實用的主次分支模型,也就是兩條分支,一條記master,另一條記develop,項目在develop下開發,再不定期合併到master分支。

首先,我們需要定位好 master 和 develop 的關係:
master :主分支,就是生產環境的版本
develop : 次分支,就是開發環境的版本

git創建時有master分支,我們需要再創建一個分支。使用下面這個命令,從master分支位置創建 develop 分支,並將當前分支切換到 develop 分支
 $ git checkout -b develop master
這樣,我們可以在 develop 分支下開發了。直到項目需要交付給用戶時,再將 develop分支 合到 master分支
 $ git checkout master
 $ git merge develop

當 develop分支合到 master 分支後,如果要繼續開發,再把 develop 分支切換成當前分支
 $ git checkout develop
這樣,又回到了開發分支,接着發佈、開發,發佈、開發,如此週而復始。

當然,分支在實際使用中可能不只這麼簡單,可能還要創建臨時的 bugfix 分支,但方法都是差不多的。
1、創建一個bugfix 的臨時分支:
 $ git checkout -b bugfix-0.1 master
2、修正bug後,再合併到master分支和develop分支
 $ git checkout master
 $ git merge --no-ff bugfix-0.1
 $ git checkout develop
 $ git merge --no-ff bugfix-0.1
3、再然後,刪掉這個臨時分支
 $ git branch -d bugfix-0.1

細心的同學會觀察到我們這裏使用了 --no-ff 參數,這是因爲,Git 默認使用快進式合併(fast-farward merge),只是將 master 分支指針指向 bugfix-0.1分支,而 --no-ff 則會創建一條合併日誌,保證bugfix-0.1分支刪除時不丟失歷史日誌。


git相關

git config之 push.default

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

大致意思是,Git2.0 將 push.default 的默認值 matching 改成了 simple
matching
如果 git push 沒有指定分支,git 會推送所有遠程分支對應的本地分支
simple
如果 git push 沒有指定分支,git 只推送當前分支


參考資料:
[3] A successful Git branching model  Vincent Driessen
[4] Git分支管理策略 阮一峯

參考:http://blog.csdn.net/mycwq/article/details/49443775
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章