1. 程式人生 > >Linux-學習筆記-04

Linux-學習筆記-04

一、上堂回顧

1.vim編輯器

​ 三種工作模式之間的切換

​ vim ---->a i --->esc,按下:,如果檔案有改動則使用wq,如果沒有改動,則使用q,則新增!強制退出

2.使用者管理

​ 注意:必須使用管理員的身份執行【root,普通使用者使用sudo】

​ useradd -m

​ userdel

​ usermod

​ passwd

​ groupadd

​ groupdel【注意:只能刪除空的使用者組】

​ chmod 【字母法和數字法】

3、系統管理

​ kill

4.shell

​ /bin/bash

​ /bin/sh

二、shell程式設計

1.shell運算子

1.1基本運算子【算術,關係,邏輯】

expr:是一款表示式計算工具,能完成表示式的求值操作

程式碼演示:

#!/bin/bash
​
val="2+2"
echo ${val}
​
val="expr 2+2"
echo ${val}
​
#expr使用功能,則需要結合反引號使用
val=`expr 2 + 2`
echo ${val}
​
​
#算術運算子
a=10
b=20
val=`expr $a + $b`
echo ${val}
​
#*必須使用\進行轉義
val=`expr $a \* $b`
echo ${val}
​
​
#關係運算符
#if語句後面的條件需要用[]括起來,但是表示式的前後都需要有空格
#if-then-else-fi
if [ $a -eq $b ]
then
  echo "yes"
else
  echo "no"
fi
​
#邏輯運算子
#邏輯運算子一般結合關係運算符使用
if [ 1 -lt 3 ] && [ 2 -gt 3 ]
then
  echo "yes"
fi

1.2檔案測試運算子

程式碼演示:

#!/bin/bash
path="/home/yangyang/Desktop/shell/math.sh"
​
if [ -d $path ]
then
  echo "yes"
else
  echo "no"
fi
~  

2.echo、printf、test命令

2.1echo

用於字串或者變數的輸出

程式碼演示:

#!/bin/bash
​
echo "this is a text"
​
echo this is a text
​
echo 'this is a text'
​
#echo `this is a text`
​
#顯示轉義字元
echo "\"this is a text\""
​
#顯示變數
name="zhangsan"
echo "his name is $name"
​
#-e表示開啟轉義,\n表示換行,\c表示不換行,echo預設可以換行
echo -e  "ok \n"
​
echo -e "ok \c"
echo "hello"
​
#執行命令
echo `date`

2.2printf

注意:預設printf不會像echo自動換行,如果需要換行需要手動新增\n

printf比echo功能更加強大

程式碼演示:

#!/bin/bash
​
​
printf "hello"
​
​
#對資料進行格式化
printf "%-10s %-6s %-4s\n" 姓名  性別 體重
printf "%-10s %-6s %-4.2f\n" 小華  女   48.3253
printf "%-10s %-6s %-4f\n" jack  男   60.435
printf "%-10s %-6s %-4f\n" tom   男   100.24
printf "%-3s %-6s %-4f\n" bobbb  男    55.00

總結:

a.%s %d %f %c【character】都是佔位符

b.%-10s,指的是一個長度最大為10 的字串,-表示左對齊,沒有-則表示右對齊,整體表示任何字元都會被顯示在10個字元寬的範圍內,如果不足則自動使用空格填充,如果超出則全部顯示

c.%-4.2f:指的是格式化浮點型資料,其中,.2f表示保留小數點後2位

2.3test

用於檢查某個條件是否成立,可以進行數值,字元和檔案三個方面的檢測

程式碼演示:

#!/bin/bash
​
#數值檢測
num1=100
num2=200
if [ $num1 -eq $num2 ]
then
  echo "yes"
else
  echo "no"
fi
​
if test $[num1] -eq $[num2]
then
  echo "yes"
else
  echo "no"
fi
~    

3.shell流程控制語句

3.1分支語句

程式碼演示:

#!/bin/bash
​
​
#1.單分支
if [ 1 -gt 3 ]
then
  echo "yes"
