Git是分布式控制系统,没有中央服务器。每个人的电脑就是一个完整的版本库。工作时候无需联网,只需要提交代码时候把各自的修改推送给对方即可。
SVN与Git不同:
- SVN是属于集中管理方式,可以一定程度掌握其他开发人员进度。
- SVN服务器单点故障,容错性较差。服务器挂掉,影响其他开发。
- Git属于分布式管理,其他开发不受影响。

【命令】
- clone(克隆):从远程仓库中克隆代码到本地仓库
- checkout(检出):从本地仓库检出一个仓库分支然后进行修订
- add(添加):在提交之前先将代码提交到暂存区
- commit(提交):提交到本地仓库。本地仓库中保存了修改过的各个历史版本
- fetch(抓取):从远程仓库抓取到本地仓库,不做任何合并操作,一般操作比较少。
- pull(拉取):从远程仓库拉取到本地,自动进行合并(merge),然后放到工作区,相当于fetch + merge
- push(推送):修改完成后,将代码推送到远程仓库
Git安装
下载完成,修改安装路径,一路next即可。 
- Git GUI:Git提供的图形界面工具。
- Git Bash:Git提供的命令行工具。
基本配置
- 打开Git Bash
- 设置用户信息
git config --global user.name "王涵" git config --global user.name "wwh_work@126.com" - 查看配置信息
git config --global user.name git config --global user.email 
解决GitBash乱码
- 打开Git Bash执行命令
git config --global core.quotepath false - ${git_home}/etc/bash.bashrc文件最后加入下面两行
export LANG="zh_CN.UTF-8" export LC_ALL="zh_CN.UTF-8"
获取本地仓库
- 选择一个空白文件夹
- 右键打开Git Bash窗口
- 执行命令
git init - 创建成功后可以在文件夹下看到隐藏的.git目录
 
基础操作命令
状态转换
git add :从工作区添加到暂存区git commit :从暂存区提交到本地仓库
查看修改的状态
添加工作区到暂存区(add)
git add 文件名 :从工作区添加指定文件到暂存区git add . :从工作区添加全部文件到暂存区
从暂存区提交到本地仓库(commit)
查看提交日志
git log --all :显示所有分支git log --pretty=online :将提交信息显示为一行git log --abbrev-commit :将输出的commitId变得更简短git log --graph :以图的形式显示  
版本回退
git reset --hard commitId
分支
加入我们需要对代码进行一个阶段性的打包,又不想影响业务继续进行。例如:一期项目上线,创建分支,继续开发二期项目,如果一期项目存在bug,切换分支到一期,可以继续修改,打包等
查看分支
git branch 
创建本地分支
切换分支
创建并切换分支
合并分支
删除分支
不能删除当前分支,只能删除其他分支
git branch -d b1 :删除分支时,需要做各种检查git branch -D b1 :不做任何检查,强制删除
操作远程仓库
添加远程仓库
git init 初始化本地仓库git remote add <远程仓库><仓库路径> git remote add origin git@172.16.60.112:test/git_test.git
查看远程仓库
git remote 
推送到远程仓库
git push origin master :如果远程分支和本地分支相同,则可以只写本地分支。git push origin master -f :强制覆盖。git push --set-upstream origin master :推送到远程的同时建立和远程分支的关联关系。
查看本地分支与远程分支关联关系
git branch -vv 
从远程仓库克隆
git clone <仓库路径>[本地目录] git clone http://172.16.60.112/test/git_test.git
IDEA中使用Git
在IDEA中配置Git
File-->Settings 打开设置窗口,找到Version Control 下的git 选项,选择到自己的git.exe 文件 
克隆远程仓库到本地
Git 下选择Clone  
创建分支
右下角点击master-->new Branch
或者 打开Git 历史,选择一条历史,右键选择New Branch

切换分支

|