美文网首页前端小教程
Git操作流程:(一)库的创建与文件的增删改查

Git操作流程:(一)库的创建与文件的增删改查

作者: wsgdiv | 来源:发表于2021-02-04 18:49 被阅读0次

    1、注册

    安装完成后,在开始菜单里找到“Git”->“Git Bash”

    $ git config --global user.name "Your Name"
    $ git config --global user.email "email@example.com"
    

    git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

    2、创建版本库并提交文件

    $ mkdir learngit       //确保目录名(包括父目录)不包含中文
    $ cd learngit
    $ pwd                //显示当前目录
    /Users/michael/learngit
    $ git init                   //创建
    Initialized empty Git repository in /Users/michael/learngit/.git/             //如果你没有看到.git目录,那是因为这个目录默认是隐藏的,用ls -ah命令就可以看见
    
    $ git add readme.txt       //第一步,用命令git add告诉Git,把文件添加到仓库
    $ git commit -m "wrote a readme file"        //第二步,用命令git commit告诉Git,把文件提交到仓库
    [master (root-commit) eaadf4e] wrote a readme file
     1 file changed, 2 insertions(+)
     create mode 100644 readme.txt
    

    3、修改文件

    $ 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:   readme.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")           //提示readme.txt被修改过了,但还没有准备提交的修改。
    
    $ git diff readme.txt                    //查看修改的内容
    diff --git a/readme.txt b/readme.txt
    index 46d49bf..9247db6 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,2 @@
    -Git is a version control system.
    +Git is a distributed version control system.
     Git is free software.
    
    $ git add readme.txt                             
    $ git commit -m "add distributed"                        //提交修改
    [master e475afc] add distributed
     1 file changed, 1 insertion(+), 1 deletion(-)
    

    4、版本回退

    $ git log                           //查看提交日志
    commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
                 //版本号
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 21:06:15 2018 +0800
    
        append GPL
    
    commit e475afc93c209a690c39c13a46716e8fa000c366
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 21:03:36 2018 +0800
    
        add distributed
    
    commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
    Author: Michael Liao <askxuefeng@gmail.com>
    Date:   Fri May 18 20:59:18 2018 +0800
    
        wrote a readme file
    
    $ git reset --hard HEAD^                     //上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
    HEAD is now at e475afc add distributed
    
    $ git reset --hard 1094a                        //回到未来
    HEAD is now at 83b0afe append GPL
    
    $ git reflog                                      //记录你的每一次命令
    e475afc HEAD@{1}: reset: moving to HEAD^
    1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
    e475afc HEAD@{3}: commit: add distributed
    eaadf4e HEAD@{4}: commit (initial): wrote a readme file
    
    $ git diff HEAD -- readme.txt                   //git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别
    diff --git a/readme.txt b/readme.txt
    index 76d770f..a9c5755 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,4 +1,4 @@
     Git is a distributed version control system.
     Git is free software distributed under the GPL.
     Git has a mutable index called stage.
    -Git tracks changes.
    +Git tracks changes of files.
    

    5、撤销修改和删除文件

    git checkout -- readme.txt //让这个文件回到最近一次git commit或git add时的状态,可以丢弃工作区的修改。

    $ git reset HEAD readme.txt           //git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。
    Unstaged changes after reset:
    M   readme.txt
    
    $ git rm test.txt                //从版本库中删除该文件,那就用命令git rm删掉,并且git commit
    rm 'test.txt'
    
    $ git commit -m "remove test.txt"
    [master d46f35e] remove test.txt
     1 file changed, 1 deletion(-)
     delete mode 100644 test.txt
    
    $ git checkout -- test.txt             //删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本。git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
    

    素材来源:廖雪峰的git教程

    相关文章

      网友评论

        本文标题:Git操作流程:(一)库的创建与文件的增删改查

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