安装Git
- 配置全局用户及邮箱
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
- 配置当前仓库用户及邮箱
git config user.name "Your Name"
git config user.email "email@xx.com"
- 查看配置文件内容
git config --list
创建版本库
- 初始化(创建)版本库:
git init
- 把文件添加到版本库:
- 把文件添加到仓库:
git add readme.txt
- 把所有文件添加到仓库:
git add .
- 把文件提交到仓库:
git commit -m "wrote a readme file"
- 把文件添加到仓库:
时光机穿梭
-
查看仓库当前状态:
git status
-
查看文件修改:
git diff <file>
git diff #是工作区(work dict)和暂存区(stage)的比较 git diff --cached #是暂存区(stage)和分支(master)的比较 git diff HEAD -- readme.txt #查看工作区和版本库里面最新版本的区别
-
查看提交日志:
git log
git log --pretty=oneline
-
回退到上一个版本:
git reset --hard HEAD^
git reset --hard commit_id
-
查看命令日志:
git reflog
-
丢弃工作区的修改:
git checkout -- file
---用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”
切换到另一个分支:git checkout
-
撤销暂存区修改(unstage),重新放回工作区:
git reset HEAD file
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区 -
删除版本库文件:
git rm test.txt
git commit -m "remove test.txt"
远程仓库
- 查看远程库信息:
git remote
查看远程库详细信息:git remote -v
- 关联远程库:
git remote add origin git@server-name:path/repo-name.git
- 删除远程 git 仓库(关联反操作):
git remote rm origin
- 首次 push分支到远程仓库:
git push -u origin master
- 非首次推送到远程仓库:
git push origin master
- 从远程仓库克隆:
git clone git@server-name:path/repo-name.git
分支管理
- 查看分支:
git branch
- 创建分支:
git branch <name>
- 切换分支:
git checkout <name>
- 创建+切换分支:
git checkout -b <name>
- 合并某分支到当前分支:
git merge <name>
- 删除分支:
git branch -d <name>
- 产看分支合并情况:
git log --graph --pretty=oneline --abbrev-commit
用git log --graph
命令可以看到分支合并图。 - 禁止使用 fast forward merge 方式:
git merge --no-ff -m "xxx" <branch name>
- 保存现场:
git stash
- 查看保存的现场:
git stash list
- 恢复现场:
git stash pop
(同时删除stash内容)git stash apply
(stash内容并不删除) - 强制删除未合并分支:
git branch -D <branch name>
- 推送本地 master 分支:
git push origin master
- 创建本地 dev 并关联远程 dev 分支:
git checkout -b dev origin/dev
- 建立本地分支与远程分支得关联:
git branch --set-upstream branch-name origin/branch-name
- 抓取远程分支:
git pull
标签管理
创建
- 创建标签:
git tag <name>
- 查看标签:
git tag
- 对某一次 commit 打标签:
git tag v0.9 6224937
- 查看标签信息:
git show <tagname>
- 创建带有说明的标签:
git tag -a v0.1 -m "version 0.1 released" 3628164
- 用私钥签名标签:
git tag -s v0.2 -m "signed version 0.2 released" fec145a
操作
- 删除标签:
git tag -d v0.1
- 推送标签到远程:
git push origin <tagname>
- 推送本地所有未推送到远程的标签:
git push origin --tags
- 删除远程标签:
git push origin :refs/tags/<tagname>
自定义git
- 配置颜色开启:
git config --global color.ui true
- 检查
.gitignore
错误:git check-ignore -v App.class
- 配置别名:
git config --global alias.st status git config --global alias.co checkout git config --global alias.ci commit git config --global alias.br branch git config --global alias.unstage 'reset HEAD' git config --global alias.last 'log -1' git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
- 设置显示中文文件名:
git config --global core.quotepath false