美文网首页git学习空间
Git使用教程-4.Git状态

Git使用教程-4.Git状态

作者: Justin小贾同学 | 来源:发表于2021-09-16 16:35 被阅读0次

    status

    1. 接着之前初始化的仓库,我们在MyProject目录下右键打开Git bash,运行git status命令看看会出现什么东西。
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    1. 我们往README.md里面添加一些内容,再运行git status命令查看结果
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (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:   README.md #注意这里是红色的
            
    no changes added to commit (use "git add" and/or "git commit -a")
    
    1.png

    git status命令可以输出仓库当前的状态,上面的命令输出告诉我们,README.md已经被修改过了,但还没有对修改进行提交。
    接下来,我们使用git diff命令查看具体修改了什么内容。

    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git diff
    warning: LF will be replaced by CRLF in README.md.
    The file will have its original line endings in your working directory
    diff --git a/README.md b/README.md
    index 106287c..f83e8a5 100644
    --- a/README.md
    +++ b/README.md
    @@ -1 +1,2 @@
     Hello Git!
    +Git is free software. #注意这是绿色的,也就是新增的内容。
    
    2.png
    1. 通过git status命令我们知道了哪些文件进行了修改,并且git diff还可以告诉我们具体修改了哪些内容。这样我们就可以安心的把它们提交到仓库了。
      第一步,先用git add命令,命令行没有输出什么提示,我们再用git status命令看看发现‘modified: README.md’已经从红色变为绿色了。
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git add README.md
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   README.md # 注意这是绿色的
    

    第二步,使用git commit进行提交。

    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git commit -m "add distributed"
    [master 668e5ad] add distributed
     1 file changed, 1 insertion(+)
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    3.png

    小结

    • 初始化一个 Git 仓库,使用 git init 命令。
    • 添加文件到 Git 仓库,分两步: 使用命令git add <file>,可反复多次使用,添加多个文件; 使用命令 git commit -m <message>完成提交。
      LS

    案例

    1. 我们创建几个文件在工作目录里面,使用git status命令输出仓库当前的状态
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ touch index.php login.php
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch mastergit 
    Untracked files: #未被追踪的文件
      (use "git add <file>..." to include in what will be committed)
            index.php
            login.php
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $
    
    4.png
    1. 下面,我们对index.php文件的修改追踪,再通过git status命令查看当前仓库状态
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git add index.php
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   index.php #注意这是绿色的
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            login.php #注意这是红色的
    
    1. 我们把login.php也一并提交到仓库
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git add login.php
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git commit -m "add tow files"
    [master ea56a43] add tow files
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 index.php
     create mode 100644 login.php
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    23696@DESKTOP-GKRBCVI MINGW64 ~/Desktop/MyProject (master)
    
    5.png

    这个案例非常简单,一个是为了再次熟悉流程,另外就是告诉大家可以多次在add后,一次commit完成提交。

    相关文章

      网友评论

        本文标题:Git使用教程-4.Git状态

        本文链接:https://www.haomeiwen.com/subject/bflrgltx.html