fi
​
#2.雙分支
if [ 1 -gt 3 ]
then
  echo "yes"
else
  echo "no"
fi
​
​
#3.多分支
a=10
b=20
if [ $a -eq $b ]
then
  echo "相等"
elif [ $a -gt $b ]
then
  echo "大於"
elif [ $a -lt $b ]
then
  echo "小於"
else
  echo "不符合條件"
fi
~  

3.2迴圈語句

程式碼演示:

!/bin/bash
​
#1.需求:輸出給定的所有的數字
for num in 1 2 3 4 5
do
  echo "the value is $num"
done
​
#2.需求:輸出字串中的字元
s="hello"
for str in s
do
  echo $str
done
​
#3.需求:輸出陣列中的所有的元素
arr=(22 33 44)
for x in ${arr[*]}
do
  echo $x
done
​
​
#需求:輸出數字1~5
n=1
while [ $n -le 5 ]
do
  echo $n
  #n=$[$n+1]
  let "n++"
done
​
#需求:求1~10之間所有整數的和
i=1
sum=0
while [ $i -le 10 ]
do
  sum=$[$sum+$i]
  let "i++"
done
echo $sum
​
​
#until
j=1
until [ $j -gt 10 ]
do
  echo $j
  ((j++))
done                      

4.shell函式

程式碼演示:

#!/bin/bash
​
#1.午參悟返回值
#定義函式
check(){
  echo "hello"
}
#呼叫函式
check
​
#2.有返回值
func1(){
​
  echo "請輸入第一個數:"
  read num1
  echo "請輸入第二個數:"
  read num2
  #echo "兩個數的和是:"
  return $(($num1+$num2))
}
​
func1
echo "兩個輸入的數的和是:$?"
​
#3.有參有返回值
func2(){
  echo $1
  echo $2
  return 123
}
​
func2 11 22
echo $?

三、git的使用

1.git簡介

git svn

如果你用Word寫過畢業論文,那你一定有這樣的經歷:

想刪除一個段落,又怕將來想恢復找不回來怎麼辦?有辦法,先把當前檔案“另存為……”一個新的Word檔案,再接著改,改到一定程度,再“另存為……”一個新檔案,這樣一直改下去,最後你的Word文件變成了這樣:

過了一週,你想找回被刪除的文字,但是已經記不清刪除前儲存在哪個檔案裡了,只好一個一個檔案去找,真麻煩。

看著一堆亂七八糟的檔案,想保留最新的一個,然後把其他的刪掉,又怕哪天會用上,還不敢刪,真鬱悶。

更要命的是,有些部分需要你的財務同事幫助填寫,於是你把檔案Copy到U盤裡給她(也可能通過Email傳送一份給她),然後,你繼續修改Word檔案。一天後,同事再把Word檔案傳給你,此時,你必須想想,發給她之後到你收到她的檔案期間,你作了哪些改動,得把你的改動和她的部分合並,真困難。

於是你想,如果有一個軟體,不但能自動幫我記錄每次檔案的改動,還可以讓同事協作編輯,這樣就不用自己管理一堆類似的檔案了,也不需要把檔案傳來傳去。如果想檢視某次改動,只需要在軟體裡瞄一眼就可以,豈不是很方便?

這個軟體用起來就應該像這個樣子,能記錄每次檔案的改動:

版本 檔名 使用者 說明 日期
1 service.doc 張三 刪除了軟體服務條款5 7/12 10:38
2 service.doc 張三 增加了License人數限制 7/12 18:09
3 service.doc 李四 財務部門調整了合同金額 7/13 9:51
4 service.doc 張三 延長了免費升級週期 7/14 15:17

這樣,你就結束了手動管理多個“版本”的史前時代,進入到版本控制的20世紀。

Git是目前世界上最先進的分散式版本控制系統(沒有之一)。

Git有什麼特點?簡單來說就是:高階大氣上檔次!

1.2git的由來

​ 很多人都知道,Linus在1991年建立了開源的Linux,從此,Linux系統不斷髮展,已經成為最大的伺服器系統軟體了。

