1. 程式人生 > >You have new mail in /var/spool/mail/root提示的原因與解決辦法

You have new mail in /var/spool/mail/root提示的原因與解決辦法

溫馨提示

此部落格用於記錄git常用的命令引數使用方法

遺忘或想不起來了可以來看一眼

所以寫的並不詳細、不適合初學者學習

 

環境說明

[[email protected] ~]# cat /etc/redhat-release  
CentOS release 6.10 (Final) 
[[email protected] ~]# uname -a Linux Check1.net 2.6.32-754.2.1.el6.x86_64 #1 SMP Fri Jul 13 12:50:12 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

 

git的安裝

[[email protected] ~]# yum -y install git

 

建立版本倉庫

[[email protected] ~]# mkdir -p /usr/local/git/test 
[[email protected] ~]# cd /usr/local/git/test/ [[email protected] test]# git i
nit  Initialized empty Git repository in /usr/local/git/test/.git/

 

建立版本庫後會在目錄內生成一個隱藏目錄.git  初始化一個空版本

[[email protected] test]# ls -al 總用量
 12 drwxr-xr-x 3 root root 4096 12月 16 20:51 
. drwxr-xr-x 3 root root 4096 12月 16 20:51 .. drwxr-xr-x 7 root
 root 4096 12月 16 20:51 .git [[email protected] test]# cd .git/ [[email protected]
. git]# ls branches config description HEAD hooks info objects refs

 

版本建立

[[email protected] test]# pwd /usr/local/git/test [root
@Check1 test]# echo "version 1.0" >> code.txt   編輯一個檔案或目錄,
在裡面新增內容 [[email protected] test]# git add code.txt   將此檔案新增到快取區 [roo
[email protected] test]# git commit -m "版本1.0"   提交此檔案建立版本記錄 -m "
本說明資訊" [master (root-commit) 0367a6a] 版本1.0 1 files changed, 1 ins
ertions(+), 0 deletions(-) create mode 100644 code.txt

 

再建立幾個版本

[[email protected] test]# echo "version 2.0" >> code.txt  
[[email protected] test]# git add code.txt  
[[email protected] test]# git commit -m "版本2.0" [master c94bcab]
 版本2.0 1 files changed, 1 insertions(+), 0 deletions(-) [
[email protected] test]# echo "version 3.0" >> code.txt  [[email protected]
heck1 test]# git add code.txt  [[email protected] test]# git commi
t -m "版本3.0" [master 9f98991] 版本3.0 1 files changed, 1 inser
tions(+), 0 deletions(-)

 

在git中建立版本記錄只是記錄一個修改後面的版本是依賴於前面的版本的

檢視版本記錄

[[email protected] test]# git log commit 9f9899114fd
b0f59b8cd0cb3e009f33f2b7702e5 Author: wcy <goo
[email protected]163.com> Date: Sun Dec 16 21:02:31 2018 +0800  
版本3.0  commit c94bcab611218e72f9d806ee347dcfb1d12e73a3 Auth
or: wcy <[email protected]163.com> Date: Sun Dec 16 21:01:58 2018 +0800

  版本2.0  commit 0367a6a3bd901309e282dc630e6562b7f270e39b Author: wc
y <[email protected]163.com> Date: Sun Dec 16 20:55:10 2018 +0800  版本1.0

 

版本回退

根據HEAD指標回退

HEAD指向當前版本

[[email protected] test]# git reset --hard HEAD^   
回退至上一個版本 HEAD is now at c94bcab 版本2.0

 

HEAD^指的是上一個版本 HEAD^^指的是上兩個版本 
上100個版本可以用 HEAD~100 
上200個版本可以用 HEAD~200表示

根據版本序列號回退

[[email protected] test]# git reset --hard 9f9899114f HEAD is now at 9f98991 版本3.0

 

版本序列號在上面git log中檢視git commit對應的字串就是  回退的時候不用填寫全部版本序列號,前幾位即可

