zoukankan      html  css  js  c++  java
  • Git 基本操作

    Git基本操作(add,commit的理解)

     

    1.创建仓库

      ——创建工作目录(Working Directory):git三种副本:工作目录(Working Direcotry),暂存区域(Stage,索引(Index)),仓库(History)

    复制代码
    #/home/yang/Documents/repo01
    . ├── datafiles │   └── data.txt ├── test01 ├── test02 └── test03
    复制代码
    # Initialize the local Git repository(在根目录下生成.git文件夹)
    git init

    2.查看文件状态

    复制代码
    yang@mint-linux ~/Documents/repo01 $ git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #    datafiles/
    #    test01
    #    test02
    #    test03
    nothing added to commit but untracked files present (use "git add" to track)
    复制代码

    3.添加文件

      ——git add files 把当前文件放入暂存区域

    复制代码
    yang@mint-linux ~/Documents/repo01 $ git add .
    yang@mint-linux ~/Documents/repo01 $ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    # (use "git rm --cached <file>..." to unstage)
    #
    #    new file: datafiles/data.txt
    #    new file: test01
    #    new file: test02
    #    new file: test03
    #
    复制代码

    3.提交文件

      ——git commit 给暂存区域生成快照并提交。

    复制代码
    yang@mint-linux ~/Documents/repo01 $ git commit -m "Initial commit"
    [master (root-commit) 3f79459] Initial commit
     4 files changed, 4 insertions(+)
     create mode 100644 datafiles/data.txt
     create mode 100644 test01
     create mode 100644 test02
     create mode 100644 test03
    yang@mint-linux ~/Documents/repo01 $ git status
    # On branch master
    nothing to commit, working directory clean
    复制代码

    4.查看文件内容变更

    复制代码
    yang@mint-linux ~/Documents/repo01 $ echo "This is a change" > test01
    yang@mint-linux ~/Documents/repo01 $ echo "and this is another change" > test02
    yang@mint-linux ~/Documents/repo01 $ git diff
    diff --git a/test01 b/test01
    index 749eb2d..d0a432b 100644
    --- a/test01
    +++ b/test01
    @@ -1,4 +1 @@
    -datafiles
    -test01
    -test02
    -test03
    +This is a change
    diff --git a/test02 b/test02
    index e69de29..552c22e 100644
    --- a/test02
    +++ b/test02
    @@ -0,0 +1 @@
    +and this is another change
    yang@mint-linux ~/Documents/repo01 $ git commit -a -m "These are new changes"
    复制代码

    5.查看工作目录提交记录

    (回顾:git status--查看文件状态,git-diff--查看文件内容状态)

    复制代码
    # Make some changes in the file
    echo "This is a new change" > test01
    echo "and this is another new change" > test02
    复制代码
    复制代码
    yang@mint-linux ~/Documents/repo01 $ git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #    modified:   test01
    #    modified:   test02
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    yang@mint-linux ~/Documents/repo01 $ git diff
    diff --git a/test01 b/test01
    index d0a432b..18fec42 100644
    --- a/test01
    +++ b/test01
    @@ -1 +1 @@
    -This is a change
    +This is a new change
    diff --git a/test02 b/test02
    index 552c22e..0b17386 100644
    --- a/test02
    +++ b/test02
    @@ -1 +1 @@
    -and this is another change
    +and this is another new change
    yang@mint-linux ~/Documents/repo01 $ git add . && git commit -m "More changes - type in the commit message"
    [master 8a18ab1] More changes - type in the commit message
     2 files changed, 2 insertions(+), 2 deletions(-)
    yang@mint-linux ~/Documents/repo01 $ git log
    commit 8a18ab1c77ecaf049d17e5ac8fb682ae618cd710
    Author: Will Hunting <yangqionggo@gmail.com>
    Date:   Sun Aug 18 18:57:38 2013 +0800
    
        More changes - type in the commit message
    
    commit a6bd74cdbaa1b349d537008f33fa186eae9d48c9
    Author: Will Hunting <yangqionggo@gmail.com>
    Date:   Sun Aug 18 18:56:07 2013 +0800
    
        These are new changes
    
    commit 3f794593e7008286a893a5d00f81ee5757140469
    Author: Will Hunting <yangqionggo@gmail.com>
    Date:   Sun Aug 18 18:50:58 2013 +0800
    
        Initial commit
    复制代码

    6.删除文件

    如果你删除了一个在版本控制之下的文件,那么使用git add .不会在索引中删除这个文件。需要通过带-a选项的git commit命令和-A选项的git add命令来完成

    复制代码
    # Create a file and put it under version control
    touch nonsense.txt
    git add . && git commit -m "a new file has been created"
    # Remove the file
    rm nonsense.txt
    # Try standard way of committing -> will not work 
    git add . && git commit -m "a new file has been created"
    # Now commit with the -a flag
    git commit -a -m "File nonsense.txt is now removed"
    # Alternatively you could add deleted files to the staging index via
    git add -A . 
    git commit -m "File nonsense.txt is now removed"
    复制代码

    7.更正提交的信息

    git commit --amend -m "More changes - now correct"
  • 相关阅读:
    第一次 实习笔记
    docker 提示 Drive has not been shared 错误
    使用matplotlib画出log的图像
    python基础实现tcp文件传输
    django简单实现注册登录模块
    python使用消息队列RabbitMq(进阶)
    scrapy爬虫值Items
    redis常见配置redis.conf
    js实现往数组中添加非存在的对象,如果存在就改变键值。
    CSS之user-select——设置标签中的文字是否可被复制
  • 原文地址:https://www.cnblogs.com/gaosheng-221/p/6784834.html
Copyright © 2011-2022 走看看