git
  • git status 显示太多怎么办?比如很多临时文件,但是又不想加入到 .gitignore 文件中

    • 不显示新增文件: git status --untracked-files=no
    • 用tldr工具可以查到一些用法
  • revert 多笔提交,注意merge类型的提交

    #!/bin/bash
    
    commits=$1
    counts=$(git rev-list --count $commits)
    commits=$(git rev-list $commits)
    
    # 逐个回退提交
    remain=$counts
    for commit in $commits; do
      # 检查是否是合并提交
      echo "Reverting commit $commit"
      if git rev-parse "$commit^2" >/dev/null 2>&1; then
        # 如果是合并提交,使用 -m 选项
        git revert -m 1 $commit -n
      else
        # 如果不是合并提交,直接回退
        git revert $commit -n
      fi
    
      # 检查回退是否成功
      if [ $? -ne 0 ]; then
        echo "Failed to revert commit $commit, remain $remain commits"
        exit 1
      fi
    
      remain=$((remain - 1))
    done
    
    # 提交所有回退
    git commit -m "Revert the last $counts commits"
    
    echo "Successfully reverted the last $counts commits"
    
  • 查找最近公共祖先

    git merge-base branch1 branch2
    
  • 获取commit距离

    git rev-list --count HEAD ^commit