git 操作

文件状态:

已提交(committed)
已修改(modified) 文件change => 已修改
已暂存(staged) git add . => 暂存状态

  1. 取消 git merge - 还未 git add // 详见 撤销操作(标黑处)
git merge --abort
  1. 查看所以分支
git branch -a
  1. 删除本地分支
git branch -d <BranchName>
  1. 删除远程分支
git push origin --delete <BranchName>
  1. 更新远程分支
git fetch
  1. 暂存文件(不想提交内容)
        git stash
        git stash list // 查看暂存文件
        git stash pop [stash@{index}]  // 释放[第idx条]暂存文件,无[],默认释放第index=0条
        git stash drop [stash@{index}] // 删除[第idx条]暂存文件,无[],默认删除第index=0条
  1. 释放暂存文件
git stash pop
  1. 删除暂存文件
git stash drop
  1. 更新git版本
git clone git://git.kernel.org/pub/scm/git/git.git
  1. 查看git配置
git config --list
  1. 查看某项配置
git config <key> //// 如: git config user.name
  1. 获取config命令手册
git help config
  1. 克隆远程分支到本地(仓库)
git clone <url> [本地仓库名字]   //// 本地仓库名字如果不添加,就是远程仓库的同一名字
  1. 跟踪新文件 / 将已跟踪的文件放至暂存区 / 将合并时有冲突的文件标记为已解决
git add
  1. 查看文件状态
        git status
        git status -s / git status --short
        // 注:_ 表示空,无内容
            M _ _  fileName  —— 该文件已被暂存
            _ _ M  fileName  —— 该文件已被修改,还未暂存
            M M _  fileName  —— 该文件修改已暂存,又被修改
            A _ _  fileName  —— 新添加到暂存区的文件
            ? _ ?  fileName  —— 新添加的文件,还未被追踪
  1. .gitignore文件 -- 将不希望被追踪的文件写入该文件中
        此处相当于注释
        node_modules/   该目录下所有文件
        .idea           idea后缀的文件
        .[abcd]         .a/.b/.c  单个字母匹配
        [0__9]          包含0-9 之间的数字匹配
        !               取反,要忽略指定模式以外的文件或目录
        ?               匹配1个任意字符
        *               匹配一个或多个任意字符
  • 查看已暂存和未暂存的修改
        git diff    ///只显示尚未暂存的改动
        git diff --cached    // 低版本  可查看已暂存的改动
        git diff  --staged    // 高版本  可查看已暂存的改动
        如: vscode    GitLens插件
  • 提交更新
        git commit [-m 注释]
        git commit -a [-m 注释]  //  可以省略 git add 步骤,如果是新增加的文件,还未被追踪,还是需要 git add 步骤!
  • 移除文件
        git rm </fileName>   //  删除[目录]文件(未推送至暂存区),并不继续追踪该文件 [后续需要git add , git commit 推送至远程分支]
        git rm -f </fileName>  // 删除[目录]文件(已推送至暂存区),并不继续追踪该文件 [后续需要git add , git commit 推送至远程分支]
        git rm --cached </fileName>  // 删除远程[目录]文件,并不继续追踪该文件,修改该文件之后仍会被提示,需要在修改之前,将该文件写入 .gitignore
  • 修改文件名字
            git mv originName afterName   // 修改的文件必须是已经被追踪的文件 [后续需要git commit 推送至远程分支]
            已上命令相当于执行了:mv originName afterName  |   git rm originName  |   git add afterName
  • 查看日志

    git log

        commit 43c46cabd67afb19454c93eb05a93da98a4d8fc2 (HEAD -> read45, origin/read45)
        Author: lu <2286703734@qq.com>
        Date:   Tue Nov 19 16:19:01 2019 +0800
    
                five commit test.js
    ———————————————————————————————
        -(n)                仅显示最近的 n 条提交
        --since, --after        仅显示指定时间之后的提交。
        --until, --before        仅显示指定时间之前的提交。
        --author            仅显示指定作者相关的提交。
        --committer        仅显示指定提交者相关的提交。
        --grep            仅显示含指定关键字的提交
        -S                仅显示添加或移除了某个关键字的提交
    ———————————————————————————————
        git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \ --before="2008-11-01" --no-merges -- t/      [--all-match] —满足所有查询条件
    

    git log -p [-<n>] // n 为任意整数,但是实际调用时,git log会自动分页调取,n过大时并不可用

        commit 194fc88d94f025ef21fcb82edc02f101ab46370b
        Author: lu <2286703734@qq.com>
        Date:   Tue Nov 19 16:18:29 2019 +0800
    
                four commit test.js
    
        diff --git a/src/utils/test.js b/src/utils/test.js
        index bdd818a..d9da4ea 100644
        --- a/src/utils/test.js
        +++ b/src/utils/test.js
        @@ -11,4 +11,9 @@
         // third commit 
        +
        +
        

    git log --stat

        commit 43c46cabd67afb19454c93eb05a93da98a4d8fc2 (HEAD -> read45, origin/read45)
        Author: lu <2286703734@qq.com>
        Date:   Tue Nov 19 16:19:01 2019 +0800
    
                five commit test.js
    
        src/utils/test.js | 6 ++++++
         1 file changed, 6 insertions(+)
        

    git log --pretty=oneline

        43c46cabd67afb19454c93eb05a93da98a4d8fc2 (HEAD -> read45, origin/read45) five commit test.js
        194fc88d94f025ef21fcb82edc02f101ab46370b four commit test.js
        be73cffe8682ff576a7b1dcc7885eaa36daf1062 third commit test.js
        

    git log --pretty=short

        相比于git log  少了时间
        

    git log --pretty=full

    commit 43c46cabd67afb19454c93eb05a93da98a4d8fc2 (HEAD -> read45, origin/read45)
    Author: lu <2286703734@qq.com>
    Commit: lu <2286703734@qq.com>
    
            five commit test.js
        

    git log --pretty=format

            // git log --pretty=format:%an  |  git log --pretty=format:"%h - %an, %ar : %s"
            
    ———————————————————————————————
        %H        提交对象(commit)的完整哈希字串
        %h        提交对象的简短哈希字串
        %T        树对象(tree)的完整哈希字串
        %t        树对象的简短哈希字串
        %P        父对象(parent)的完整哈希字串
        %p        父对象的简短哈希字串
        %an        作者(author)的名字  — 修改代码的人
        %ae        作者的电子邮件地址
        %ad        作者修订日期(可以用 --date= 选项定制格式)
        %ar        作者修订日期,按多久以前的方式显示
        %cn        提交者(committer)的名字  — 提交代码的人
        %ce        提交者的电子邮件地址
        %cd        提交日期
        %cr        提交日期,按多久以前的方式显示
        %s        提交说明
    ———————————————————————————————
