Git教程

Git是什麼

Git是具有快速靈活分佈式特性的版本控制系統,內置了豐富的命令。

快速在簡單命令即可實現版本控制的功能:

git commit -am "commit message"

靈活在隨時開分支以便實驗想法,敏捷的迭代,有想法就有分支:

git checkout -b i_have_an_idea

分佈式體現在你可以隨時把代碼提交到本地倉庫,公司斷網你依然可以在本地倉庫自己玩,即使公司Git服務器丟失損毀,每個人的本地倉庫都是備份。

離線倉庫意味着大多數的操作都可以本地完成,比如查看文件修改歷史(建議用IDEA,可視化做得很好):

git log -p pom.xml

瞬間就可以查到這文件自創建以來所有的修改記錄,作爲對比的SVN則慢得無以復加。

最好在能熟練使用git commands後再使用GUI,這有利於理解每一步Git究竟做了什麼,否則你會發現Git有各種莫名其妙的錯誤,輕則push失敗,重則回滾他人代碼。

Git常用操作

下面講講Git的基本操作

新建倉庫:

# 在當前目錄新建一個Git代碼庫
$ git init

# 下載一個項目和它的整個代碼歷史
$ git clone [url]

修改提交:

# 將文件此時的變化加入暫存區
$ git add filename.txt 

# 將暫存區中的文件提交到本地倉庫
$ git commit -m "commit message"

# 更新遠程代碼到本地倉庫
$ git fetch origin 
# 將遠程倉庫master分支merge到當前分支
# 如要取消此次衝突合併,切記使用git merge --abort,否則使用git reset --hard會導致他人的commit被你沖掉
$ git merge origin/master

# 拉取遠程倉庫的代碼,併合併到本地origin爲遠程倉庫
# 等於git fetch + git merge,
$ git pull origin

# 將本地倉庫推送到遠程倉庫,origin爲遠程倉庫,master爲本地分支名,也是Git默認的主線
$ git push origin master

中級操作:

# 新建一個tag在當前commit,比如發版本後,記錄此commit,有緊急發佈可以在這個tag處開緊急發佈分支
$ git tag [tag]

# 列出所有本地分支
$ git branch

# 列出所有遠程分支
$ git branch -r

# 列出所有本地分支和遠程分支
$ git branch -a

# 新建一個分支,但依然停留在當前分支
$ git branch [branch-name]

# 新建一個分支,並切換到該分支
$ git checkout -b [branch]

# 新建一個分支,指向指定commit
$ git branch [branch] [commit]

# 新建一個分支,與指定的遠程分支建立追蹤關係
$ git branch --track [branch] [remote-branch]

# 切換到指定分支,並更新工作區
$ git checkout [branch-name]

# 切換到上一個分支
$ git checkout -

# 建立追蹤關係,在現有分支與指定的遠程分支之間
$ git branch --set-upstream [branch] [remote-branch]

# 合併指定分支到當前分支
$ git merge [branch]

# 選擇一個commit,合併進當前分支,這個功能我們在SVN中用的多,實際不建議這樣使用,容易造成文件提交歷史的混亂
$ git cherry-pick [commit]

# 刪除分支
$ git branch -d [branch-name]

# 刪除遠程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]


# 恢復暫存區的指定文件到工作區
$ git checkout [file]

# 恢復某個commit的指定文件到暫存區和工作區
$ git checkout [commit] [file]

# 恢復暫存區的所有文件到工作區
$ git checkout .

# 重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
$ git reset [file]

# 重置暫存區與工作區,與上一次commit保持一致,用的較多,IDEA中的Revert功能
$ git reset --hard

# 暫存當前工作區代碼:
# 當你正在進行項目中某一部分的工作,裏面的東西處於一個比較雜亂的狀態,
# 而你想轉到其他分支上進行一些工作,可你不想提交進行了一半的工作,否則以後你無法回到這個工作點。
# 解決這個問題的辦法就是git stash命令。
$ git stash

高級玩法

  1. git rebase 除非你非常熟悉git否則不要玩這個!!!!代碼丟了找都找不回來!!

當你在自己的分支上進行了大量的commit,可能這些commit有很多都是無意義的rename variable之類的,合併到主線會影響分支線的美觀,這時候可使用git rebase命令來壓縮commit。
需要注意的是最好只壓縮本地的commit,或你確定該遠程分支只有你一人使用,原因是git rebase會修改git倉庫的歷史,可能會和他人的倉庫衝突。

# 壓縮最近4次提交,會進入交互,將需要壓縮的commit從pick改爲squash
$ git rebase -i HEAD~4
$ git add .
$ git rebase --continue # 放棄本次rebase使用git rebase --abort
$ git push -f origin master # -f強制推送,本地的倉庫覆蓋遠程分支,忽略衝突,master分支應禁止此操作
  1. 修改commit message,使用idea很方便
$ git commit --amend
# ...vim your file...
$ git commit --continue
  1. Patch,用的不多
$ git diff branch1 branch2 > diff.patch
$ git am diff.path # 將patch補丁提交到當前分支
  1. Revert與Reset,使用idea很方便
git revert HEAD # 使用一個新的commit來抵充上一次提交,有commit hisroty
git reset # 使用暫存區覆蓋工作區,沒有commit history

更多的Git用法可以看官方文檔,有中文。
也可以使用命令(無中文)如:

$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

如果在具體的指令後加--help則會打開本地網頁,如git branch --help,內容爲官方文檔的英文版

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