​ Linus雖然建立了Linux,但Linux的壯大是靠全世界熱心的志願者參與的,這麼多人在世界各地為Linux編寫程式碼,那Linux的程式碼是如何管理的呢?

​ 事實是,在2002年以前,世界各地的志願者把原始碼檔案通過diff的方式發給Linus,然後由Linus本人通過手工方式合併程式碼!

​ 你也許會想,為什麼Linus不把Linux程式碼放到版本控制系統裡呢?不是有CVS、SVN這些免費的版本控制系統嗎?因為Linus堅定地反對CVS和SVN,這些集中式的版本控制系統不但速度慢,而且必須聯網才能使用。有一些商用的版本控制系統,雖然比CVS、SVN好用,但那是付費的,和Linux的開源精神不符。

​ 不過,到了2002年,Linux系統已經發展了十年了,程式碼庫之大讓Linus很難繼續通過手工方式管理了,社群的弟兄們也對這種方式表達了強烈不滿,於是Linus選擇了一個商業的版本控制系統BitKeeper,BitKeeper的東家BitMover公司出於人道主義精神,授權Linux社群免費使用這個版本控制系統。

​ 安定團結的大好局面在2005年就被打破了,原因是Linux社群牛人聚集,不免沾染了一些梁山好漢的江湖習氣。開發Samba的Andrew試圖破解BitKeeper的協議(這麼幹的其實也不只他一個),被BitMover公司發現了(監控工作做得不錯!),於是BitMover公司怒了,要收回Linux社群的免費使用權。

​ Linus可以向BitMover公司道個歉,保證以後嚴格管教弟兄們,嗯,這是不可能的。實際情況是這樣的:

​ Linus花了兩週時間自己用C寫了一個分散式版本控制系統,這就是Git!一個月之內,Linux系統的原始碼已經由Git管理了!牛是怎麼定義的呢?大家可以體會一下。

​ Git迅速成為最流行的分散式版本控制系統,尤其是2008年,GitHub網站上線了,它為開源專案免費提供Git儲存,無數開源專案開始遷移至GitHub,包括jQuery,PHP,Ruby等等。

歷史就是這麼偶然,如果不是當年BitMover公司威脅Linux社群,可能現在我們就沒有免費而超級好用的Git了

1.3集中式和分散式

​ svn:集中式的版本控制系統

​ git:分散式的版本控制系統

集中式:版本是存放在中央伺服器上的,【缺點:必須聯網才能工作】

分散式:沒有中央伺服器,每個人的電腦上都有一個完整的版本庫【專案】【優點:不需要聯網就可以工作,如果其中的一個人改動了檔案,只需要將改動的部分推送給對方】,git中有分支管理

2.安裝git

sudo apt-get install git

演示命令:
[email protected]:~$ sudo apt-get install git
[sudo] yangyang 的密碼: 
正在讀取軟體包列表... 完成
正在分析軟體包的依賴關係樹       
正在讀取狀態資訊... 完成       
git 已經是最新版 (1:2.16.2-0ppa1~ubuntu16.04.1)。
升級了 0 個軟體包,新安裝了 0 個軟體包,要解除安裝 0 個軟體包,有 157 個軟體包未被升級。

3.建立版本庫

3.1什麼是版本庫

版本庫也被叫做倉庫,repository,初期理解為一個目錄,這個目錄裡面的所有的內容都可以被git管理,每個檔案的修改,刪除,增加檔案或者刪除檔案,git都可以追蹤,以便任何時刻都可以追蹤改動,或者在將來某個時刻可以還原

3.2建立版本庫

步驟:建立一個普通目錄【mkdir 目錄名】

​ 初始化倉庫【git init】

注意:在目錄下將出現一個.git的隱藏目錄,標記當前的目錄就是一個倉庫,可以進行追蹤倉庫中任何的變化【可以跟蹤管理版本庫】

