基础-02

Tutorial: Docker初级 Category: 运维 Published: 2026-04-07 13:58:25 Views: 21 Likes: 0 Comments: 0

Git 基础-02

  1. 高级命令

    • 每个提交在一行内显示

      git log --oneline
      

    • 日志搜索

      git log --all --grep='XXX'
      
    • 获取某人的提交日志

      git log --author="XXX"
      
    • 此次提交的 Commit 日志与上次 Commit 日志合并

      git commit --amend -m "上次的-m内容将被我覆盖"
      
    • 在上次提交中附加一些内容, 保持提交日志不变

      git add . && git commit --amend --no-edit
      
    • 不做任务改动空提交

      git commit --allow-empty -m "这是一次空提交"
      
    • 别名

      $ git config --global alias.co checkout
      $ git config --global alias.ci commit
      $ git config --global alias.br branch
      $ git config --global alias.st status
      $ git config --global alias.lg log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
      
    • 放弃本地修改, 回复原状(未 add 状态)

      git checkout -- .
      
    • 将所有 add 的文件移出缓存区

      $ git reset HEAD .

    • 回退到上一个版本(本地 commit 之后未 push)

      git reset --hard HEAD^
      
    • 回退到指定版本

      git reset --hard CommitID
      
    • 打 tag

      git tag 1.1.8
      
    • 切换到 tag 版本

      git checkout 1.1.8
      
    • 统计代码增加的行数

      git log --stat|perl -ne 'END { print $c } $c += $1 if /(\d+) insertions/'
      
    • 精确的代码行数修改数量

      git log --author="$(git config --get user.name)" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
      
    • 统计某位开发者有效的代码行数

      git log --author="张三" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "增加的行数:%s 删除的行数:%s 总行数: %s\n",add,subs,loc }'
      
    • 统计某人的 commit 数量

      git log --author="张三" --oneline | wc -l
      
    • 统计一段时间内代码的变化

      git log --pretty=tformat: --since ==2016-01-01 --until=2016-08-08 --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
      
    • 提交排名 top10

      git log --pretty=%aN | sort | uniq -c | sort -k1 -n -r | head -n 10
      
    • 统计有多少代码贡献者

      git log --pretty=%aN | sort -u | wc -l
      
  2. 分支操作

    • 跳到之前的分支

      git checkout -
      
    • 拉取远程最新代码并强制清除本地所有改动, 与远程保持一致

      $ git fetch origin
      $ git checkout master
      $ git reset --hard origin/master
      
    • 跳到之前的分支

      git checkout -
      
    • 比较两个分支

      git diff master b1
      
    • 删除远程分支

      git push origin --delete REMOTE_BRANCH
      
Prev: 基础-01 Next: None