Git 版本控制器

配置

  1. git config --list #查看全局配置的信息
  2. git config --global -l #查看全局配置
  3. git config --system -l #查看系统配置
  4. git config --local -l #查看本地仓库配置
  5. git config --global user.name Nick #设置用户名
  6. git config --global user.email nick_php@163.com #设置邮箱
  7. git config --global log.date format:'%Y-%m-%d %H:%M:%S' #时间显示格式修改
  8. git config --global core.ignorecase false #配置文件目录区分大小写
  9. git config --global core.editor vim #配置 vim 为编辑器
  10. git config --global color.ui true #配置高亮
  11. git config --global merge.tool vimdiff #配置差异分析工具
  12. git help config #获取帮助信息

快捷别名

  1. alias gs='git status'
  2. alias gm='git merge'
  3. alias gtag='git tag'
  4. alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -- | less"
  5. alias gb='git branch'
  6. alias gpull='git pull'
  7. alias gpush='git push'
  8. alias ga='git add -A'
  9. alias gc='git commit -m'
  10. alias gam='git commit -am'
  11. alias gcheckout='git checkout'
  12. alias glt='git describe --tags' #最新的 tag 名称,提交次数

普通操作

  1. git status #查看已添加到暂存区文件状态
  2. git diff #查看未添加暂存区文件状态
  3. git add file #添加文件
  4. git add . #添加当前目录下所有文件到版本库
  5. git add -A #添加所有文件到版本库
  6. git commit -m '注释' #提交
  7. git commit -am '提交信息' #添加并提交信息
  8. git commit --amend #修改最近一次的 commit 信息
  9. git blame filename #查看文件修改记录
  10. git push #推送到仓库
  11. git pull #拉取当前分支仓库最新代码
  12. git pull dev #拉取指定分支的代码与本地分支合并
  13. git reset --hard #回滚到上版本
  14. git reset --hard af442cb672b02cdfca1fcb #回滚到指定的版本
  15. git reset --hard origin/master #将本地代码重置和远程仓库代码一致,避免产生一个新个合并日志
  16. git reset --hard origin/api #将本地代码重置与远程仓库指定分支一致,避免产生一个新个合并日志
  17. git revert af442cb67 #回退到某个提交的代码,新增一个 commit
  18. git checkout . #恢复暂存区的所有文件到工作区
  19. git checkout file #恢复暂存区的指定文件到工作区
  20. git checkout af442cb672b02cdfca1fcb index.php 恢复某个 commit 的指定文件到暂存区和工作区
  21. git rebase -i af442cb672 将多个已提交未推送的 commit 合并成一个,把 pick 改成 s 保存之后修改提交信息即可
  22. git rebase --abort 撤销合并

新建仓库

  1. mkdir www && cd www
  2. git init #初始化
  3. git status #查看文件状态
  4. git add file #.或*代表全部添加
  5. git commit -m "message"#此处注意乱码
  6. git remote add origin git@github.com:username/project.git #关联远程仓库
  7. git push -u origin master #第一次推送文件到远程仓库

克隆仓库

  1. git clone https://github.com/Nick233333/gitbook.git #克隆远程仓库
  2. git clone https://github.com/Nick233333/gitbook.git linux #克隆并指定目录名称

日志

  1. git log #查看提交日志
  2. git reflog #查看提交日志 git reset 回滚版本依然有日志,可以撤销回滚
  3. git log --pretty=oneline #单行显示提交日志
  4. git log --author=nick #查看指定用户的日志
  5. git log -p filename #查看文件每次提交的修改部分

分支

  1. git branch #查看本地分支
  2. git branch -r #查看远端分支
  3. git branch -a #查看所有分支
  4. git branch test #新建 test 分支
  5. git checkout -b dev ##新建 dev 分支并切换
  6. git checkout - #切换到上一个分支
  7. git checkout -b test dev #基于 dev 新建 test 分支,并切换
  8. git merge test #将test分支合并到当前分支
  9. git branch -m old new #重命名分支
  10. git branch -M old new #强制重命名分支
  11. git push origin branch #推送分支到远程
  12. git branch -d branch #删除本地分支
  13. git branch -D branch #强制删除本地分支(当分支内容有修改并且已经 commit 时,分支没有合并,需要强制删除)
  14. git push origin :branch #删除远程分支
  15. git branch -r -d branch #删除远程分支,没有执行此命令 git branch-r 还是能看到远程分支

标签

  1. git tag #列出现有标签
  2. git tag v0.1#新建标签
  3. git tag -a v0.1 -m 'my version 1.4'#新建带注释标签
  4. git checkout tagname #切换到标签
  5. git push origin v1.5 #推送分支到源上
  6. git push origin --tags #一次性推送所有分支
  7. git tag -d tag #删除本地 tag
  8. git push origin :tag #删除远程 tag

关联远程仓库

  1. git remote -v #查看全部远程仓库
  2. git remote add origin https://github.com/Nick233333/gitbook.git #添加本地仓库与远程仓库关联
  3. git remote rename origin github #重命名
  4. git remote remove origin #取消与远程仓库关联

保持 fork 之后的项目和源仓库同步

  1. git remote add upstream [upstream_url] #1、添加上游仓库命名为 upstream
  2. git fetch upstream #2、拉取最新代码
  3. git checkout master #3、切换分支
  4. git merge upstream/<branch_name> #4、将 upstream/<branch_name> merge 到本地当前分支
  5. git push origin <branch_name> #5、push 到自己的 github 仓库

暂存代码

  1. git stash #将本地修改代码暂存
  2. git stash save 'message' #暂存代码并添加备注
  3. git stash pop #把最近的暂存代码还原
  4. git stash pop stash@{0} #还原指定 id 的暂存代码
  5. git stash list #查看暂存代码列表
  6. git stash drop #删除最近的暂存代码
  7. git stash drop stash@{0} #删除指定 id 的暂存代码
  8. git stash clear #清空所有暂存代码

实用不常用命令

  1. git shortlog -sn #查看成员提交次数
  2. git whatchanged --since='2 weeks ago' #查看指定时间的提交记录
  3. git diff --name-only --diff-filter=U | uniq | xargs $EDITOR #查看所有冲突文件
  4. #删除最近一次提交中文件并修改提交信息
  5. git rm —-cached file_name
  6. git commit —-amend