演示命令:
[email protected]:~$ cd Desktop/
[email protected]:~/Desktop$ mkdir python1804
[email protected]:~/Desktop$ ls
python1804  shell  text.sh
[email protected]:~/Desktop$ cd python1804/
[email protected]:~/Desktop/python1804$ pwd
/home/yangyang/Desktop/python1804
[email protected]:~/Desktop/python1804$ git init
已初始化空的 Git 倉庫於 /home/yangyang/Desktop/python1804/.git/
[email protected]:~/Desktop/python1804$ ls -a
.  ..  .git

3.3把檔案新增到倉庫

明確一點:任何的版本控制系統,其實都只能追蹤文字檔案的改動,實質上追蹤的是改動【刪除一個檔案屬於一個改動,增加一個檔案也屬於一個改動,向檔案中新增內容也屬於 一個改動。。。。。】

演示命令:
[email protected]:~/Desktop/python1804$ touch text.txt
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "create a new file text.txt and init"
​
*** 請告訴我你是誰。
​
執行
​
  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"
​
來設定您賬號的預設身份標識。
如果僅在本倉庫設定身份標識,則省略 --global 引數。
fatal: 無法自動探測郵件地址(得到 '[email protected](none)')
[email protected]:~/Desktop/python1804$ git config --global user.email "[email protected]"
[email protected]:~/Desktop/python1804$ git config --global user.name "yangyang-git"
[email protected]:~/Desktop/python1804$ git commit -m "create a new file text.txt and init"
[master(根提交) b80fd54] create a new file text.txt and init
 1 file changed, 1 insertion(+)
 create mode 100644 text.txt
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git add text.txt '
> ^C
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "add abc"
[master 19a9faa] add abc
 1 file changed, 1 insertion(+)
[email protected]:~/Desktop/python1804$ touch check.txt
[email protected]:~/Desktop/python1804$ vim check.txt 
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git add check.txt 
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "modify check and text"
[master 6846a1d] modify check and text
 2 files changed, 2 insertions(+)
 create mode 100644 check.txt
git add filename  :新增改動
git commit -m "xxxx" :提交到git
​
每一次add可以新增一次改動,執行add多次則表示增加多次改動,commit只需要執行一次,一次性將所有的改動全部提交給git
-m;表示每一次提交的提交日誌,可以輸入任意內容,當然最好是有意義的,好處:可以從歷史版本中找到需要的某個版本

4.時光穿梭機【覆水可收】

可以回退版本

1>git status 檢視倉庫當前的狀態【時刻掌握倉庫的狀態,當沒有任何檔案需要提交時,是乾淨的工作區】

2>git diff 檢視具體修改的內容

如果對本地的某個檔案做了修改,但是還沒有add,則通過git diff就可以檢視本地檔案和倉庫之間的差異,當修改被add之後,再檢視差異則沒有任何輸出

[email protected]:~/Desktop/python1804$ vim check.txt 
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    修改:     check.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git add check.txt 
[email protected]:~/Desktop/python1804$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
​
    修改:     check.txt
​
[email protected]:~/Desktop/python1804$ git commit -m "4364"
[master 969cf64] 4364
 1 file changed, 1 insertion(+)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區
[email protected]:~/Desktop/python1804$ git diff check.txt
[email protected]ng-virMachine:~/Desktop/python1804$ vim check.txt 
[email protected]:~/Desktop/python1804$ git diff check.txt
diff --git a/check.txt b/check.txt
index 316b0ad..bf65706 100644
--- a/check.txt
+++ b/check.txt
@@ -1,2 +1,3 @@
 46337
 date
+2018.9.7
[email protected]:~/Desktop/python1804$ git add check.txt 
[email protected]:~/Desktop/python1804$ git diff check.txt
[email protected]:~/Desktop/python1804$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
​
    修改:     check.txt
​
[email protected]:~/Desktop/python1804$ git commit -m "fhfh"
[master 0da4f31] fhfh
 1 file changed, 1 insertion(+)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區

4.1版本回退

不斷對檔案進行修改,然後不斷提交到版本庫裡

當一個檔案修改到某個程度的時候,就可以儲存一個快照,這個快照在git被稱為commit