如果Git的版本號通過git log檢視不到了可以通過git reflog檢視操作記錄

比如回退到版本3.0

[[email protected] test]# git reset --hard HEAD^ HEAD is now at c94bcab 
版本2.0 [[email protected] test]# git log commit c94bcab611218e72f9d80
6ee347dcfb1d12e73a3 Author: wcy <[email protected]163.com> Date: Sun Dec 1
6 21:01:58 2018 +0800  版本2.0  commit 0367a6a3bd901309e282dc630
e6562b7f270e39b Author: wcy <[email protected]163.com> Date: Sun Dec 16 2
0:55:10 2018 +0800  版本1.0

 

檢視操作記錄

[[email protected] test]# git reflog c94bcab [email protected]{0}: H
EAD^: updating HEAD 9f98991 [email protected]{1}: 9f9899114f: upd
ating HEAD c94bcab [email protected]{2}: HEAD^: updating HEAD 9f989
91 [email protected]{3}: commit: 版本3.0 c94bcab [email protected]{4}: commit: 版本2.
0 0367a6a [email protected]{5}: commit (initial): 版本1.0 [[email protected] tes
t]# git reset --hard 9f98991   回退到版本3.0 HEAD is now at 9f98991 版本3.0

 

可以看到上面版本3.0 最前面的就是之前版本序列號的前幾位,然後再跟進這個序列號進行其他操作

9f98991 [email protected]{3}: commit: 版本3.0    序列號即:9f98991

 

工作區、暫存區和版本庫

git是管理版本的修改

git commit時只是會把暫存區的修改提交到版本記錄中(也就是建立一個版本記錄)

撤銷修改

丟棄工作區的改動

在工作區修改 code.txt  使用git status檢視一下狀態

[[email protected] test]# echo "version 4.0" >> code.
txt  [[email protected] test]# 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: c
ode.txt # no changes added to commit (use "git add" and/or "git commit -a")

 

可以看到已修改檔案 code.txt

可以操作的選項是

1、git add 提交至暫存區

2、git checkout -- 刪除工作區的修改

[[email protected] test]# git checkout -- code.txt  [[email protected] test]# cat code.txt  version 1.0 version 2.0 version 3.0

 

刪除工作區的修改後 發現剛才新增的version 4.0 已經沒有了

已經新增至暫存區未commit

[[email protected] test]# echo "version 4.0" >> code.tx
t  [[email protected] test]# git add code.txt  [[email protected]
k1 test]# git status # On branch master # Changes to be 
committed: # (use "git reset HEAD <file>..." to unstage) # #   
 modified: code.txt #

 

把暫存區的檔案拉回工作區

[[email protected] test]# git reset HEAD code.txt  Unstaged changes after reset: M    code.txt

 

再把檔案從工作區中checkout出去 丟棄工作區的改動

[[email protected] test]# git status # On branch master # Changed 
but not updated: # (use "git add <file>..." to update what wi
ll be committed) # (use "git checkout -- <file>..." to discard cha
nges in working directory) # #    modified: code.txt # no changes adde
d to commit (use "git add" and/or "git commit -a") [[email protected] test]# 
git checkout -- code.txt  [[email protected] test]# git status # On branch mas
ter nothing to commit (working directory clean)

 

已經commit

[[email protected] test]# echo "version 4.0" >> code.txt  [[email protected] test]# cat code.
txt  version 1.0 version 2.0 version 3.0 version 4.0 [[email protected] test]# git add
 code.txt  [[email protected] test]# git commit -m "版本4.0" [master d61d2f2] 
版本4.0 1 files changed, 1 insertions(+), 0 deletions(-)

 

進行版本回退

[[email protected] test]# git log --pretty=oneline d61d
2f2cfdf2e2feb0dec63227315fcb7c004ff9 版本4.0 9f98991
14fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e7
2f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e
6562b7f270e39b 版本1.0 [[email protected] test]# git reset --hard 9
f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 HEAD is now at 9f98991 
版本3.0 [[email protected] test]# cat code.txt  version 1.0 version 2.0 version 3.0

 

