您当前的位置: 首页 >  git

Dongguo丶

暂无认证

  • 1浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Git常用命令

Dongguo丶 发布时间:2021-10-24 19:43:39 ,浏览量:1

常用命令名称作用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)基本语法
git config --global user.name 用户名
git config --global user.email 邮箱  
2)案例实操

全局范围的签名设置:

git config --global user.name dongguo4812
git config --global user.name 291320608@qq.com

设置完之后可以在C:\Users\Administrator.gitconfig中查看到

image-20211023205533374

说明: 签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看 到,以此确认本次提交是谁做的。 Git 首次安装必须设置一下用户签名,否则无法提交代码。 ※注意: 这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任 何关系 。

初始化本地库

git获得管理的权限

1) 基本语法
git init
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test
$ git init
Initialized empty Git repository in E:/test/.git/
3)结果查看

.git是一个隐藏文件,可以通过ll -a查看

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ ll -a
total 12
drwxr-xr-x 1 Administrator 197121 0 Oct 23 20:57 ./
drwxr-xr-x 1 Administrator 197121 0 Oct 23 20:57 ../
drwxr-xr-x 1 Administrator 197121 0 Oct 23 20:57 .git/

也可以直接在文件夹中查看到

image-20211023210124175

如果查看不到,选择显示隐藏的文件即可。

image-20211023210511407

查看本地库状态 1) 基本语法
git status  
2)案例实操 首次查看(工作区没有任何文件)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master

No commits yet

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

表示没有要提交的。

新增文件(hello.txt)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ vim hello.txt

插入模式输入

hello git
hello git
hello git
hello git
hello git
hello git
hello git
hello git

:wq退出并保存

再次查看(检测到未追踪的文件)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.txt

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

此时已经检测到未被跟踪的文件hello.txt,hello.txt颜色为红色

添加暂存区 将工作区的文件添加到暂存区 1)基本语法
git add 文件名  
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (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

警告提示的是git转换了你的换行符,LF替代了CRLF

查看状态(检测到暂存区有新文件)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   hello.txt

hello.txt文件已经加入到暂存区,hello.txt颜色变为绿色

提示可以使用git rm --cached 删除暂存区中的hello.txt

提交本地库 将暂存区的文件提交到本地库 1)基本语法
git commit -m "日志信息" 文件名  
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git commit -m "add hello.txt" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) 0a5b7e2] add hello.txt
 1 file changed, 10 insertions(+)
 create mode 100644 hello.txt

0a5b7e2为精简版的版本号

1个文件改变,新增10行代码

查看状态(没有文件需要提交)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
nothing to commit, working tree clean
修改文件(hello.txt)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ vim hello.txt

第一行添加123456789

hello git123456789
hello git
hello git
hello git
hello git
hello git
hello git
hello git
查看状态(检测到工作区有文件被修改)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
将修改的文件再次添加暂存区
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (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
查看状态(工作区的修改添加到了暂存区)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   hello.txt
提交本地库(将暂存区的文件提交到本地库 )
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git commit -m "second commit hello.txt"
[master 611d18f] second commit hello.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
查看状态(工作区的修改提交到了本地库)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
nothing to commit, working tree clean
历史版本 查看历史版本 1) 基本语法

git reflog 查看版本信息 git log 查看版本详细信息

2)案例实操

git reflog

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git reflog
611d18f (HEAD -> master) HEAD@{0}: commit: second commit hello.txt
0a5b7e2 HEAD@{1}: commit (initial): add hello.txt

git log

$ git log
commit 611d18f70cd74c6bd9bc83bbdbe0437fba4ce159 (HEAD -> master)
Author: dongguo4812 
Date:   Sat Oct 23 21:45:41 2021 +0800

    second commit hello.txt

commit 0a5b7e2013ff508c9d0af325e233dfd317ad1561
Author: dongguo4812 
Date:   Sat Oct 23 21:22:10 2021 +0800

    add hello.txt

显示出两次提交的版本信息

611d18f

0a5b7e2

为精简版的版本号

611d18f70cd74c6bd9bc83bbdbe0437fba4ce159

0a5b7e2013ff508c9d0af325e233dfd317ad1561

为完整版的版本号

版本穿梭 1) 基本语法
git reset --hard 版本号  
2)案例实操

首先查看当前的历史记录, 可以看到当前是在 611d18f这个版本

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git reflog
611d18f (HEAD -> master) HEAD@{0}: commit: second commit hello.txt
0a5b7e2 HEAD@{1}: commit (initial): add hello.txt

切换到 0a5b7e2版本,也就是我们第一次提交的版本

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git reset --hard 0a5b7e2
HEAD is now at 0a5b7e2 add hello.txt

切换完毕之后再查看历史记录,当前成功切换到了 0a5b7e2版本

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git reflog
0a5b7e2 (HEAD -> master) HEAD@{0}: reset: moving to 0a5b7e2
611d18f HEAD@{1}: commit: second commit hello.txt
0a5b7e2 (HEAD -> master) HEAD@{2}: commit (initial): add hello.txt

然后查看文件 hello.txt,发现文件内容已经变化

Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ cat hello.txt
hello git
hello git
hello git
hello git
hello git
hello git
hello git
hello git

Git 切换版本, 底层其实是移动的 HEAD 指针,具体原理如下图所示。

image-20211023220932081

image-20211023221010685

关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.3789s