一、Git配置
1.查看配置信息
git config --list
2.Git用户信息
git config --global user.name "xxx"
git config --global user.email "xxx@xx.com"
3.文件编辑器
git config --global core.editor emacs
4.差异化分析工具
git config --global merge.tool vimdiff
二、Git对文件的管理
1.获得Git仓库两种途径
1.新建Git仓库
#1.初始化新的仓库
git init
#2.跟踪所有文件
git add *
2.克隆一个仓库
git clone [url]
Git管理文件变化周期: untracked unmodified modified staged
1.查看文件状态
git status
2.添加 追踪/暂存 文件
git add xxx
3.提交本地仓库
git commit -m '描述'
#跳过暂存直接提交到本地仓库
git commit -a -m '描述'
4.从追踪清单删除
#1.删除文件和追踪清单
git rm xxx
#2.仅删除追踪清单
git rm --cache xxxx
5.文件改名
git mv xxx_from xxx_to
撤销操作
1.重新提交
git commit -amend
#重新commit,覆盖上一次commit
git commit --amend -m “commit描述信息”
2.取消已暂存的文件
git reset HEAD xxx
3.取消对文件的修改
git checkout xxxx
三、远程仓库管理
1.克隆远程仓库到本地仓库
git clone [url]
git clone git@github.com:jirengu/blog.git
2.查看当前的远程库
#远程仓库简短名
git remote
#显示克隆地址
git remote -v
2.添加远程仓库
#添加远程库,并指定一个简短名
git remote add [remote-name] [url]
3.从远程仓库抓取数据
git fetch [remote-name]
4.推送数据到远程仓库
git push [remote-name] [branch-name]
5.查看远程仓库信息
git remote show [remote-name]
6.远程仓库的删除和重命名
git remote rm [remote-name]
#或者
git remote rename [remote-name] [new-remote-name]
四、分支管理
1.查看分支
git branch
#本地和远程分支
git branch -a
2.创建和切换分支
#1.创建分支
git branch [branch-name]
#2.切换分支
git checkout [branch-name]
#或者
#创建并切换分支
git checkout -b [branch-name]
3.合并分支
#切换分支
git checkout master
#将hotfix分支合并到master分支
git merge hotfix
4.合并分支冲突,解决冲突办法就是二选一
#1.查看冲突文件
git status
#2.手动修改合并冲突文件
git mergetool
#3.提交缓存
git add
#4.提交
git commit -m 'xxxx'
#5.合并
git merge hotfix
5.查看与当前分支(未)合并的分支
git branch --mereged
git branch --no-mereged
6.删除分支
git branch -d xxxx
#强制删除
git branch -D xxxx
#批量删除本地分支
git branch | grep -E 'fixbug|feature' | xargs git branch -D
7.分支回退到某次commit
#本地分支回退到上N次提交
git reset --hard HEAD~2
git reset --hard HEAD~N
#本地分支回退到 指定commit的sha码
git reset --hard commit_id
#加入-f参数,强制提交到远程仓库,将远程分支回退到reset版本
git push -f origin release-0.1.0
git push -f origin release-0.11.5
五、远程分支
1.克隆远程仓库到本地仓库
git clone [url]
2.从远程仓库抓取数据
git fetch [remote-name]
3.推送本地分支
git push [remote-name] [branch-name]
git push [remote-name] [branch-name-local]:[branch-name-remote]
4.追踪远程分支
git checkout -b [branch-name-local] [remote-name]/[branch-name-remote]
5.本地关联远程分支
git branch --set-upstream-to=[remote-name]/[branch-name-remote] [branch-name-local]
6.删除远程分支
git push [remote-name]:[branch-name-remote]