Git是什麼?
git
Git是世界最早進的分佈式版本控制系統之一。github
1 安裝
vim
[root@localhost ~]# yum -y install gitapp
[root@localhost ~]# git --versiondom
git version 1.7.1ssh
2 版本庫操做分佈式
建立版本庫
ide
版本庫又名倉庫,英文名repository,能夠簡單理解爲一個目錄。這個目錄裏全部的文件均可以被Git管理起來,而且每一個文件的刪除、修改Git都能作到跟蹤,以便在未來某個時刻進行「還原」。網站
[root@localhost ~]# mkdir -pv /date/gitdir #建立新目錄this
[root@localhost ~]# cd /date/gitdir/
[root@localhost gitdir]# git init #把當前目錄變成Git能夠管理的倉庫
Initialized empty Git repository in /date/gitdir/.git/
[root@localhost gitdir]# ls -a #目錄下有.git說明建立成功
. .. .git
把文件加入版本庫
首先編寫文件:
[root@localhost gitdir]# cd /date/gitdir/
[root@localhost gitdir]# vim a.txt
This is a.txt
[root@localhost gitdir]# vim b.txt
This is b.txt
把文件提交到暫存區,使用git add:
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git add b.txt
把文件從暫存區提交到倉庫的當前分支(事先必須已提交到暫存區),使用git commit,-m爲說明信息:
[root@localhost gitdir]# git commit -m "add 3 files"
[master (root-commit) b9d90d7] add 3 files
如今來修改下文件a.txt
[root@localhost gitdir]# vim a.txt
This is a.txt ,this is not b.txt
使用git status 能夠獲取當前倉庫的狀態,下面的命令告訴咱們,a.txt被修改過了,可是尚未提交。
[root@localhost gitdir]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
若是想知道修改的內容,請使用git diff
[root@localhost gitdir]# git diff a.txt
diff --git a/a.txt b/a.txt
index e7a5e02..50fcf2b 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,2 @@
-This is a.txt
+This is a.txt ,this is not b.txt
再次提交
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git commit -m "add xiugai"
1 files changed, 1 insertions(+), 1 deletions(-)
再查看狀態,告訴咱們沒有要提交的修改:
[root@localhost gitdir]# git status
# On branch master
nothing to commit (working directory clean)
查看提交的歷史記錄 git log,顯示從最近到最遠的提交日誌。
[root@localhost gitdir]# git log
commit 2ee955e3cd0892fed757bb8f3c1300c04cb92e9e
Author: root <root@localhost.localdomain>
Date: Mon May 22 18:02:09 2017 +0800
add xiugai
commit b9d90d7efd809ac61c36539f6266131b7263f036
Author: root <root@localhost.localdomain>
Date: Mon May 22 17:37:52 2017 +0800
add 3 files
顯示更簡明的信息,能夠增長下面的參數,前面一大串的字符就是版本號
[root@localhost gitdir]# git log --pretty=oneline
2ee955e3cd0892fed757bb8f3c1300c04cb92e9e add xiugai
b9d90d7efd809ac61c36539f6266131b7263f036 add 3 files
版本回退
再次修改a.txt
[root@localhost gitdir]# vim a.txt
This is a.txt ,this is not b.txt
xiugai
提交
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git commit -m "2 xiugai"
1 files changed, 2 insertions(+), 0 deletions(-)
查看提交的歷史記錄
[root@localhost gitdir]# git log
commit 8ce2bfbb4db6d8dbbdb3eb2d4a2017296db98096
Author: root <root@localhost.localdomain>
Date: Tue May 23 19:25:30 2017 +0800
2 xiugai
commit 2ee955e3cd0892fed757bb8f3c1300c04cb92e9e
Author: root <root@localhost.localdomain>
Date: Mon May 22 18:02:09 2017 +0800
add xiugai
commit b9d90d7efd809ac61c36539f6266131b7263f036
Author: root <root@localhost.localdomain>
Date: Mon May 22 17:37:52 2017 +0800
add 3 files
版本回退使用命令 git reset,在Git中HEAD 表示當前版本,HEAD^ 表示上一個版本,HEAD^^表示上上個版本,若是往前10個版本則表示爲HEAD~10,注意參數--hard
[root@localhost gitdir]# git reset --hard HEAD^
HEAD is now at 2ee955e add xiugai
[root@localhost gitdir]# cat a.txt
This is a.txt ,this is not b.txt
再次查看狀態,發現最終修改的版本不見了
[root@localhost gitdir]# git log
commit 2ee955e3cd0892fed757bb8f3c1300c04cb92e9e
Author: root <root@localhost.localdomain>
Date: Mon May 22 18:02:09 2017 +0800
add xiugai
commit b9d90d7efd809ac61c36539f6266131b7263f036
Author: root <root@localhost.localdomain>
Date: Mon May 22 17:37:52 2017 +0800
add 3 files
若是如今想找回最終修改的版本怎麼辦呢,看下面的操做,注意版本號能夠只寫前面幾位。
首先查看全部提交的版本號和命令記錄,
[root@localhost gitdir]# git reflog
8ce2bfb HEAD@{0}: 8ce2bf: updating HEAD
2ee955e HEAD@{1}: HEAD^: updating HEAD
8ce2bfb HEAD@{2}: commit: 2 xiugai
2ee955e HEAD@{3}: commit: add xiugai
b9d90d7 HEAD@{4}: commit (initial): add 3 files
回到將來的某個版本
[root@localhost gitdir]# git reset --hard 8ce2bfb
HEAD is now at 8ce2bfb 2 xiugai
[root@localhost gitdir]# cat a.txt
This is a.txt ,this is not b.txt
xiugai
工做區和暫存區:
工做區:系統裏能看到的目錄,如以前建立的目錄/gitdir 就是一個工做區。
版本庫:工做區裏有一個隱藏目錄.git,即是Git的版本庫。
版本庫裏有不少東西,其中最重要的就是暫存區(或叫index)。
撤銷修改:
撤銷修改使用命令git checkout,在這裏有兩種狀況:
1)文件修改後沒有放到暫存區,如今撤銷修改後就回到和版本庫如出一轍的狀態;
2)文件添加到暫存區後,又作了修改。撤銷修改就回到當初添加到暫存區的狀態;
修改文件a.txt
[root@localhost gitdir]# vim a.txt
This is a.txt ,this is not b.txt
xiugai
Taday 05/25
文件沒有放到暫存區查看倉庫當前的狀態,Git會提示你提交修改或撤銷修改:
[root@localhost gitdir]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#c.txt
no changes added to commit (use "git add" and/or "git commit -a")
撤銷修改:
[root@localhost gitdir]# git checkout -- a.txt
查看文件:
[root@localhost gitdir]# cat a.txt
This is a.txt ,this is not b.txt
xiugai
修改文件a.txt
[root@localhost gitdir]# cat a.txt
This is a.txt ,this is not b.txt
xiugai
05/25
提交到暫存區:
[root@localhost gitdir]# git add a.txt
查看狀態,Git會提醒你,使用命令 git reset HEAD 把暫存區文件a.txt從新放回到工做區:
[root@localhost gitdir]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#c.txt
[root@localhost gitdir]# git reset HEAD a.txt
Unstaged changes after reset:
Ma.txt
在工做區撤銷修改:
[root@localhost gitdir]# git checkout -- a.txt
[root@localhost gitdir]# cat a.txt
This is a.txt ,this is not b.txt
xiugai
刪除文件:
首先建立文件
[root@localhost gitdir]# vim d.txt
This is d.txt
而後提交
[root@localhost gitdir]# git add d.txt
[root@localhost gitdir]# git commit -m "add d.txt"
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 d.txt
若是要刪除此文件,直接使用rm命令,
[root@localhost gitdir]# rm d.txt
走到這一步,有兩個選擇,
1)刪錯了,對其進行還原,實際上是把版本庫的版本複製到工做區
[root@localhost gitdir]# git checkout -- d.txt
[root@localhost gitdir]# ls
a.txt b.txt c.txt d.txt
2)肯定要刪除此文件,使用git rm 命令,而後git commit
[root@localhost gitdir]# git rm d.txt
rm 'd.txt'
[root@localhost gitdir]# git commit -m "remove d.txt"
...
delete mode 100644 d.txt
3 遠程倉庫,GitHub網站是提供Git倉庫託管服務的,因此只要註冊一個帳號,就能夠得到一個免費的Git遠程倉庫。這個倉庫任何人均可以看到但只有本身能夠修改。
1)在https://github.com/註冊帳號,創建倉庫,並設置SSH服務。
帳號註冊(略)
設置SSH服務,首先生成密鑰對:
[root@localhost ~]# ssh-keygen -t rsa -C "youremail@qq.com"
而後登錄GitHub,打開「Account settings」,「SSH Keys」頁面:而後,點「Add SSH Key」,填上任意Title,在Key文本框裏粘貼id_rsa.pub
文件的內容:
2)關聯遠程倉庫,遠程庫的名字就是origin
,這是Git默認的叫法,也能夠改爲別的。
[root@localhost gitdir]# git remote add origin https://github.com/zen9/test.git
把本地庫的內容推送到遠程庫上,master爲當前分支,-u參數會把本地master分支和遠程倉庫的master分支關聯起來。
[root@localhost gitdir]# git push -u origin master
從如今起,只要本地庫提交了修改,就能夠經過下面的命令把修改推送至GitHub。注意,第一次推送時須要使用-u的參數。
[root@localhost gitdir]# git push origin master
從遠程庫克隆
從遠程庫克隆到本地
[root@localhost gitdir]# git clone git@github.com:z949/test.git
4 分支管理
建立和合並分支:
在Git中,master分支爲主分支,咱們能夠創建新的分支,
建立dev分支,並切換到dev分支,git checkout
命令加上-b
參數表示建立並切換
[root@localhost gitdir]# git checkout -b dev
DREADME.md
Da.txt
Db.txt
Dd.txt
De.txt
Switched to a new branch 'dev'
查看當前分支:
[root@localhost gitdir]# git branch
* dev
備註:建立和切換分支也能夠經過如下命令實現:
[root@localhost gitdir]# git branch zwj
[root@localhost gitdir]# git checkout zwj
[root@localhost gitdir]# git branch
dev
master
* zwj
在zwj分支下(當前分支)編輯f.txt文件:
[root@localhost gitdir]# cat f.txt
This is f.txt
而後提交:
[root@localhost gitdir]# git add f.txt
[root@localhost gitdir]# git commit -m "add f.txt"
[zwj 23ee86c] add f.txt
切換到master分支:
[root@localhost gitdir]# git checkout master
Switched to branch 'master'
在master分支上發現沒有f.txt文件,由於以前的提交是在zwj分支上
[root@localhost gitdir]# ls
a.txt b.txt d.txt e.txt README.md test
如今,把zwj分支上的工做成果合併到master分支(當前分支)上,命令 git merge 用於合併指定分支到當前分支。
[root@localhost gitdir]# git merge zwj
Updating 6daf74a..23ee86c
Fast-forward
README.md | 2 --
a.txt | 5 +----
b.txt | 1 -
d.txt | 1 -
f.txt | 1 +
5 files changed, 2 insertions(+), 8 deletions(-)
如今能夠查看f.txt文件了,和zwj分支提交的徹底同樣:
[root@localhost gitdir]# cat f.txt
This is f.txt
合併完成後,能夠刪除zwj分支了:
[root@localhost gitdir]# git branch -d zwj
Deleted branch zwj (was 23ee86c).
[root@localhost gitdir]# git branch
dev
* master
解決衝突
新建test1分支:
[root@localhost gitdir]# git checkout -b test1
Switched to a new branch 'test1'
修改文件a.txt:
[root@localhost gitdir]# vim a.txt
This is a.txt and date is 04
在test1分支中提交:
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git commit -m "add a.txt"
[test1 3d8b8bd] add a.txt
切換到master分支:
[root@localhost gitdir]# git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 2 commits.
而後修改a.txt
[root@localhost gitdir]# vim a.txt
This is a.txt and date 100
提交:
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git commit -m "add a a.txt"
[master 01f6560] add a a.txt
嘗試合併到分支test1,發現存在衝突(由於修改的是同一文件的同一行)
[root@localhost gitdir]# git merge test1
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
命令git status 也能夠告訴咱們衝突的文件 :
[root@localhost gitdir]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
#both modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#test/
no changes added to commit (use "git add" and/or "git commit -a")
或者直接查看a.txt文件:
[root@localhost gitdir]# cat a.txt
<<<<<<< HEAD
This is a.txt and date 100
=======
This is a.txt and date is 04
>>>>>>> test1
對a.txt做出以下修改:
[root@localhost gitdir]# vim a.txt
This is a.txt and date is 04
再次提交:
[root@localhost gitdir]# git add a.txt
[root@localhost gitdir]# git commit -m "2017 a.txt"
使用如下命令查看分支合併的狀況:
[root@localhost gitdir]# git log --graph --pretty=oneline --abbrev-commit
* c13503d 2017 a.txt
|\
| * 3d8b8bd add a.txt
* | 01f6560 add a a.txt
|/
* 23ee86c add f.txt
* 78db2d8 add a.txt
* 6daf74a add e.txt
* c45d7c8 Merge branch 'master' of github.com:zengwj1949/MyRepository
|\
| * 4d2d8fb Initial commit
* 4cd4265 add d.txt
* 8ce2bfb 2 xiugai
* 2ee955e add xiugai
* b9d90d7 add 3 files
最後,刪除分支test1:
[root@localhost gitdir]# git branch -d test1
Deleted branch test1 (was 3d8b8bd).
總結:當Git沒法自動合併分支時,必須首先解決衝突,而後再提交,合併完成。
未完待續