03.Git常用的命令

03.Git常用的命令

命令名稱 作用
git config --global user.name 用戶名 設(shè)置用戶簽名
git config --global user.email 郵箱 設(shè)置用戶簽名
git init 初始化本地庫(kù)
git status 查看本地庫(kù)狀態(tài)
git add 文件名 添加到暫存區(qū)
git commit -m "日志信息" 文件名 提交到本地庫(kù)
git reflog 查看歷史記錄
git reset --hard 版本號(hào) 版本穿梭
image-20210309144018337

一.設(shè)置用戶簽名

1.1 linux常用的命令

#切換到某個(gè)目錄
$ cd /d
#創(chuàng)建一個(gè)文件夾(為避免錯(cuò)誤:文件名不可含中文)
$ mkdir learnGit
#看在當(dāng)前哪個(gè)文件夾
$ pwd
#顯示目錄列表 用于顯示指定工作目錄下之內(nèi)容(列出目前工作目錄所含之文件及子目錄)
$ ls
#顯示所有文件及目錄 (ls內(nèi)定將文件名或目錄名稱開頭為"."的視為隱藏檔,不會(huì)列出)
$ ls -a
#顯示文件 
$ ll
#清理屏幕
ctrl+l

1.2 基本語法

git config --global user.name 用戶名
git config --global user.email 郵箱

==tips:配置好用戶簽名以后,可以在c盤的users文件夾->用戶名->.gitconfig文件,打開.gitconfig文件,然后就可以看到配置==

[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
[user]
    email = wei1396981310@163.com
    name = jason
[core]
    autocrlf = false

全局范圍的簽名設(shè)置:

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git config --global user.name Layne
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git config --global user.email Layne@atguigu.com
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat ~/.gitconfig
[user]
name = Layne
email = Layne@atguigu.com

說明:
簽名的作用是區(qū)分不同操作者身份。用戶的簽名信息在每一個(gè)版本的提交信息中能夠看到,以此確認(rèn)本次提交是誰做的。Git 首次安裝必須設(shè)置一下用戶簽名,否則無法提交代碼。
注意:這里設(shè)置用戶簽名和將來登錄 GitHub(或其他代碼托管中心)的賬號(hào)沒有任何關(guān)系。

二. 查看基本設(shè)置

git config -l
git config --global -l
image-20210901214924698
image-20210901215716500

三.初始化本地庫(kù)

3.1 語法

git init

3.2 案例實(shí)操

Jason@JASON MINGW64 ~/Desktop/GitTest
$ git init(初始化本地庫(kù))
Initialized empty Git repository in C:/Users/13969/Desktop/GitTest/.git/

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ ll(顯示文件)
total 0

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ ls(顯示文件夾)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ ls -a
./  ../  .git/(顯示.git文件夾)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)

3.3 結(jié)果查看

文件目錄下多了一個(gè).git的文件夾

image-20210902200205445

四.查看本地庫(kù)的狀態(tài)

4.1 語法

git status

4.2 首次查看

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

4.3 新增文件(hello.txt)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ vim hello.txt(新增一個(gè)hello.txt文件)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt(查看hello.txt文件)
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!

4.4 再次查看 ( 檢測(cè)到未追蹤的文件)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status(查看本地庫(kù)狀態(tài))
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt(這里的文字是紅色,代表違背追蹤)

nothing added to commit but untracked files present (use "git add" to track)

五.將工作區(qū)的文件添加到暫存區(qū)

5.1 提交工作區(qū)代碼到暫存區(qū)語法

git add 文件路徑或者.(所有文件)

5.2 實(shí)例實(shí)操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.

5.3 查看狀態(tài)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git add hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt(這里的文字已經(jīng)變?yōu)榫G色,代表已經(jīng)被追蹤)

5.4 刪除暫存區(qū)的文件(hello.txt)

image-20210902203113089

刪除之后再次查看狀態(tài),文字顏色由綠色變?yōu)榧t色

image-20210902203550135

六.將暫存區(qū)文件提交到本地庫(kù)

6.1 語法

git commit -m "日志信息" 文件名

6.2 實(shí)例操作

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git commit -m "first commit"
[master (root-commit) 162913e] first commit
 1 file changed, 8 insertions(+)
 create mode 100644 hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean
image-20210902204906366

七.修改文件

7.1修改文件

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ vim hello.txt

hello world!hello Git!1111
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!

7.2 查看文件狀態(tài) ( 檢測(cè)到工作區(qū)有文件被修改)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

7.3 將修改的文件再次添加暫存區(qū)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git add hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.txt

7.4 將修改的文件提交到本地庫(kù)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git commit -m "second commit"
[master a2b8091] second commit
 1 file changed, 1 insertion(+), 1 deletion(-)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

八.歷史版本

8.1 查看版本

8.1.1 歷史版本語法

git reflog 查看版本信息
git log 查看版本詳細(xì)信息

8.2 版本實(shí)操
image-20210902211830590

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git reflog
761404b (HEAD -> master) HEAD@{0}: commit: third commit
a2b8091 HEAD@{1}: commit: second commit
162913e HEAD@{2}: commit (initial): first commit

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git log
commit 761404bf587d7f607041ae1bc3c06591cb00227c (HEAD -> master)
Author: jason <wei1396981310@163.com>
Date:   Thu Sep 2 21:16:22 2021 +0800

    third commit

commit a2b8091c2d89cef9b1a9e8be54f512d8c4e36242
Author: jason <wei1396981310@163.com>
Date:   Thu Sep 2 21:10:37 2021 +0800

    second commit

commit 162913e8b542a1af38a921b53fa639145c3e0387
Author: jason <wei1396981310@163.com>
Date:   Thu Sep 2 20:39:19 2021 +0800

    first commit

8.2 ==版本穿梭==

8.2.1 語法

git reset --hard 版本號(hào)

8.2.2 實(shí)操

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git reset --hard a2b8091(返回第二次提交)
HEAD is now at a2b8091 second commit

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git reflog
a2b8091 (HEAD -> master) HEAD@{0}: reset: moving to a2b8091
761404b HEAD@{1}: commit: third commit
a2b8091 (HEAD -> master) HEAD@{2}: commit: second commit
162913e HEAD@{3}: commit (initial): first commit

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt
hello world!hello Git!1111
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!

image-20210902214106119

8.2.3 另一種方法查看所在分支和指向版本

  • ==在.git文件夾下有一個(gè)head文件,打開可以看到所在分支==
image-20210902214653884
image-20210902214709379
  • ==在.git文件夾下有一個(gè)refs文件夾里有一個(gè)heads文件夾,里面有一個(gè)master文件,打開可以看到所在版本號(hào)==
image-20210902214449721
image-20210902214505580

Git 切換版本,底層其實(shí)是移動(dòng)的 HEAD 指針,具體原理如下圖所示。

image-20210902215207681
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。