03.Git常用的命令
命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m "日志信息" 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
一.设置用户签名
1.1 linux常用的命令
#切换到某个目录
$ cd /d
#创建一个文件夹(为避免错误:文件名不可含中文)
$ mkdir learnGit
#看在当前哪个文件夹
$ pwd
#显示目录列表 用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录)
$ ls
#显示所有文件及目录 (ls内定将文件名或目录名称开头为"."的视为隐藏档,不会列出)
$ 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
全局范围的签名设置:
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
说明: 签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。 注意:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。
二. 查看基本设置
git config -l
git config --global -l
三.初始化本地库
3.1 语法
git init
3.2 案例实操
Jason@JASON MINGW64 ~/Desktop/GitTest
$ git init(初始化本地库)
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 结果查看
文件目录下多了一个.git的文件夹
四.查看本地库的状态
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(新增一个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 再次查看 ( 检测到未追踪的文件)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status(查看本地库状态)
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)
五.将工作区的文件添加到暂存区
5.1 提交工作区代码到暂存区语法
git add 文件路径或者.(所有文件)
5.2 实例实操
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 查看状态
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(这里的文字已经变为绿色,代表已经被追踪)
5.4 删除暂存区的文件(hello.txt)
删除之后再次查看状态,文字颜色由绿色变为红色
六.将暂存区文件提交到本地库
6.1 语法
git commit -m "日志信息" 文件名
6.2 实例操作
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
七.修改文件
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 查看文件状态 ( 检测到工作区有文件被修改)
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 将修改的文件再次添加暂存区
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 将修改的文件提交到本地库
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 查看版本详细信息
8.2 版本实操
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 版本号
8.2.2 实操
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!
8.2.3 另一种方法查看所在分支和指向版本
- ==在.git文件夹下有一个head文件,打开可以看到所在分支==
- ==在.git文件夹下有一个refs文件夹里有一个heads文件夹,里面有一个master文件,打开可以看到所在版本号==
Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示。