# cat /etc/redhat-releasegit
CentOS release 6.8 (Final)vim
# uname -r緩存
2.6.32-642.4.2.el6.x86_64bash
# uname -mapp
x86_64ide
能夠直接經過源碼安裝。先從Git官網下載源碼,而後解壓,依次輸入:./config
,make
,sudo make install
這幾個命令安裝就行了。測試
這裏用的是yum安裝url
[root@laowang ~]# rpm -qa gitspa
git-1.7.1-4.el6_7.1.x86_64日誌
[root@laowang ~]# yum install git –y
[root@laowang ~]# git --version
git version 1.7.1
什麼是版本庫呢?版本庫又名倉庫,英文名repository,你能夠簡單理解成一個目錄,這個目錄裏面的全部文件均可以被Git管理起來,每一個文件的修改、刪除,Git都能跟蹤,以便任什麼時候刻均可以追蹤歷史,或者在未來某個時刻能夠「還原」。
因此,建立一個版本庫很是簡單,首先,選擇一個合適的地方,建立一個空目錄:
建立版本庫目錄:
[root@laowang /]# mkdir gitdir
[root@laowang /]# cd gitdir/
[root@laowang gitdir]# git init
Initialized empty Git repository in/gitdir/.git/
建立完版本庫以後會多出一個隱藏文件
[root@laowang gitdir]# ls -a
. .. .git
[root@laowang gitdir]# touch file.txt 建立文件
[root@laowang gitdir]# git add file.txt 添加到管理庫
[root@laowang gitdir]# git commit -m "addone file dor test" 提交
[master (root-commit) d042b99] add one filedor test
0files changed, 0 insertions(+), 0 deletions(-)
createmode 100644 file.txt 這裏顯示建立一個文件
[root@laowang gitdir]# echo"test" >>./file.txt
[root@laowang gitdir]# git commit -m"add one word for test"
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will becommitted)
# (use "git checkout -- <file>..." to discard changes inworking directory)
#
# modified: file.txt 這裏顯示修改的文件名
#
no changes added to commit (use "gitadd" and/or "git commit -a") 沒有文件提交
[root@laowang gitdir]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will becommitted)
# (use "git checkout -- <file>..." to discard changes inworking directory)
#
# modified: file.txt
#
no changes added to commit (use "gitadd" and/or "git commit -a")
[root@laowang gitdir]# git diff file.txt
diff --git a/file.txt b/file.txt
index 1153985..cf19221 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
test
1234
-5678 刪除的內容
+789 增長的內容
[root@laowang gitdir]# echo"0000">>file.txt
[root@laowang gitdir]# git diff file.txt
diff --git a/file.txt b/file.txt
index 1153985..2744a10 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
test
1234
-5678
+789
+0000
[root@laowang gitdir]# git add file.txt
[root@laowang gitdir]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file.txt
#
[root@laowang gitdir]# git commit file.txt -m"add 0000"
[master ec76b27] add 0000
1files changed, 2 insertions(+), 1 deletions(-)
[root@laowang gitdir]# git status
# On branch master
nothing to commit (working directory clean)
放咱們對一個文件進行屢次修改以後,如果想查看以前的修改記錄,光是靠回憶確定是不靠譜的。
版本查看
[root@laowang gitdir]# git log
commit ec76b27efc328da568f9462563848d89aaa25a54
Author: oldwang <18539641511@163.com>
Date: WedSep 21 05:07:29 2016 +0800
add 0000
commit 0ecd6c9c3a76c7028141ddc4242dcefa5f556c44
Author: oldwang <18539641511@163.com>
Date: WedSep 21 05:01:23 2016 +0800
add somenumbel
commit d042b99b866063f7ed2594eea60d032e90402361
Author: oldwang <18539641511@163.com>
Date: WedSep 21 04:42:19 2016 +0800
add one file dor test
如果嫌棄他們輸出信息太多能夠試試如下參數
[root@laowang gitdir]# git log --pretty=oneline
ec76b27efc328da568f9462563848d89aaa25a54add 0000
0ecd6c9c3a76c7028141ddc4242dcefa5f556c44add some numbel
d042b99b866063f7ed2594eea60d032e90402361add one file dor test
首先,Git必須知道當前版本是哪一個版本,在Git中,用HEAD
表示當前版本,也就是最新的提交3628164...882e1e0
(注意個人提交ID和你的確定不同),上一個版本就是HEAD^
,上上一個版本就是HEAD^^
,固然往上100個版本寫100個^
比較容易數不過來,因此寫成HEAD~100
。
如今,咱們要把當前版本「appendGPL」回退到上一個版本「adddistributed」,就可使用git reset
命令:
[root@laowang gitdir]# git reset --hard HEAD^ 回退到上一個版本
HEAD is now at 0ecd6c9 add some numbel
[root@laowang gitdir]# cat file.txt
test
1234
5678
[root@laowang gitdir]# git log --pretty=oneline
0ecd6c9c3a76c7028141ddc4242dcefa5f556c44 add somenumbel
d042b99b866063f7ed2594eea60d032e90402361 add onefile dor test
[root@laowang gitdir]# git reset --hard 0ecd6c9c
HEAD is now at 0ecd6c9 add some numbel
[root@laowang gitdir]# git log
commit 0ecd6c9c3a76c7028141ddc4242dcefa5f556c44
Author: oldwang <18539641511@163.com>
Date: WedSep 21 05:01:23 2016 +0800
add some numbel
commit d042b99b866063f7ed2594eea60d032e90402361
Author: oldwang <18539641511@163.com>
Date: WedSep 21 04:42:19 2016 +0800
add one file dor test
如今,你回退到了某個版本,關掉了電腦,次日早上就後悔了,想恢復到新版本怎麼辦?找不到新版本的commit id
怎麼辦?
在Git中,老是有後悔藥能夠吃的。當你用$ git reset --hard HEAD^
回退到add distributed
版本時,再想恢復到append GPL
,就必須找到append GPL
的commit id。Git提供了一個命令git reflog
用來記錄你的每一次命令:
[root@laowang gitdir]# git reflog
0ecd6c9 HEAD@{0}: 0ecd6c9c: updating HEAD
d042b99 HEAD@{1}: HEAD^: updating HEAD
0ecd6c9 HEAD@{2}: HEAD^: updating HEAD
ec76b27 HEAD@{3}: commit: add 0000
0ecd6c9 HEAD@{4}: commit: add some numbel
如今總結一下:
1,HEAD指向的版本就是當前版本,所以,Git容許咱們在版本的歷史之間穿梭,使用命令git reset --hard commit_id。
2,穿梭前,用git log能夠查看提交歷史,以便肯定要回退到哪一個版本。
3,要重返將來,用git reflog查看命令歷史,以便肯定要回到將來的哪一個版本。
Git的工做分爲三塊:工做區、緩存區(stage)、庫(./git)
[root@laowang gitdir]# vim file.txt
[root@laowang gitdir]# cat file.txt
zxas
i will add it to stage
i had add somethoing
[root@laowang gitdir]# git checkout -- file.txt
[root@laowang gitdir]# cat file.txt
zxas
i will add it to stage
分兩步,第一步用命令git reset HEAD file
,就回到了場景1,第二步git checkout -- file
[root@laowang gitdir]# echo "addsomething" >>./file.txt
[root@laowang gitdir]# cat file.txt
zxas
i will add it to stage
add something
[root@laowang gitdir]# git add file.txt
[root@laowang gitdir]# git status
# On branch master
# Changes to be committed:
# (use"git reset HEAD <file>..." to unstage)
#
# modified: file.txt
#
[root@laowang gitdir]# cat file.txt
zxas
i will add it to stage
add something
[root@laowang gitdir]# git reset HEAD file.txt
Unstaged changes after reset:
M file.txt
[root@laowang 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 workingdirectory)
#
# modified: file.txt
#
no changes added to commit (use "git add"and/or "git commit -a")
[root@laowang gitdir]# git checkout -- file.txt
本處參考版本回退
[root@laowang gitdir]# touch test.filr
[root@laowang gitdir]# vim test.filr
[root@laowang gitdir]# git add test.filr
[root@laowang gitdir]# dit commit -m "add afile for test"
-bash: dit: command not found
[root@laowang gitdir]# git commit -m "add afile for test"
[master 121fc12] add a file for test
1 fileschanged, 1 insertions(+), 0 deletions(-)
create mode100644 test.filr
[root@laowang gitdir]# git status
# On branch master
# Changed but not updated:
# (use"git add/rm <file>..." to update what will be committed)
# (use"git checkout -- <file>..." to discard changes in workingdirectory)
#
# deleted: test.filr
#
no changes added to commit (use "git add"and/or "git commit -a")
狀態告訴咱們文件刪除以後咱們有兩種選擇:
一、 吧刪除的數據提交到版本庫
二、 吧工做區回退到緩存區(就是文件沒被刪除以前)
如今你有兩個選擇,
git rm
刪掉,而且git commit
:
[root@laowang gitdir]# git checkout --test.filr
[root@laowang gitdir]# ls
file.txt test.filr
[root@laowang gitdir]# cat test.filr
test somethinhg
建立分支dev
root@laowang gitdir]# git checkout -b dev
Switched to a new branch 'dev'
git checkout命令加上-b參數表示建立並切換,至關於如下兩條命令:
git branch dev
gitcheckout dev
Switched to branch 'dev'
[root@laowang gitdir]# git branch
* dev
Master
[root@laowang gitdir]# git checkout master
Switched to branch 'master'
git merge dev
git merge 是合併的命令 後面接合並的分支名,表示合併dev到當前分支
在合併以後,以前的分支就能夠刪除了
[root@laowang gitdir]# git branch
dev
* master
[root@laowang gitdir]# git branch -d dev
Deleted branch dev (was df323f3).
[root@laowang gitdir]# git branch
* master
Git鼓勵大量使用分支:
查看分支:git branch
建立分支:git branch <name>
切換分支:git checkout <name>
建立+切換分支:git checkout -b <name>
合併某分支到當前分支:git merge <name>
刪除分支:git branch -d <name>