Git是分布式版本控制系統,沒有中央服務器,每個人的電腦就是一個完整的版本庫,工作的時候不需要聯網了,因為版本都在自己電腦上。協同的方法是這樣的:比如說自己在電腦上改了文件A,其他人也在電腦上改了文件A,這時,你們兩之間只需把各自的修改推送給對方,就可以互相看到對方的修改了。
一、git常用術語:
1)、倉庫(Repository)
受版本控制的所有文件修訂歷史的共享數據庫
2)、工作空間(Workspace)
本地硬盤或Unix 用戶帳戶上編輯的文件副本
3)、工作樹/區(Working tree)
工作區中包含了倉庫的工作文件。您可以修改的內容和提交更改作為新的提交到倉庫。
4)、暫存區(Staging area)
暫存區是工作區用來提交更改(commit)前可以暫存工作區的變化。
5)、索引(Index)
索引是暫存區的另一種術語。
6)、簽入(Checkin)
將新版本復制回倉庫
7)、簽出(Checkout)
從倉庫中將文件的最新修訂版本復制到工作空間
8)、提交(Commit)
對各自文件的工作副本做了更改,並將這些更改提交到倉庫
9)、沖突(Conflict)
多人對同一文件的工作副本進行更改,並將這些更改提交到倉庫
10)、合並(Merge)
將某分支上的更改聯接到此主干或同為主干的另一個分支
11)、分支(Branch)
從主線上分離開的副本,默認分支叫master
12)、鎖(Lock)
獲得修改文件的專有權限。
13)、頭(HEAD)
頭是一個象征性的參考,最常用以指向當前選擇的分支。
14)、修訂(Revision)
表示代碼的一個版本狀態。Git通過用SHA1 hash算法表示的ID來標識不同的版本。
15)、標記(Tags)
標記指的是某個分支某個特定時間點的狀態。通過標記,可以很方便的切換到標記時的狀態。
二、git的工作流程一般:
1、在工作目錄中添加、修改文件;
2、將需要進行版本管理的文件放入暫存區域;
3、將暫存區域的文件提交到git倉庫。
三、工作區
Git本地有三個工作區域:
1、工作目錄(Working Directory)
2、暫存區(Stage/Index)
3、資源庫(Repository或Git Directory)
遠程有一個工作區域:
1、git倉庫(Remote Directory)
在push到遠程git倉庫之前,所有的操作都是在本地進行的。
四、git文件狀態及互相轉化:
1、Untracked:未跟蹤
2、Unmodify:未修改
3、Modified:已修改
4、Staged:已暫存
Untracked: 未跟蹤, 此文件在文件夾中, 但並沒有加入到git庫, 不參與版本控制. 通過git add
狀態變為Staged
.
Unmodify: 文件已經入庫, 未修改, 即版本庫中的文件快照內容與文件夾中完全一致. 這種類型的文件有兩種去處, 如果它被修改, 而變為Modified
. 如果使用git rm
移出版本庫, 則成為Untracked
文件
Modified: 文件已修改, 僅僅是修改, 並沒有進行其他的操作. 這個文件也有兩個去處, 通過git add
可進入暫存staged
狀態, 使用git checkout
則丟棄修改過, 返回到unmodify
狀態, 這個git checkout
即從庫中取出文件, 覆蓋當前修改
Staged: 暫存狀態. 執行git commit
則將修改同步到庫中, 這時庫中的文件和本地文件又變為一致, 文件為Unmodify
狀態. 執行git reset HEAD filename
取消暫存, 文件狀態為Modified
五、git --help:
These are common Git commands used in various situations: 這些都是各種情況下常見的Git命令:
start a working area (see also: git help tutorial) 創建一個工作區(參見:Git幫助教程)
clone Clone a repository into a new directory clone 克隆一個倉庫到一個新目錄
init Create an empty Git repository or reinitialize an existing one init 創建一個空的Git倉庫或重新初始化現有倉庫
work on the current change (see also: git help everyday) 操作當前工作空間(參見:Git日常幫助)
add Add file contents to the index add 向索引(暫存區)中添加文件內容
mv Move or rename a file, a directory, or a symlink mv 移動或重命名文件、目錄,或者符號關聯
reset Reset current HEAD to the specified state reset 復位HEAD(常用以指向當前選擇的分支)到指定狀態
rm Remove files from the working tree and from the index rm 從工作樹(包含了倉庫的工作文件)和索引(暫存區)刪除文件
examine the history and state (see also: git help revisions) 查看歷史和狀態(參見:Git修訂幫助)
bisect Use binary search to find the commit that introduced a bug bisect 使用二進制搜索找到出錯的提交
grep Print lines matching a pattern grep 打印出匹配的行
log Show commit logs log 顯示提交日志
show Show various types of objects show 顯示各種類型的對象
status Show the working tree status status 顯示工作樹的狀態
grow, mark and tweak your common history 生成、標記和調整歷史
branch List, create, or delete branches branch 查看、創建或刪除分支
checkout Switch branches or restore working tree files checkout 切換分支或恢復工作樹文件
commit Record changes to the repository commit 記錄更改到數據庫
diff Show changes between commits, commit and working tree, etc diff 顯示兩次提交之間的變化,提交和工作樹之間的變化,等等
merge Join two or more development histories together merge 合並兩個或更多個開發歷史
rebase Reapply commits on top of another base tip rebase 申請覆蓋上一次提交
tag Create, list, delete or verify a tag object signed with GPG tag 用GPG創建,查看,刪除或驗證一個有符號的對象
collaborate (see also: git help workflows) 協作(參見:Git幫助工作流)
fetch Download objects and refs from another repository fetch 從另一個庫下載項目或文檔
pull Fetch from and integrate with another repository or a local branch pull 將其他的庫或本地分支整合
push Update remote refs along with associated objects push 更新遠程項目和文檔
六、命令
1、配置
#添加配置項 git config [--local|--global|--system] section.key value git config --system user.name wang #系統級,配置信息適用當前系統的所有用戶和項目, 配置文件位置Win:C:\Program Files\Git\mingw64\etc\gitconfig git config --global user.name wang #當前用戶級,配置信息適用當前用戶的所有項目, 配置文件位置Win:C:\Users\Administrator\.gitconfig
git config --local user.name wang #項目級,配置信息僅適用於當前項目, 配置文件位置Win:C:\gitProject
注意:對於同一配置項,三個配置文件的優先級:system<global<local
#刪除配置項 git config [--local|--global|--system] --unset section.key git config [--local|--global|--system] --unset user.name
#更多配置項 git config --global color.ui true #打開所有的默認終端着色 git config --global alias.ci commit #別名 ci 是commit的別名 [alias] co = checkout ci = commit st = status pl = pull ps = push dt = difftool l = log --stat cp = cherry-pick ca = commit -a b = branch user.name #用戶名 user.email #郵箱 core.editor #文本編輯器 merge.tool #差異分析工具 core.paper "less -N" #配置顯示方式 color.diff true #diff顏色配置 alias.co checkout #設置別名 git config user.name #獲得用戶名 git config core.filemode false #忽略修改權限的文件
#所有config命令參數 語法: git config [<options>] 文件位置 --global #use global config file 使用全局配置文件 --system #use system config file 使用系統配置文件 --local #use repository config file 使用存儲庫配置文件 -f, --file <file> #use given config file 使用給定的配置文件 --blob <blob-id> #read config from given blob object 從給定的對象中讀取配置 動作 --get #get value: name [value-regex] 獲得值:[值]名[正則表達式] --get-all #get all values: key [value-regex] 獲得所有值:[值]名[正則表達式] --get-regexp #get values for regexp: name-regex [value-regex] 得到的值根據正則 --get-urlmatch #get value specific for the URL: section[.var] URL 為URL獲取特定的值 --replace-all #replace all matching variables: name value [value_regex] 替換所有匹配的變量:名稱值[ value_regex ] --add #add a new variable: name value 添加一個新變量:name值 --unset #remove a variable: name [value-regex] 刪除一個變量名[值]:正則表達式 --unset-all #remove all matches: name [value-regex] 刪除所有匹配的正則表達式:名稱[值] --rename-section #rename section: old-name new-name 重命名部分:舊名稱 新名稱 --remove-section #remove a section: name 刪除部分:名稱 -l, --list #list all 列出所有 -e, --edit #open an editor 打開一個編輯器 --get-color #find the color configured: slot [default] 找到配置的顏色:插槽[默認] --get-colorbool #find the color setting: slot [stdout-is-tty] 發現顏色設置:槽[ stdout是TTY ] 類型 --bool #value is "true" or "false" 值是“真”或“假”。 --int #value is decimal number 值是十進制數。 --bool-or-int #value is --bool or --int 值--布爾或int --path #value is a path (file or directory name) 值是路徑(文件或目錄名) 其它 -z, --null #terminate values with NUL byte 終止值與null字節 --name-only #show variable names only 只顯示變量名 --includes #respect include directives on lookup 尊重包括查找指令 --show-origin #show origin of config (file, standard input, blob, command line) 顯示配置(文件、標准輸入、數據塊、命令行)的來源
2、常用指令
#創建工作目錄 git init 在當前目錄新建一個Git代碼庫 git init [project-name] 新建一個目錄,將其初始化為Git代碼庫 git clone [url] 克隆一個項目和它的整個代碼歷史(版本信息)
#操作文件
git status [filename] 查看指定文件的狀態
git status 查看所有文件的狀態 git add [file1] [file2] ... 添加指定文件到暫存區 git add [dir] 添加指定目錄到暫存區,包括子目錄 git add . 添加當前目錄的所有文件到暫存區 git commit 提交,暫存區的目錄樹寫到版本庫(對象庫)中,master 分支會做相應的更新。即 master 指向的目錄樹就是提交時暫存區的目錄樹。 git checkout .簽出,用暫存區全部或指定的文件替換工作區的文件,匯總顯示工作區、暫存區與HEAD的差異。這個操作很危險,會清除工作區中未添加到暫存區的改動。 git checkout HEAD .會用 HEAD 指向的 master 分支中的全部或者部分文件替換暫存區和以及工作區中的文件。這個命令也是極具危險性的,因為不但會清除工作區中未提交的改動,也會清除暫存區中未提交的改 動。 git checkout -- filename 用暫存區中filename文件來覆蓋工作區中的filename文件。相當於取消自上次執行git add filename以來(如果執行過)的本地修改。 git rm --cached <file> 直接從暫存區刪除文件,工作區則不做出改變 git reset HEAD <file>... 通過重寫目錄樹移除add文件,如果已經用add 命令把文件加入stage了,就先需要從stage中撤銷 git clean [options] 移除所有未跟蹤文件,一般會加上參數-df,-d表示包含目錄,-f表示強制清除。 git rm --cached readme.txt 只從stage中刪除,保留物理文件 git rm readme.txt 不但從stage中刪除,同時刪除物理文件 git mv a.txt b.txt 把a.txt改名為b.txt git diff [files] 查看文件修改后的差異 git diff --cached 比較暫存區的文件與之前已經提交過的文件 git diff HEAD~n 比較repo與工作空間中的文件差異
git log 顯示當前分支的版本歷史
git log --stat 顯示commit歷史,以及每次commit發生變更的文件
git commit -m "first commit" 提交到本地倉庫並寫日志
git remote add origin https://github.com/wangnl/test.git 添加遠程主機,主機名為origin 地址為https://github.com/wangnl/test.git
git push -u origin master 本地的master分支推送到origin主機,同時指定origin為默認主機,后面就可以不加任何參數使用git push了,-u 參數指定一個默認主機
歸納如圖:
七、分支(待)