Git常用命令

创建版本库

  1. 克隆远程版本库
git clone <url>
  1. 初始化本地版本库
git init

修改和提交

  1. 查看状态
git status
  1. 查看变更内容
git diff
  1. 跟踪所有改动过的文件
git add .
  1. 跟踪指定的问题件
git add <file>
  1. 文件改名
git mv <old> <new>
  1. 删除文件
git rm <file>
  1. 停止跟踪文件但不删除
git rm --cached <file>
  1. 提交所有更新过的文件
git commit -m "commit message"
  1. 修改最后一次提交
git commit --amend

查看提交历史

  1. 查看提交历史
git log
  1. 查看制定文件的提交历史
git log -p <file>
  1. 以列表方式查看制定文件
git blame <file>

撤销

  1. 撤销工作目录中所有未提交文件的修改内容
git rest --hard HEAD
  1. 修改制定的未提交文件的修改内容
git checkout HEAD <file>
  1. 撤销制定的提交
git revert <cimmit>

分支与标签

  1. 显示所有本地分支
git branch
  1. 切换到制定分支或标签
git checkout <branch/tag>
  1. 创建新分支
git branch <new branch>
  1. 删除本地分支
git branch -d <branch>
  1. 列出所有本地分支
git tag
  1. 基于最新提交创建标签
git tag <tagname>
  1. 删除标签
git tag -d <tagname>

合并与衍合

  1. 合并制定分支到当前分支
git merge <branch>
  1. 衍合制定分支到当前分支
git rebase <branch>

远程操作

  1. 查看远程版本库信息
git remote -v
  1. 查看制定远程版本库信息
git remote show <remote>
  1. 添加远程版本库信息
git remote add <remote> <url>
  1. 从远程库获代码
git fetch <remote>
  1. 下载代码及快速合并
git pull <remote> <brabch>
  1. 上传代码及快速合并
git push <remote> <branch>
  1. 删除远程分支或标签
git push <remote> :<branch/tag-namr>
  1. 上传所有标签
git push --tags