對比檔案的不同

對比工作區和版本庫的某個檔案

[[email protected] test]# git diff HEAD^ -- code.txt  diff -
-git a/code.txt b/code.txt index d1f9bad..69afb9a 10064
4 --- a/code.txt +++ b/code.txt @@ -1,2 +1,3 @@ version 
1.0 version 2.0 +version 3.0

 

---為三個版本 HEAD^

+++為工作區的版本

結果為+version 3.0 也就是工作區比上個版本多這一行

對比兩個版本庫中檔案

[[email protected] test]# git diff HEAD~1 HEAD~2 -- code.t
xt  diff --git a/code.txt b/code.txt index d1f9bad..7
c8de03 100644 --- a/code.txt +++ b/code.txt @@ -1,2 +1

 @@ version 1.0 -version 2.0

 

由此可以看出---為前者 +++為後者

可看出 上個版本比上上個版本多一行 version 2.0

刪除檔案

新建一個測試檔案code2.txt

[[email protected] test]# echo "version 1.0" >> code2.txt 
[[email protected] test]# git status # On branch master # Un
tracked files: # (use "git add <file>..." to include in 
what will be committed) # #    code2.txt nothing added to c
ommit but untracked files present (use "git add" to track) [r
[email protected] test]# git add code2.txt  [[email protected] test]# git co
mmit -m "new code" [master e39c2ef] new code 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 code2.txt

 

rm直接刪除工作區檔案

[[email protected] test]# ls code2.txt code.txt [[email protected] test]# rm code2.txt  rm:是否刪除普通檔案 "code2.txt"?y [[email protected] test]# 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 working directory) # #    deleted: code2.txt # no changes added to commit (use "git add" and/or "git commit -a")

刪除後可以通過git checkout -- 撤回刪除

[[email protected] test]# git checkout -- code2.txt [[email protected] test]# ls code2.txt code.txt

git rm刪除暫存區檔案

[[email protected] test]# rm code2.txt  rm:是否刪除普通檔案 "code2.txt"?y [[email protected] test]# 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 working directory) # #    deleted: code2.txt # no changes added to commit (use "git add" and/or "git commit -a") [[email protected] test]# git rm code2.txt rm 'code2.txt' [[email protected] test]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: code2.txt #

刪除後可以通過

1、git reset HEAD <file>...

2、git checkout -- <file>...

進行恢復

[[email protected] test]# git reset HEAD code2.txt Unstaged changes after reset: M    code2.txt [[email protected] test]# git checkout -- code2.txt [[email protected] test]# ls code2.txt code.txt

git commit提交刪除

[[email protected] test]# rm code2.txt  rm:是否刪除普通檔案 "code2.txt"?y [[email protected] test]# git rm code2.txt rm 'code2.txt' [[email protected] test]# git commit -m "DEL code2.txt" [master c88df5f] DEL code2.txt 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 code2.txt

恢復只能版本回退

[[email protected] test]# git reflog c88df5f [email protected]{0}: commit: DEL code2.txt e39c2ef [email protected]{1}: commit: new code 9f98991 [email protected]{2}: 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5: updating HEAD d61d2f2 [email protected]{3}: commit: 版本4.0 9f98991 [email protected]{4}: 9f98991: updating HEAD c94bcab [email protected]{5}: HEAD^: updating HEAD 9f98991 [email protected]{6}: 9f9899114f: updating HEAD c94bcab [email protected]{7}: HEAD^: updating HEAD 9f98991 [email protected]{8}: commit: 版本3.0 c94bcab [email protected]{9}: commit: 版本2.0 0367a6a [email protected]{10}: commit (initial): 版本1.0 [[email protected] test]# git reset --hard e39c2ef HEAD is now at e39c2ef new code [[email protected] test]# ls code2.txt code.txt

分支

檢視當前的分支  預設只有一個 master 主分支

[[email protected] test]# git branch * master

