zoukankan      html  css  js  c++  java
  • git 命令 git status add rm commit mv

     1.查看 git 仓库文件改动状态

    Git 仓库内文件改动有 4 种状态,除了 Unmodified 状态的文件因为并未改动默认没有状态不做显示之外,其他文件改动状态都可以通过 git status 来查看

    查看 Git 记录的状态 常用命令,

    查看git仓库状态

    git status

    拿到一个git仓库,进入仓库,第一执行这个命令查看

    [root@ci-node1 ~]# cd /data/git_test/
    [root@ci-node1 git_test]# 
    [root@ci-node1 git_test]# git status
    On branch master  // 在master分支上 默认在master分支上
    
    Initial commit // 初始化commit
    
    nothing to commit (create/copy files and use "git add" to track) 
    // 现在是空仓库 你可以创建,拷贝文件然后可以使用git add 命令

    在工作区创建 a、b、c 三个文件。

    [root@ci-node1 git_test]# touch a b c
    [root@ci-node1 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Aug 24 22:36 a
    -rw-r--r-- 1 root root 0 Aug 24 22:36 b
    -rw-r--r-- 1 root root 0 Aug 24 22:36 c

    看 Git 记录的状态,从下面结果可知,新增的 3 个文件在 Git 空间里都属于Untracked 文件,存放在工作区内

    [root@ci-node1 git_test]# git status
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        a
        b
        c
    
    nothing added to commit but untracked files present (use "git add" to track)

    Git 仓库目录下的文件改动操作默认都发生在 Git 工作区内,Git 并不会主动管理。如果希望 Git 能够管理这些变动,你需要主动通知 Git。共有 3 种通知 Git 的命令(git add/rm/mv)


    2. 将工作区文件改动添加到暂存区 git add

    git add是第一种通知Git命令,这个命令用于告诉Git我们新增了文件改动,被git add命令操作过的文件(改动)便会处于 Git 暂存区

    [root@ci-node1 git_test]# git add a // 添加单文件改动到暂存区
    [root@ci-node1 git_test]# git status  // 查看此时的文件状态,a 文件已在暂存区中了
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        b
        c

     git add 把一个文件从工作目录移动到暂存区,从Untracked 状态到Staged状态

    暂存区是在.git目录下index文件,初始化仓库时候index文件不存在,当使用git add 把文件移动到暂存区这个index文件就生成了

    [root@ci-node1 git_test]# cd .git/
    [root@ci-node1 .git]# ll
    total 16
    drwxr-xr-x 2 root root   6 Aug 24 00:17 branches
    -rw-r--r-- 1 root root  92 Aug 24 00:17 config
    -rw-r--r-- 1 root root  73 Aug 24 00:17 description
    -rw-r--r-- 1 root root  23 Aug 24 00:17 HEAD
    drwxr-xr-x 2 root root 242 Aug 24 00:17 hooks
    -rw-r--r-- 1 root root  96 Aug 24 22:49 index // index文件生成
    drwxr-xr-x 2 root root  21 Aug 24 00:17 info
    drwxr-xr-x 5 root root  40 Aug 24 22:49 objects
    drwxr-xr-x 4 root root  31 Aug 24 00:17 refs

    我们可以使用 git add . git add * 可以一次将多个文件改动添加到暂存区

    把工作目录所有文件都提交到暂存区

    [root@ci-node1 git_test]# git add .

    输出结果

    // 查看此时的文件状态,3 个文件都已在暂存区中了
    
    [root@ci-node1 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
        new file:   b
        new file:   c

    3.将暂存区文件改动回退 git rm

    Git rm 命令用于告诉 Git 我们想把之前用 git add 添加的文件改动从 Git 暂存区里拿出去,不需要 Git 记录了。拿出去有两种拿法,一种是从暂存区退回到工作区,另一种是直接丢弃文件改动。让我们试着将 c 退回到工作区,b 直接丢弃。

    git rm 将文件从暂存区移回到工作目录,使状态Staged变成unstaged

    git rm --cached

    git rm -f

    git rm --cached

    //将 c 的改动从暂存区工作区

    //将 c 的改动从暂存区工作区
    [root@ci-node1 git_test]# git rm --cached c
    rm 'c'
    //查看 c 是否已经移回工作区
    [root@ci-node1 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
        new file:   b
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        c

    git rm -f

    //将 b 文件直接从 git 空间里删除,也是就是从暂存区和工作区都删除。

    //将 b 文件直接从 git 空间里删除,也是就是从暂存区和工作区都删除。
    [root@ci-node1 git_test]# git rm -f b
    rm 'b'
    
    //查看状态
    [root@ci-node1 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        c
    
    
    
    //当前目录中已经没有文件 b
    [root@ci-node1 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Aug 24 22:36 a
    -rw-r--r-- 1 root root 0 Aug 24 22:36 c

    4.提交 git commit

    把文件从暂存区提交到本地仓库

    当我们在仓库工作区下完成了文件增删改操作之后,并且使用 git add 将文件改动记录在暂存区之后,便可以开始将其提交到 Git 本地仓库。

    将暂存区内容提交 git commit –m “”   

    后面的“”是描述,描述这次提交内容是什么

    [root@ci-node1 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a

    //将暂存区内的文件 a.txt 提交到本地仓库

    //将暂存区内的文件 a.txt 提交到本地仓库
    root@ci-node1 git_test]# git commit -m "commit a"
    [master (root-commit) 73d7230] commit a
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a
    
    // 查看工作区状态,现在工作区是干净的工作区,可以理解为工作目录、
    //缓存区、本地仓库三个区域都存储内存是一样的,git三个区域缓存区、工作目录、本地仓库都保存着a文件,a文件在三个区域是一致的
    [root@ci-node1 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    
    // a文件被提交到本地仓库了,a文件才真正被git管理

    提交到本地仓库的文件在三个区域 工作目录,暂存区,本地仓库  分别存储一份,副本

     5.将暂存区文件移动位置/重命名 git mv

    Git mv 命令用于告诉 Git 我们想把之前用 git add 添加的文件直接在暂存区里重新命名或移动到新位置。

    git mv 对缓存区和工作目录的文件命名和移动到新位置

    //将 a 文件改名为 a.txt

    //将 a 文件改名为 a.txt
    [root@ci-node1 git_test]# git mv a a.txt
    [root@ci-node1 git_test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        renamed:    a -> a.txt
    
    // 直接告诉状态renamed
    
    [root@ci-node1 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Aug 24 22:36 a.txt

    再提交

    [root@ci-node1 git_test]# git commit -m "rename a to a.txt"
    [master cc8bd80] rename a to a.txt
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename a => a.txt (100%)
    
    [root@ci-node1 git_test]# git status
    On branch master
    nothing to commit, working tree clean

     

  • 相关阅读:
    火狐浏览器标签之间切换的快捷键
    LeetCode 69. x 的平方根
    LeetCode 51. N皇后
    win 10 自带 Ubuntu 系统的文件位置
    LeetCode 122. 买卖股票的最佳时机 II
    LeetCode 169. 求众数
    LeetCode 50. Pow(x, n)
    LeetCode 236. 二叉树的最近公共祖先
    LeetCode 235. 二叉搜索树的最近公共祖先
    LeetCode 98. 验证二叉搜索树
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/11406563.html
Copyright © 2011-2022 走看看