Git 常用命令使用教程
Git 安装
Git安装配置--Centos/RedHat
$ yum install curl-devel expat-devel gettext-devel \ openssl-devel zlib-devel $ yum -y install git-core $ git --version git version 1.7.1
Git 日常使用
创建版本库
$ git init #初始化本地版本库
$ git clone <url> #克隆远程版本库
添加文件到git
$ git status #查看文件变更状态
$ git add <file> #跟踪指定的文件
$ git add <file> <file> #跟踪指定多个文件
$ git add . #跟踪所有改动过的文件
$ git commit -m "备注信息" #提交所有更新过的文件
$ git diff <file> #查看文件变更内容
$ git diff HEAD -- <file> #查看工作区与版本库最新版本的区别
$ git rm <file> #删除文件
查看提交历史
$ git log #查看提交历史 $ git log [-p] <file> #查看指定文件提交历史 $ git blame <file> #以列表形式查看指定文件提交历史 $ git log --pretty=oneline <file> #简洁方式查看提交历史 $ git reflog <file> #查看历史操作命令
撤销回退
$ git reset --hard HEAD^ #撤销到上一版本 $ git reset --hard HEAD #撤销工作目录中所有未提交文件的修改内容 $ git reset --hard 3628164 #撤销到指定版本号 git reflog <file> $ git checkout -- <file> #撤销到最近git commit或git add时的状态 #git add提交后怎么撤回到上一版本 $ git reset HEAD <file> #把暂存区的修改回退到工作区 $ git checkout -- <file>
分支合并
$ git branch #查看分支 $ git checkout -b <name> #创建+切换分支 $ git branch <name> #创建分支 $ git checkout <name> #切换分支 $ git merge <name> #合并某分支到当前分支 $ git branch -d <name> #删除分支 $ git branch -D #没有被合并的分支可以强行删除分支 $ git log --graph --pretty=oneline --abbrev-commit #查看分支合并图 $ git tag #查看所有标签 $ git tag <name> #打标签 $ git tag -d <name> #删除标签 $ git push origin :refs/tags/<name> #删除远程标签
bug分支解决方案
$ git stash #把当前工作现场“储藏”起来 $ git checkout master $ git checkout -b issue-101 $ git checkout master $ git merge --no-ff -m "merged bug fix 101" issue-101 #--no-ff参数表示禁用Fast forward $ git branch -d issue-101 $ git checkout dev $ git status $ git stash list $ git stash pop #恢复的同时把stash内容也删了,
远程操作
$ git remote #查看远程库信息 $ git remote -v #查看远程库详细信息 $ git remote add origin <url> #添加远程版本库 $ git push origin <branch> #推送分支 $ git clone <url> #抓取分支
创建SSH key
$ ssh-keygen -t rsa -C "youremail@example.com"