建立一個分支

(建立並切換的過程很快 即建立一個指標 HEAD指向新的指標)

git checkout -b <分支名> 是建立並切換分支,是建立分支git branch <分支名>和切換分支 git checkout <分支名>的結合

[[email protected] test]# git checkout -b dev Switched to a new branch 'dev' [[email protected] test]# git branch * dev master

新分支會有之前主分支的所有內容

[[email protected] test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

在dev分支下編輯檔案

[[email protected] test]# echo "version 4.0" >> code.txt  [[email protected] test]# git add code.txt  [[email protected] test]# git commit -m "dev分支提交" [dev 80d1fe8] dev分支提交 1 files changed, 1 insertions(+), 0 deletions(-)

切換分支

dev分支工作完成,切換回master分支

[[email protected] test]# git checkout master Switched to branch 'master'

檢視當前所在分支

[[email protected] test]# git branch dev * master

檢視當前master分支下有沒有dev下的版本記錄 並沒有

[[email protected] test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

合併分支

(快速合併,直接把master指標指向dev的當前提交,所以速度也非常快)

[[email protected] test]# git merge dev Updating e39c2ef..80d1fe8 Fast-forward code.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)

再檢視一下分支記錄

[[email protected] test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

合併後刪除分支 

(直接刪除dev指標)

[[email protected] test]# git branch -d dev Deleted branch dev (was 80d1fe8). [[email protected] test]# git branch * master

解決衝突

現象:建立一個分支dev,在分支中修改code.txt檔案並提交

[[email protected] test]# git checkout -b dev Switched to a new branch 'dev' [[email protected] test]# echo "version 5.0" >> code.txt  [[email protected] test]# git add code.txt  [[email protected] test]# git commit -m "版本5.0" [dev b001913] 版本5.0 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] test]# git log --pretty=oneline b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

提交後切換到master分支,也修改code.txt檔案並提交

[[email protected] test]# git checkout master Switched to branch 'master' [[email protected] test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0 [[email protected] test]# echo "version 5.0 in master" >> code.txt [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "版本5.0master分支提交" [master 064fe6a] 版本5.0master分支提交 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] test]# git log --pretty=oneline 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

合併分支失敗

(自動合併code.txt  合併時起了一個衝突 合併失敗)

[[email protected] test]# git merge dev Auto-merging code.txt CONFLICT (content): Merge conflict in code.txt Automatic merge failed; fix conflicts and then commit the result.

找到衝突的檔案,可以通過git status

[[email protected] test]# git status # On branch master # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # #    both modified: code.txt    雙方修改 衝突 # no changes added to commit (use "git add" and/or "git commit -a")

檢視這個檔案,git已給標註是哪裡起了衝突

[[email protected] test]# cat code.txt  version 1.0 version 2.0 version 3.0 version 4.0 <<<<<<< HEAD version 5.0 in master ======= version 5.0 >>>>>>> dev

開始解決 (手動修改衝突的部分 並提交)在兩個分支上面都有新的提交,並且編輯的還是同一個檔案 才會有這樣的衝突  衝突解決後需要一個手動的提交

[[email protected] test]# cat code.txt  version 1.0 version 2.0 version 3.0 version 4.0 version 5.0 in master version 5.0 [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "解決衝突" [master b893d4c] 解決衝突 [[email protected] test]# git log --pretty=oneline b893d4cfa12894c24f255ce25590f47f9ca62e15 解決衝突 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

檢視分支衝突的情況

[[email protected] test]# git log --graph --pretty=oneline * b893d4cfa12894c24f255ce25590f47f9ca62e15 解決衝突 |\  | * b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 * | 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 |/  * 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 * e39c2efc2c787218c481a625893af153aed3e42b new code * 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 * c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 * 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0

解決衝突後再刪除dev分支

[[email protected] test]# git branch -d dev Deleted branch dev (was b001913). [[email protected] test]# git branch * master

 

稍候繼續更新。。。感謝關注