git log 檢視git的工作日誌【提交記錄】

需求:回退到上一個版本

問題:要回退版本,git必須要知道回到哪個版本

解決辦法:在git中,用HEAD表示當前版本,上一個版本使用HEAD^,上上一個版本則使用HEAD^^,如果要回退到的版本比較久遠,則使用commit id

命令:git reset --hard HEAD^ 回退到最近的一個版本【--hard能夠使得版本回退到最近的一個版本】

工作原理:git之所以能夠進行版本回退,因為在git的內部有一個指向當前版本的HEAD指標,當你回退版本的時候,git將HEAD指標的指向發生了一個改變,指向了最近的一個版本

演示命令:
[email protected]:~/Desktop/python1804$ git log --pretty=oneline    #檢視版本提交記錄
0da4f317b9f97469e3a493107228a09b3c97581c (HEAD -> master) fhfh
969cf64f99c9584d0206d0df70d5a157dff69a4c 4364
6846a1dbf48675a96c28f93c52032c604b50de6f modify check and text
19a9faa7f0c2a9bf861267562d6d69afe077d6d4 add abc
b80fd54fac6981ad35909ca08fbb0b8e319d2ce7 create a new file text.txt and init
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區
[email protected]:~/Desktop/python1804$ cat check.txt 
46337
date
2018.9.7
[email protected]:~/Desktop/python1804$ cat text.txt 
hello
abc
46343
[email protected]:~/Desktop/python1804$ git reset --hard HEAD^     #回退到最近的一個版本
HEAD 現在位於 969cf64 4364
[email protected]:~/Desktop/python1804$ car check.txt 
程式“car”尚未安裝。 如需執行 'car',請要求管理員安裝 'ucommon-utils' 軟體包
[email protected]:~/Desktop/python1804$ cat check.txt 
46337
date
[email protected]:~/Desktop/python1804$ git log --pretty=oneline
969cf64f99c9584d0206d0df70d5a157dff69a4c (HEAD -> master) 4364
6846a1dbf48675a96c28f93c52032c604b50de6f modify check and text
19a9faa7f0c2a9bf861267562d6d69afe077d6d4 add abc
b80fd54fac6981ad35909ca08fbb0b8e319d2ce7 create a new file text.txt and init
[email protected]:~/Desktop/python1804$ git reset --hard 0da4f317    #通過commit id回到某個指定的版本,commit id是唯一的
HEAD 現在位於 0da4f31 fhfh
[email protected]:~/Desktop/python1804$ cat check.txt 
46337
date
2018.9.7
[email protected]:~/Desktop/python1804$ git reflog  #檢視git的提交記錄,可以獲取過去和未來的所有的版本
0da4f31 (HEAD -> master) [email protected]{0}: reset: moving to 0da4f317
969cf64 [email protected]{1}: reset: moving to HEAD^
0da4f31 (HEAD -> master) [email protected]{2}: commit: fhfh
969cf64 [email protected]{3}: commit: 4364
6846a1d [email protected]{4}: commit: modify check and text
19a9faa [email protected]{5}: commit: add abc
b80fd54 [email protected]{6}: commit (initial): create a new file text.txt and init

總結:

a.HEAD預設指向的版本是當前版本,git允許我們在不同的版本之間進行穿梭,使用命令git reset --hard commit_id

b.穿梭前,使用git log可以檢視提交的歷史,以便於確定需要穿梭到哪個版本

c.要重返未來,用git reflog檢視命令歷史,以便於確定需要回到未來的哪個版本

4.2工作區和暫存區

工作區:working Directory,就是在電腦上能看見的目錄,舉例:python1804

版本庫:Repository,工作區內有一個隱藏目錄.git,就是git的版本庫

暫存區:是一個抽象的概念

將檔案提交到版本庫分為兩步:

git add xxx:將檔案的改動新增到暫存區

git commit -m "xxx";將暫存區中所有的改動提交到版本庫中的當前分支

建立git版本庫時,git會自動為我們建立唯一一個分支master分支,所以,每次將改動提交給了master分支【主分支】

