• git 入门基本命令


      之前配置过Git,但用的也是可视化工具sourceTree,但对于我这种转行的小白,原理一直是一知半解,只知道一些简单的功能键。配置teamcity和jenkins的时候也是吃了不少苦头,今天把这些内容整理下来,以后用到也能顺手点。

      不同于集中式的版本控制系统,git是一个分布式版本控制系统。最大的区别是,本地会保存所有版本的内容,这样在中央库崩溃或者离线的情况时,依旧可以完成大部分的操作,减少与中央库的网络数据交互,提升效率。另外,越来越多的开源项目都迁移到了Github上,如果需要查看这部分代码,使用git最为方便。

    git的安装教程网上很多,首先从https://git-scm.com/download上下载对应系统的版本,然后按照教程安装就好。安装好后可以查看版本git --version。windows下除了有专门的git shell之外,还可以用git Bash,进行命令行的操作。

    git config --global user.name
    git config --global user.email

    git查询文档的命令:

    git config --help
    git help config
    man git-config

    基本的config增删改查操作:

    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global --unset user.email changjiang*
    
    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global --list
    user.name=Bruce
    core.autocrlf=true
    filter.lfs.clean=git-lfs clean %f
    filter.lfs.smudge=git-lfs smudge %f
    filter.lfs.required=true
    push.default=matching
    alias.lol=log --oneline --decorate --graph --all
    
    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global user.name
    Bruce
    
    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global --get user.name
    Bruce
    
    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global --add user.email xxx@yyy.com
    
    BruceChan@changjiang MINGW32 ~/Desktop
    $ git config --global --list
    user.name=Bruce
    user.email=xxx@yyy.com
    core.autocrlf=true
    filter.lfs.clean=git-lfs clean %f
    filter.lfs.smudge=git-lfs smudge %f
    filter.lfs.required=true
    push.default=matching
    alias.lol=log --oneline --decorate --graph --all

    另外,config还可以给命令起别名(以查看dubbo历史记录演示):

    f6cea42 DUBBO-422 修改父节点都为持久结点。
    5edb6a0 修改profile
    08c151f 修改profile
    7f3fabb DUBBO-370 泛化调用性能优化
    7eb7662 修改README
    a2189d1 修改demo依赖
    b8a90e8 修改demo依赖
    362071e DUBBO-422 订阅时创建节点,以免出现订阅空节点错误。
    c73252f DUBBO-422 订阅时创建节点,以免出现订阅空节点错误。
    20d5a66 DUBBO-435 zookeeper在删空数据后,没有通知empty消息
    :
    
    BruceChan@changjiang MINGW32 /e/company_project_git/dubbo ((dubbo-2.4.11))
    $ git config --global alias.lol "log --oneline"
    
    BruceChan@changjiang MINGW32 /e/company_project_git/dubbo ((dubbo-2.4.11))
    $ git lol
    0e1752e move executes attributes to abstractMethodType, so as to config executes on method
    40b5dad add timeout ZkclientZookeeperClient
    45e66f4 fix classload problem when dubbo jar at tomcat libs
    9a9d05f add dubbo-maven/src/ to git ignore
    261e71f update pom version to 2.4.11
    caff4bc update version to 2.4.10
    7dd34d5 DUBBO-625 业务对象的toString方法异常会导致RemotingInvocationTimeoutScan失效
    2d73e4f DUBBO-635 JValidator在类名生成的类名有$,有frozen class异常
    aabd4bc DUBBO-633 fix ut fail
    c81e278 解决method的配置executes参数的xsd验证错误
    ce1dde0 修改Redis注册中心宕机时,应用占用cpu高的问题。

    ---------------------------------------------------------------------------------------->

      Git使用40个十六进制字符的SHA1 Hash来唯一标示一个对象,Git中所指的对象有四种,blob(一个文件)、tree(一个目录)、commit(一次提交)、tag(一个固定的版本)。一个tag指向一个commit,而一个commit又指向一个tree对象,这代表工作区的提交时的某个状态,一个tree对象又指向其它tree和blob,如果文件内容一致,他们指向一个blob,文件的名称等其他信息存放在tree对象中。对这些对象的内容进行sha1 hash之后,就可以得到唯一标示。

      有了对象,我们就需要用到git仓库来对它们进行操作。git仓库分为带工作区--non-bare(含有.git)与不带工作区--bare两种,后者通常放在服务器上以供协作。对于裸仓(bare),它的工作文件就直接放在了仓库目录下,而非裸仓则放到了.git文件目录下。

    git init git_non_bare_repo
    git init git_bare_repo --bare

    注意裸仓和非裸仓在提示master的区别哦。如果已经存在了一份工作的代码,可以直接到这个目录下面,用git init这个命令来初始化一份git的工作空间。

    克隆本地库或者远程地址:

    git clone git_bare_repo/ git_bare_repo2

    如果是远程的仓库地址,可以直接使用默认的仓库地址名称,除非与本地的地址名称有冲突。

    ---------------------------------------------------------------------------------------->

      有了仓库,我们就可以进行基本操作,下面这张图基本囊括了几个比较重要的基本操作:

    working directory是工作区,是日常管理代码的区域,它维护着一个树形结构;history repository是commit指向的tree结构,而staging area是工作区和历史区的中间层,代表需要提交的状态,维护虚拟结构。

    git add -- 将工作区代码提交到历史
    git commit --将暂存区代码提交到服务器
    git status --查看暂存区的状态
    git rm -- 从暂存区删除不需要的元素
    git mv --工作区中重命名、移动文件并将之添加到暂存区
    gitignore --确定工作区里不想提交的文件(规则此处不详述)

    基本操作:

    创建文件提交暂存区,提交历史记录以及修改文件并提交:

    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ touch m n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  m  n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    warning: LF will be replaced by CRLF in a.
    The file will have its original line endings in your working directory.
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            m
            n
    
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git add .
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git commit -m "create m and n"
    [master warning: LF will be replaced by CRLF in a.
    The file will have its original line endings in your working directory.
    64e3367] create m and n
    warning: LF will be replaced by CRLF in a.
    The file will have its original line endings in your working directory.
     3 files changed, 1 insertion(+)
     create mode 100644 m
     create mode 100644 n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    On branch master
    nothing to commit, working directory clean
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  m  n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ vim m
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ 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:   m
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git add .
    warning: LF will be replaced by CRLF in m.
    The file will have its original line endings in your working directory.
    gi
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git commit -m "modify file m"
    [master warning: LF will be replaced by CRLF in m.
    The file will have its original line endings in your working directory.
    a83d53f] modify file m
    warning: LF will be replaced by CRLF in m.
    The file will have its original line endings in your working directory.
     1 file changed, 1 insertion(+)

    git rm <file>删除的是工作区文件,同时把状态自动add进了暂存区,此时如果想从历史区checkout的话,需要先将暂存区的状态抹掉

    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git rm m
    rm 'm'
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    m
    
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git checkout m
    error: pathspec 'm' did not match any file(s) known to git.
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git reset HEAD m
    Unstaged changes after reset:
    D       m
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git checkout m
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  m  n

    如果只是将暂存区的文件抹掉,可以用git rm -cached <file>,此时会发现在暂存区出现删除了file ,但工作区中,该文件仍然存在:

    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git rm --cached a
    rm 'a'
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            deleted:    a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            a
    
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git commit -m "delete a in history but keep in working directory"
    [master c28a690] delete a in history but keep in working directory
     1 file changed, 1 deletion(-)
     delete mode 100644 a
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  m  n
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            a
    
    nothing added to commit but untracked files present (use "git add" to track)

    修改文件名称用git mv <fileFrom> <fileTo>

    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ ls
    a  b  c  n  x
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git mv x y
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            renamed:    x -> y
    
    
    BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
    $ git commit -m "rename x to y"
    [master d24f75f] rename x to y
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename x => y (100%)
  • 相关阅读:
    数组优化 Dijkstra 最短路
    F
    树 (p155, 从中序和后续回复二叉树)
    矩阵连乘 LRJ白书 p141 栈 解析表达式
    Train Problem II HDU 1023 卡特兰数
    codevs 1166 矩阵取数游戏
    BZOJ 2754: [SCOI2012]喵星球上的点名
    2017.6.11 校内模拟赛
    HDU 2896 病毒侵袭
    UvaLive 4670 Dominating Patterns
  • 原文地址:https://www.cnblogs.com/bruceChan0018/p/5795831.html
Copyright © 2020-2023  润新知