【Git】3. Git常用命令

3. Git常用命令

3.1 设置用户签名

  1. 基本语法:

    1. git config --global user.name 用户名
    2. git config --global user.email 邮箱
  2. 实例操作:

    lenovo@DESKTOP-55E400K MINGW64 ~/Desktop
    $ git config --global user.name qhj
    
    lenovo@DESKTOP-55E400K MINGW64 ~/Desktop
    $ git config --global user.mail 1029930034@qq.com
    
    lenovo@DESKTOP-55E400K MINGW64 ~/Desktop
    $ cat ~/.gitconfig
    [user]
            name = qhj
            mail = 1029930034@qq.com
    
  3. 说明:

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

3.2 初始化本地库

  1. 基本语法:git init首先进入项目目录,右键Git Bash,可以直接在Git进入该目录

  2. 实例操作:

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 //在项目目录下
    $ git init
    Initialized empty Git repository in E:/Git-space/project_1/.git/
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ ll
    total 0  //已隐藏
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ ll -a //查看隐藏文件
    total 4
    drwxr-xr-x 1 lenovo 197121 0 Jun 26 11:24 ./
    drwxr-xr-x 1 lenovo 197121 0 Jun 26 11:24 ../
    drwxr-xr-x 1 lenovo 197121 0 Jun 26 11:25 .git/
    
  3. 在window中查看:(project_1就是工作区

    在这里插入图片描述

3.3 查看本地库状态

  1. 基本语法:git status

  2. 实例操作:

    1. 首次查看(工作区没有任何文件)

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git status
      On branch master
      
      No commits yet //当前还没有提交过任何东西
      
      nothing to commit (create/copy files and use "git add" to track)
      
    2. 新增文件(hello.txt)

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ vim hello.txt
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ ll
      total 1
      -rw-r--r-- 1 lenovo 197121 110 Jun 26 11:33 hello.txt
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ cat hello.txt
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      
    3. 再次查看状态(检测到未被追踪的文件)

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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)
      

3.4 添加到暂存区

  1. 将工作区的文件添加到暂存区

    1. 基本语法:git add 文件名

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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
      
  2. 再次查看状态(检测到暂存区有新文件)

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:	//🟢检测到有文件被追踪了(暂存区有新文件)
      (use "git rm --cached <file>..." to unstage)
            new file:   hello.txt  	//(绿色)
    
  3. 如果此时删掉hello.txt

    1. 删除文件(只是删掉了暂存区的文件)

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git rm --cached hello.txt
      rm 'hello.txt'
      
    2. 查看工作区(工作区仍然存在hello.txt文件)

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ ll
      total 1
      -rw-r--r-- 1 lenovo 197121 110 Jun 26 11:33 hello.txt
      
    3. 查看本地库状态

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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)
      

3.5 提交本地库

  1. 将暂存区的文件提交到本地库(暂存区和工作区的代码是可以被删掉的

    1. 基本语法:git commit -m "日志信息" 文件名

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git commit -m "first commit" 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) f5dc083] first commit	
          //f5dc083]即为7位版本号
       1 file changed, 11 insertions(+) //一个文件被修改,11条记录被插入
       create mode 100644 hello.txt
      
  2. 查看状态

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git status
    On branch master //当前是主干分支
    nothing to commit, working tree clean 
    //已经提交过了,当前树是干净的,不需要再次提交
    

3.6 修改代码

  1. 修改代码

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ vim hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ cat hello.txt
    hello git
    hello git
    hello git
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    
  2. 查看当前本地库状态(检测到工作区有文件被修改 )

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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	
                //🔴(红色:未被追踪)提示本地文件被修改了
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
  3. 将修改的文件再次添加暂存区

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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
    
  4. 查看状态(工作区的修改添加到了暂存区)

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   hello.txt 
                //🟢(绿色:已被追踪)
    
  5. 再次提交到本地库

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git commit -m "first commit" 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 8c7d395] second commit
     1 file changed, 6 insertions(+), 8 deletions(-) //8行删除,6行新增
    
  6. 查看详细日志

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git log
    commit 8c7d39568ab72dd614b1d8992c05dc301584027b (HEAD -> master)
    Author: qhjqhj <1029930034@qq.com>
    Date:   Sat Jun 26 13:32:58 2021 +0800
    
        first commit
    
    commit f5dc083c86f7eba3a0a0d2102d36f92dfe5ae21c
    Author: qhjqhj <1029930034@qq.com>
    Date:   Sat Jun 26 12:02:22 2021 +0800
    
        second commit	//显示两次修改的版本
    
    
  7. 查看日志

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git reflog
    8c7d395 (HEAD -> master) HEAD@{0}: commit: second commit
        //表示当前指针是指向第二个版本的
    f5dc083 HEAD@{1}: commit (initial): first commit
        
    //此时查看文件,显示的第二个版本
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ cat hello.txt
    hello git
    hello git
    hello git
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    hello hhh
    

    工作区的文件始终只有一个

    在这里插入图片描述

3.7 历史版本

  1. 查看历史版本

    1. 基本语法、功能描述:

      1. git reflog 查看版本信息
      2. git log 查看版本详细信息
    2. 操作实例:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git reflog
      8c7d395 (HEAD -> master) HEAD@{0}: commit: second commit
      f5dc083 HEAD@{1}: commit (initial): first commit
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git log
      commit 8c7d39568ab72dd614b1d8992c05dc301584027b (HEAD -> master)
      Author: qhjqhj <1029930034@qq.com>
      Date:   Sat Jun 26 13:32:58 2021 +0800
      
          second commit
      
      commit f5dc083c86f7eba3a0a0d2102d36f92dfe5ae21c
      Author: qhjqhj <1029930034@qq.com>
      Date:   Sat Jun 26 12:02:22 2021 +0800
      
          first commit
      
  2. 版本穿梭

    1. 基本语法:git reset --hard 版本号

    2. 操作实例:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git reset --hard f5dc083
      HEAD is now at f5dc083 first commit
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git reflog
      f5dc083 (HEAD -> master) HEAD@{0}: reset: moving to f5dc083
      8c7d395 HEAD@{1}: reset: moving to HEAD
      8c7d395 HEAD@{2}: commit: first commit
      f5dc083 (HEAD -> master) HEAD@{3}: commit (initial): first commit
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ cat hello.txt //head指向的内容,已经是之前的版本
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      

      在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值