演示命令:
[email protected]:~/Desktop/python1804$ vim text.txt  #在工作區中工作
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    修改:     text.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git add text.txt  #新增到暫存區
[email protected]:~/Desktop/python1804$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
​
    修改:     text.txt
​
[email protected]:~/Desktop/python1804$ git commit -m "add zhangsan"   #提交到版本庫的master分支
[master 1d235bf] add zhangsan
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區

4.3管理修改

在一個檔案中新增了一行,這時一個修改

刪除了一行內容,這是一個修改

更改了某些字元,這是一個修改

演示命令:
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "on my god"
[master c799364] on my god
 1 file changed, 1 insertion(+)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    修改:     text.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git diff HEAD -- text.txt
diff --git a/text.txt b/text.txt
index a12c65b..d02f0f4 100644
--- a/text.txt
+++ b/text.txt
@@ -1,4 +1,4 @@
 hello zhangsan
-abc
+def
 46343
 on my god
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "def"
[master 4970717] def
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區
[email protected]:~/Desktop/python1804$ git diff HEAD -- text.txt

4.4撤銷修改

第一種情況:修改了檔案,但是還沒有新增到暫存區

git checkout -- 被修改的檔案

演示命令:
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    修改:     text.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git checkout -- text.txt
[email protected]:~/Desktop/python1804$ cat text.txt 
hello zhangsan
def
46343
on my god

第二種:修改了檔案,新增到了暫存區,但是還沒有提交到版本庫

演示命令:
[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
​
    修改:     text.txt
​
[email protected]:~/Desktop/python1804$ git reset HEAD text.txt
重置後取消暫存的變更:
M   text.txt
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    修改:     text.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git checkout -- text.txt
[email protected]:~/Desktop/python1804$ cat text.txt 
hello zhangsan
def
46343

第三種:修改了檔案,直接提交到了版本庫

[email protected]:~/Desktop/python1804$ vim text.txt 
[email protected]:~/Desktop/python1804$ git add text.txt 
[email protected]:~/Desktop/python1804$ git commit -m "fhdf"
[master 1cd49a3] fhdf
 1 file changed, 1 insertion(+)
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區
[email protected]:~/Desktop/python1804$ git reset --hard HEAD^
HEAD 現在位於 4970717 def
[email protected]:~/Desktop/python1804$ cat text.txt 
hello zhangsan
def
46343
on my god

總結:

a.如果修改了工作區中的某個檔案,向直接丟棄工作區的改動,用命令git checkout -- file

b.如果修改了工作區中的某個檔案,還新增到了暫存區,向丟棄修改,分為兩步:第一步:使用命令git reset HEAD file撤銷了add,然後命令git checkout -- file撤銷工作區的修改

c.如果修改了工作區中的某個檔案,還提交到了版本庫,只需要回退版本即可

4.5刪除檔案

步驟:

第一步:刪除工作區中的檔案【rm file】

第二步:刪除版本庫中的檔案【git rm file】

演示命令:
[email protected]:~/Desktop/python1804$ rm check.txt 
[email protected]:~/Desktop/python1804$ ls
text.txt
[email protected]:~/Desktop/python1804$ git status
位於分支 master
尚未暫存以備提交的變更:
  (使用 "git add/rm <檔案>..." 更新要提交的內容)
  (使用 "git checkout -- <檔案>..." 丟棄工作區的改動)
​
    刪除:     check.txt
​
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[email protected]:~/Desktop/python1804$ git rm check.txt
rm 'check.txt'
[email protected]:~/Desktop/python1804$ git status
位於分支 master
要提交的變更:
  (使用 "git reset HEAD <檔案>..." 以取消暫存)
​
    刪除:     check.txt
​
[email protected]ang-virMachine:~/Desktop/python1804$ git commit -m "delete check.txt"
[master 91114f4] delete check.txt
 1 file changed, 3 deletions(-)
 delete mode 100644 check.txt
[email protected]:~/Desktop/python1804$ git status
位於分支 master
無檔案要提交,乾淨的工作區