01_初入Git之常用命令

Git常用命令

在文件夹内右键选择Git Bash Here,可以直接打开Git客户端
代码快捷方式:写出前几个字母然后敲击两次Tab键即可自动补齐代码

命令名称 作用
git config --global user.name 用户名 设置用户签名(新Git仓库必须要执行)
git config --global user.email 用户邮箱 设置用户邮箱(不会验证)
git init 初始化本地库
git status 查看本地库状态
git add 文件名 添加到暂存区
git commit -m "日志信息" 文件名 提交到本地库
git reflog 查看历史记录
git reset -hard 版本 版本穿梭

1)git init(初始化)

$ git init
Initialized empty Git repository in D:/GitSpace/git-demo1/.git/

2)ll(查看)/ll -a(查看隐藏文件)

王杰@wangjie MINGW64 /d/GitSpace/git-demo1 (master)
$ ll
total 0
王杰@wangjie MINGW64 /d/GitSpace/git-demo1 (master)
$ ll -a
total 8
drwxr-xr-x 1 王杰 197121 0 Mar  9 14:57 ./
drwxr-xr-x 1 王杰 197121 0 Mar  9 14:49 ../
drwxr-xr-x 1 王杰 197121 0 Mar  9 14:50 .git/

3)git status(查看状态)

在工作区内的状态颜色为红色,在暂存区内的颜色为绿色

$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
新增hello文件
  1. vim hello.txt
  2. Alt+i进入编辑模式,输入hello
  3. ESC退出编辑模式,yy+p复制粘贴
  4. :wq保存退出
  5. cat hello.txt查看文件内容
  6. tail -r 1 hello.txt 查看最后一行内容
  7. 再次查看:
$ 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)

4)git add(添加暂存区)

$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it 转化换行符
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
从暂存区中删除(工作区还存在)
$ git rm --cached hello.txt
rm 'hello.txt'

5)git commit -m "版本" 文件名(提交本地库)

$ git commit -m "first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 3311fae] first commit
 1 file changed, 10 insertions(+)
 create mode 100644 hello.txt

6)查看版本信息

6.1)git reflog 查看版本精简信息-1
$ git reflog
3311fae (HEAD -> master) HEAD@{0}: commit (initial): first commit
6.2)git log 查看版本详细信息-2
$ git log
commit 3311faef637b68cd032553f60b97dfdab94cac6f (HEAD -> master)
Author: WangJie <WangJie@WangJie.com>
Date:   Thu Mar 9 15:30:17 2023 +0800

    first commit

7)修改文件

7.1)进入文件进行修改

$ vim hello.txt

7.2)修改后使用 git status 查看
$ 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

no changes added to commit (use "git add" and/or "git commit -a")
7.3)提交到暂存区
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
7.4)提交到本地库
$ git commit -m "second commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master b88ce29] second commit
 1 file changed, 2 insertions(+), 2 deletions(-)
git的修改是按照行进行的,修改了一行显示为删除一行,新增一行
7.5)查看版本信息
$ git reflog
b88ce29 (HEAD -> master) HEAD@{0}: commit: second commit
指针指向最后修改的版本
3311fae HEAD@{1}: commit (initial): first commit

注意:在git工作区中只会有一个版本的文档,并不是在本地保存多个版本的文档

8)git reset--hard "版本号"(版本穿梭)

原理:修改head->master的指针指向版本,通过修改指针指向的位置,即可实现版本穿梭
每次进行更改版本都会留下记录

王杰@wangjie MINGW64 /d/GitSpace/git-demo1 (master)
$ git reset --hard b88ce29
HEAD is now at b88ce29 second commit
王杰@wangjie MINGW64 /d/GitSpace/git-demo1 (master)
$ git reset --hard 3311fae
HEAD is now at 3311fae first commit
王杰@wangjie MINGW64 /d/GitSpace/git-demo1 (master)
$ git reflog
3311fae (HEAD -> master) HEAD@{0}: reset: moving to 3311fae
b88ce29 HEAD@{1}: reset: moving to b88ce29
b88ce29 HEAD@{2}: commit: second commit
3311fae (HEAD -> master) HEAD@{3}: commit (initial): first commit
posted @ 2023-03-14 19:13  超重了  阅读(31)  评论(0编辑  收藏  举报