Git常用命令参考手册
基本涵盖了在开发中用到的git命令,能满足日常需求。
目录
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   |  git config -l
  git config --local --list
 
  git config --global --get user.name git config --global --get user.email
 
  git config --global user.name "xiejiahe" git config --global user.email "example@example.com"
 
  git config --local user.name "xiejiahe" git config --local user.email "example@example.com"
 
  git config --global core.editor emacs
 
  git config --global merge.tool vimdiff
 
  | 
 
生成SSH_Key
1 2 3 4 5 6 7 8 9
   |  ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
 
  > Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
 
  > Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
 
  | 
 
最后需要将生成的 SSH Key 添加到 ssh config 中
1 2 3 4 5 6 7 8
   |  vim ~/.ssh/config
 
  Host *   AddKeysToAgent yes   UseKeychain yes   IdentityFile ~/.ssh/id_rsa
 
  | 
 
初始化仓库
git init 创建一个空的Git仓库或重新初始化一个现有的仓库
1 2 3 4 5 6 7 8
   |  git init
 
  git init -q
 
  git init --bare
 
  | 
 
文件状态
1 2 3 4 5 6 7 8
   |  git status
 
  git status -s
 
  git status --ignore-submodules
 
  | 
 
日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   |  git log
 
  git log -2
 
  git log -p -2
 
  git log -S Java
 
  git log --merges
 
  git log --graph --oneline
 
  git shortlog -sn
 
  git shortlog -n
 
  git shortlog -e
 
  git blame README.md
 
  | 
 
克隆
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   |  git clone https://github.com/xjh22222228/git-manual.git
 
  git clone git@github.com:xjh22222228/git-manual.git
 
  git clone -b master https://github.com/xjh22222228/git-manual.git
 
  git clone --recursive git@github.com:xjh22222228/git-manual.git
 
  git clone --depth=1 https://github.com/xjh22222228/git-manual.git
 
  | 
 
查看分支
1 2 3 4 5 6 7 8
   |  git branch --all
 
  git branch
 
  git branch -r
 
  | 
 
切换分支
1 2 3 4 5 6 7 8 9
   |  git checkout master git switch master
 
  git checkout -
 
  git checkout -t origin/dev
 
  | 
 
创建分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   |  git branch develop
 
  git checkout -b develop
 
  git checkout -b develop git push origin develop
 
 
  git checkout --orphan develop
  git rm -rf .
  git add -A && git commit -m "提交"
  git push --set-upstream origin develop
 
  | 
 
删除分支
1 2 3 4 5 6
   |  git branch -d <branchName>
 
  git branch -d -r origin/<branchName> git push origin :<branchName>
 
  | 
 
重命名分支
1 2
   |  git branch -m <branchName>
 
  | 
 
代码合并
1 2 3 4 5 6 7 8 9
   |  git checkout develop git merge feature/v1.0.0
 
  git merge feature/v1.0.0 develop
 
  git merge develop -q
 
  | 
 
暂存
1 2 3 4 5 6 7 8 9 10 11
   |  git add -A
 
  git add ./README.md
 
  git add .
 
  git add 1.txt 2.txt ...
 
  | 
 
删除
git add 的反向操作
1 2 3 4 5 6 7 8
   |  git rm 1.txt
 
  git rm -rf .
 
  git rm -r --cached .
 
  | 
 
提交
1 2 3 4 5 6 7 8 9 10 11
   |  git commit -m "changes log"
 
  git commit -v
 
  git commit --allow-empty-message
 
  git commit --amend -m "新的提交信息"
 
  | 
 
推送
1 2 3 4 5 6 7 8 9 10 11
   |  git push -u origin master
 
  git push origin <branchName>:<branchName>
 
  git push
 
  git push -f
 
  | 
 
拉取最新内容
1 2 3 4 5 6 7 8 9 10 11
   |  git fetch origin master
 
  git pull
 
  git pull origin master:master
 
  git pull origin master
 
  | 
 
查看文件的改动
1 2 3 4 5 6 7 8 9 10 11 12
   |  git diff
 
  git diff README.md
 
  git diff d68a1ef2407283516e8e4cb675b434505e39dc54
 
  git log README.md git show d68a1ef2407283516e8e4cb675b434505e39dc54 README.md
 
  | 
 
回滚版本
1 2 3 4 5 6 7 8 9 10 11
   |  git reset --hard HEAD^
 
  git reset --hard HEAD^^
 
  git reset --hard 'commit id'
 
  git reflog
 
  | 
 
撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   |  git checkout -- .
 
  git checkout -- README.md
 
  git reset HEAD ./README.md
 
  git reset <commit_id>
 
  git reset --hard <commit_id>
 
  | 
 
标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   |  git tag
 
  git ls-remote --tags origin
 
  git tag -l "v1.0.0*"
 
  git tag -a v1.1.0 -m "标签描述"
 
  git tag v1.1.0
 
  git log git tag -a v1.1.0 <commit_id>
 
  git push origin v1.1.0
 
  git push origin --tags
 
  git tag -d v1.1.0
 
  git push origin --delete v1.1.0
 
  git checkout v1.1.0
 
  git show v1.1.0
 
  | 
 
Rebase
git rebase 主要作用可以将多个commit记录合并为一条
1 2 3 4 5 6 7 8 9 10 11
   |  git rebase -i HEAD~4
  git rebase -i e88835de905ad396f61a0dc8c040a8ac8a34f3f8
 
 
  git rebase --abort
 
  git rebase --continue
 
  | 
 
参考:git rebase将多次commit合并为一条
GitFlow
Git Flow 不是内置命令,需要单独安装
初始化 每个仓库都必须初始化一次
功能
1 2 3 4 5 6 7 8
   |  git flow feature start v1.1.0
 
  git flow feature publish v1.1.0
 
  git flow feature finish v1.1.0
 
  | 
 
打补丁
hotfix是针对 master 进行打补丁的
1 2 3 4 5 6 7 8
   |  git flow hotfix start v1.1.0_hotifx
 
  git flow hotfix publish v1.1.0_hotifx
 
  git flow hotfix finish v1.1.0_hotifx
 
  | 
 
发布
1 2 3 4 5 6 7 8
   |  git flow release start v1.1.0
 
  git flow release publish v1.1.0
 
  git flow release finish v1.1.0
 
  | 
 
Git flow schema

子模块
具体使用还可以看这里 git submodule子模块使用教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   |  git submodule add https://github.com/xjh22222228/git-manual.git
 
 
  git submodule update --remote
  git pull
 
  git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'
 
  git submodule deinit <common>
  git rm --cached common
  git commit -am "Remove a submodule" && git push
 
  | 
 
帮助
1 2 3 4 5 6 7 8
   |  git help
 
  git help -a
 
  git help -c
 
  | 
 
其他
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   |  git --version
 
  git remote -v
 
  git config --global credential.helper store
 
 
  git credential-manager uninstall
  git config --global credential.helper ""
  git config --global --unset credential.helper
 
  git rm -r --cached .
 
  | 
 
附上一张鹅厂的 git 思维导图

License
MIT