撤销操作
    git commit --amend       // 忘记暂存问价之后,可再次执行git add , git commit ,然后执行 --amend去替换之前的信息,如果已经全部暂存,则之后更改提交的注释信息???
    git reset HEAD<fileName>    // git add (暂存)之后,可用该命令取消暂存
    git checkout  --<fileName>   // 可以撤销自上次提交之后的所有更改
  • 远程仓库
        git remote   // 查看远程仓库名字
        git remote -v  // 查看远程仓库名字  路径
        git remote show <name>  // 获取远程仓库的名字,地址,远程分支的名字以及跟踪情况
        git remote add <shortName> <gitUrl>  // 添加一个新的远程仓库
        git fetch <gitUrl / name / shortName>  // 从远程中获取数据,不会自动合并更新/冲突
        git remote rename <originName> <afterName>  // 远程分支重命名
        git remote rm <name>  // 移除远程分支
  • 标签
        git tag  // 查询标签
        git tag <tagName>  // 创建 轻量标签 — 只有提交的hash, 提交注释(普通提交)
        git tag -a <tagName> -m <注释>  // 创建 附注标签 — 包含打签者的名字,邮件地址,日期,标签信息等 
        git show <tagName>  //  展示该标签的信息
        git tag -a <tagName> <hash>  // 后期给某次提交打标签,hash可以是全数据/部分校验和(9fceb02)
        git push origin <tagName>   // 共享标签,将某个标签推送到远程
        git push origin --tags   // 共享标签,推送所有标签到远程
        git tag -d <tagName>  // 删除本地标签
        git push <remote> :refs/tags/<tagName>   // 删除远程标签   remote:远程仓库名字(不会删除本地标签)
        git checkout <tagName>  // 检出标签,相当于切换到该标签的代码,不可更改,更改也无用,但是可以从该标签切换出一个分支去做迭代 
  • GIT别名
            git config --global alias.<别名> <原操作>
            git config --global alias.<别名> <!非git操作>
            git config --global alias.last  ‘log -1 HEAD’   =>  git last  // 打印最后一次提交信息 
            git config --global alias.co ‘checkout’ => git co dev  // 切换分支
            git config --global alias.visual '!gitk'
  • GIT分支
    HEAD 是指针,指向当前分支,切换分支时会改变指向
    git clone 克隆拉取远程数据并创建一个指向它master的指针,本地将该指针命名为 origin/master ——origin为远程仓库的名字,可更改
        git clone <gitUrl> [-o <originName>]   // <originName>/master

        git branch <branch>   // 创建分支
        git checkout <branch>  //  切换分支
        git checkout -b <branch>   // 由当前分支创建并切换到新分支
        git checkout --track <remote>/<branch>  // 
        git checkout -b <branch> <remote>/<branch>  // 创建一个跟踪分支并切换至该分支
            eg:   git checkout -b dev2 origin/dev   —跟踪远程origin/dev分支
        git branch -u <remote>/<branch>  |  git branch --set-upstream-to <remote>/<branch>   // 切换追踪的远程分支
        git branch -d <branch>  //  删除本地分支   -D  强制删除
        git push origin —delete <branch>  //   删除远程分支
        git merge <branch>  //  合并branch分支到当前分支

        git branch   //  查看所有分支,*跟随的分支时当前所处的分支
        git branch -v  //  查看每一个分支的最后一次提交
        git branch -vv  //  查看所有分支的远程分支及领先落后情况   ahead—领先   behind—落后   |  该命令下只是当前相对本地服务器的一下状态,如过需要最新的,可以执行下面命令
        git fetch --all; git branch -vv;  // git fetch 只会更新,不会自动合并; git pull 会自动合并
        git branch --merged  //  查看已经合并到当前分支的分支
        git branch --no-merged  //  查看未合并到当前分支的分支
        git ls-remote [remote]  //  查看远程引用完整列表   remote—远程名字(origin)
        git remote show [remote]  //  查看远程分支信息
        git fetch <remote>  //  从中抓取本地没有的数据,更新本地数据库,移动<remote>/master指向更新、更候的位置

  • GIT变基
        可以将一分支的修改在另一分支上应用一次,类似于重新播放。  // 可以保持提交历史的整洁性
        git rebase master(c3)  // HEAD=>Fix(c4)  将C4变基到C3
        git merge Fix  // HEAD=>master   

不定期更新
欢迎一键三联(虽然也不知道哪三联 ?)ღ( ´・ᴗ・` )比心


桃小妖
278 声望16 粉丝