Git 常用命令速查表

配置

  • 设置用户名:

    1
    git config --global user.name "Your Name"
  • 设置邮箱:

    1
    git config --global user.email "[email protected]"
  • 查看当前配置:

    1
    git config --list

初始化

  • 初始化新的 Git 仓库:

    1
    git init
  • 克隆远程仓库:

    1
    git clone [url]

基本操作

  • 查看当前状态:

    1
    git status
  • 添加文件到暂存区:

    1
    git add [file1] [file2] ...
  • 提交暂存区到仓库:

    1
    git commit -m "提交信息"
  • 查看提交日志:

    1
    git log

分支管理

  • 列出所有分支:

    1
    git branch
  • 创建新分支:

    1
    git branch [branch-name]
  • 切换到指定分支:

    1
    git checkout [branch-name]
  • 创建并切换到新分支:

    1
    git checkout -b [branch-name]
  • 合并指定分支到当前分支:

    1
    git merge [branch-name]
  • 删除分支:

    1
    git branch -d [branch-name]

远程操作

  • 查看远程仓库:

    1
    git remote -v
  • 添加远程仓库:

    1
    git remote add [shortname] [url]
  • 从远程仓库获取数据:

    1
    git fetch [remote]
  • 拉取远程分支并合并:

    1
    git pull [remote] [branch]
  • 推送本地分支到远程仓库:

    1
    git push [remote] [branch]

标签管理

  • 列出所有标签:

    1
    git tag
  • 创建新标签:

    1
    git tag [tag-name]
  • 删除标签:

    1
    git tag -d [tag-name]
  • 推送标签到远程仓库:

    1
    git push [remote] [tag-name]
  • 删除远程标签:

    1
    git push [remote] :refs/tags/[tag-name]

撤销操作

  • 撤销工作区的修改:

    1
    git checkout -- [file]
  • 重置暂存区的文件:

    1
    git reset HEAD [file]
  • 回退到上一个版本:

    1
    git reset --hard HEAD^
  • 将远程仓库覆盖本地仓库:

1
2
3
git fetch --all
git reset --hard origin/master
git clean -f
  • 重置本地分支以匹配远程分支:
1
2
git fetch --all
git reset --hard origin/[branch-name]

查看信息

  • 查看分支合并图:

    1
    git log --graph
  • 查看远程分支:

    1
    git branch -r
  • 查看所有分支:

    1
    git branch -a
  • 查看某个文件的修改历史:

    1
    git log -- [file]
  • 查看提交的变更内容:

    1
    git show [commit]

其他

  • 清理未跟踪的文件:

    1
    git clean -f
  • 显示当前分支的提交历史:

    1
    git reflog
  • 生成可供发布的压缩包:

    1
    git archive [branch-name] --format=zip --output=output.zip
  • 查看帮助文档:

    1
    git help [command]
  • 设置命令别名:

    1
    git config --global alias.[alias-name] [git-command]
  • 查看工作区状态:

    1
    git status
  • 查看提交日志:

    1
    git log