Git 常用命令(本地)

作者: 倔强的潇洒小姐 | 来源:发表于2018-03-06 22:13 被阅读37次
    Git安装可以参考此文第六点

    PS:如果在MAC上操作,安装有2种方法
    1、安装homebrew,然后通过homebrew安装Git,安装方法:http://brew.sh/
    2、从AppStore安装Xcode,Xcode集成了Git,不过默认没有安装,你需要运行Xcode,选择菜单“Xcode”->“Preferences”,在弹出窗口中找到“Downloads”,选择“Command Line Tools”,点“Install”就可以完成安装了


    (1)git config:配置用户信息
    因为Git是分布式版本控制系统,所以,每个机器都必须有名字和Email地址。

    git config user.name "XXX"
    git config user.email XXX@XXX.com
    

    全局加上--global,表示这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址

    git config --global user.name "XXX" (配置所有项目用户信息)
    git config --global user.email XXX@XXX.com (配置所有项目邮箱信息)
    

    获取Git配置信息,只需执行 git config --list


    获取Git配置信息

    (2)git init:创建一个新的Git版本库
    把这个目录变成Git可以管理的仓库,每个文件的修改、删除,Git都能跟踪

    [carina@localhost ***]$ mkdir learngit && cd learngit
    [carina@localhost learngit]$ git init
    Initialized empty Git repository in /home/carina/***/learngit/.git/
    

    (3)git add:添加文件

    git add README.md      //单个文件
    git add a.txt b.txt c.txt      //多个不同的文件,以空格分隔
    git add .       //所有文件,包括子目录,但不含空目录
    

    (4)git commit:提交文件
    -m后面输入的是本次提交的日志记录,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

    git commit -m "增加接口说明等等类似的明确描述"        //提交所有修改
    git commit -m "增加****"  文件名       //提交单个文件
    

    为什么Git添加文件需要add,commit一共两步?
    文件可以多次add,commit可以批量添加


    (5)git status:查看当前状态

    [carina@localhost learngit]$ git add hw.text
    [carina@localhost learngit]$ vi hw.text       //修改文件内容
    [carina@localhost learngit]$ git status    //查看仓库状态
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
          new file:   hw.text
    
    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:   hw.text
    

    以上提示文件内容被修改过了,但还没有准备提交,如想看修改明细,需要用到命令 git diff
    (文件原内容为hello world)

    [carina@localhost learngit]$ git diff hw.text
    diff --git a/hw.text b/hw.text
    index 3b18e51..2f61005 100644
    --- a/hw.text
    +++ b/hw.text
    @@ -1 +1,4 @@
    -hello world
    +hello
    +say goodbye
    +yes
    +no
    

    (6)git log:查看日志
    如果一个文件修改次数过多,时间一长难免会忘了改了些什么内容,git log命令显示从最近到最远的修改日志

    [carina@localhost learngit]$ git log
    commit bf186caef13e38183d4df1b5ec225c15f1dbc069 (HEAD -> master)
    Author: username <email>
    Date:   Tue Mar 6 07:18:11 2018 -0800
    
        delete yes and no
    
    commit fbf59465ca20b6ed81ca83d2723815141c1c79e1
    Author: username <email>
    Date:   Tue Mar 6 07:16:50 2018 -0800
    
        add see you tomorrow
    
    commit cc0d244b063570b2591166956e6ac84763c53f7c
    Author: username <email>
    Date:   Tue Mar 6 07:15:28 2018 -0800
    
        delete say
    

    加上--pretty=oneline参数可以查看精简版输出

    [carina@localhost learngit]$ git log --pretty=oneline
    bf186caef13e38183d4df1b5ec225c15f1dbc069 (HEAD -> master) delete yes and no
    fbf59465ca20b6ed81ca83d2723815141c1c79e1 add see you tomorrow
    cc0d244b063570b2591166956e6ac84763c53f7c delete say
    

    类似bf186caef...15f1dbc069的一大串字符是Git的commit id(版本号)

    (7)git reset:版本回退
    在 Git中,用HEAD表示当前版本,也就是最新的提交commit id(bf186caef...15f1dbc069),上一个版本就是HEAD^ ,上上一个版本就是HEAD^^ ......依此类推,1000个版本就要写1000个^,数值越大写起来越麻烦,可写成HEAD~100

    现在要回到add see you tomorrow这个版本
    1、执行命令 git reset --hard HEAD^
    2、查看文件内容 执行 cat

    [carina@localhost learngit]$ git reset --hard HEAD^
    HEAD is now at fbf5946 add see you tomorrow
    [carina@localhost learngit]$ cat hw.text
    hello
    goodbye
    Lily
    yes
    no
    see you tomorrow
    

    注:HEAD为大写

    [carina@localhost learngit]$ git reset --hard head^
    fatal: ambiguous argument 'head^': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'
    

    思考一个问题:
    此时想要回到刚才那个版本,怎么办?

    1、只要命令行窗口没有被关掉,就可以往上翻记录,找到delete yes and no 的commit id是bf186cae...执行reset后查看文件内容
    格式:git reset --hard commit id(任一版本)

    [carina@localhost learngit]$ git reset --hard bf186cae
    HEAD is now at bf186ca delete yes and no
    [carina@localhost learngit]$ cat hw.text
    hello
    goodbye
    Lily
                //yes行
                //no行
    see you tomorrow
    

    2、命令行被关掉,找不到新版本的commit id
    git reflog用来记录用户的每一次命令,所以说commit时的描述非常重要,查找起来很方便

    [carina@localhost learngit]$ git reflog
    bf186ca (HEAD -> master) HEAD@{0}: reset: moving to bf186cae
    fbf5946 HEAD@{1}: reset: moving to HEAD^
    bf186ca (HEAD -> master) HEAD@{2}: commit: delete yes and no
    fbf5946 HEAD@{3}: commit: add see you tomorrow
    cc0d244 HEAD@{4}: commit: delete say
    bf8a52f HEAD@{5}: commit (initial): first commit

    相关文章

      网友评论

        本文标题:Git 常用命令(本地)

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