git使用方法
1.git配置
1.1)配置sshkey
生产key:ssh-keygen -t rsa -C "邮箱地址"
cd ~/.ssh(win下 cd .ssh)
复制id_rsa.pub的公钥内容到github网站中
1.2)配置多个git
cd ~/.ssh目录下
vim config
添加:Host/HostName/User/IdentityFile四项
![](https://img.haomeiwen.com/i17993869/bb55a2ab5e6b8998.png)
![](https://img.haomeiwen.com/i17993869/f948bebbf801e4b1.png)
IdentityFile此项填写各自的公钥路径不受影响,以下2个文件都是id_rsa.pub复制的
![](https://img.haomeiwen.com/i17993869/f68f790a0f9dc575.png)
2.使用方法
1.建立本地仓库
git init
2.将该目录下的所有文件全部提交:
3.编写文件
4.查看状态
git status
5.添加文件
git add .
如果只提交部分文件:
git add 文件名
这种提交是覆盖型提交
6.将文件提交到本地仓库
git commit -m "一些注释"
7.推送代码
git push
3.分支创建和删除操作
3.1) 查看本地分支:git branch
3.2) 查看本地和远程分支:git branch -a
![](https://img.haomeiwen.com/i17993869/152307131c3bee53.png)
3.3) 创建分支 :git checkout -b branch1
![](https://img.haomeiwen.com/i17993869/eb91330143b5449f.png)
3.4) 分支提交代码
git status
git add .
git commit -m "提交到分支代码"
git push --set-upstream origin branch1
![](https://img.haomeiwen.com/i17993869/ca5666177c948f10.png)
3.5) 删除分支操作
git branch -d branch1
删除分支前必须切换次分支才能删除
![](https://img.haomeiwen.com/i17993869/21bf8d80282cb164.png)
切换分支:git checkout master
![](https://img.haomeiwen.com/i17993869/bb1fc4d70d8b1afd.png)
此时查看分支是否删除
![](https://img.haomeiwen.com/i17993869/f60039082477a9d2.png)
必须执行 git branch -r -d origin/branch1
![](https://img.haomeiwen.com/i17993869/9ac8613170997f0c.png)
提交本次所有操作:git push origin:branch1
![](https://img.haomeiwen.com/i17993869/7b5276cf77cf5587.png)
git push origin:branch1提交后会报错,解决办法是
git push origin :branch1 (origin 和 : 之间加空格)
![](https://img.haomeiwen.com/i17993869/511a32ef3e4a5cab.png)
4.分支合并
4.1)checkout分支和提交分支代码
![](https://img.haomeiwen.com/i17993869/a9ace0c6e8ec6b82.png)
提交分支代码
![](https://img.haomeiwen.com/i17993869/eb1f3bac99d2860a.png)
![](https://img.haomeiwen.com/i17993869/49340f8130d4c0a8.png)
4.2)切换到master分支
![](https://img.haomeiwen.com/i17993869/d05b305bd0202148.png)
4.3)合并分支代码 :git merge mergedome
![](https://img.haomeiwen.com/i17993869/4c65c44329d374b2.png)
5.分支冲突解决
5.1)分支上修改代码提交
![](https://img.haomeiwen.com/i17993869/84cdd1f145e62402.png)
5.2)切换到master分支上修改同一处内容
![](https://img.haomeiwen.com/i17993869/9288615b076679fc.png)
5.3)master分支发起合并操作
![](https://img.haomeiwen.com/i17993869/2ffd4be8e16fb496.png)
5.4)解决冲突问题
![](https://img.haomeiwen.com/i17993869/5ba4a7d3d599be64.png)
![](https://img.haomeiwen.com/i17993869/e8a414ce4e8b8423.png)
6.git版本管理和版本回退
6.1)查看当前版本内容
![](https://img.haomeiwen.com/i17993869/7b43b238b18245e0.png)
6.2)版本回退:git reset --hard HEAD^
^每一个表示回退到上个版本
^^表示回退2个版本
![](https://img.haomeiwen.com/i17993869/9c76c441d7e0966a.png)
![](https://img.haomeiwen.com/i17993869/d6e6d33308f9e5f5.png)
另外一种写法:
![](https://img.haomeiwen.com/i17993869/3bcc5effeb02ed02.png)
根据版本好来回退版本,查看历史版本
![](https://img.haomeiwen.com/i17993869/c1af61e07b732670.png)
版本回退:git reset --hard " 版本号"
![](https://img.haomeiwen.com/i17993869/daf5ec845569a555.png)
7.变更文件名
![](https://img.haomeiwen.com/i17993869/a3b0f97108861bfd.png)
8.查看git历史提交信息
![](https://img.haomeiwen.com/i17993869/9b1079fa5c83cf90.png)
![](https://img.haomeiwen.com/i17993869/df434c4502176072.png)
![](https://img.haomeiwen.com/i17993869/7fd48888e8369919.png)
![](https://img.haomeiwen.com/i17993869/39117d60f3f367b5.png)
9.git切换分支代码
![](https://img.haomeiwen.com/i17993869/29d2b903c6f921e7.png)
![](https://img.haomeiwen.com/i17993869/7cfec22283a7ac74.png)
10.web帮助文档
![](https://img.haomeiwen.com/i17993869/c306ed02eafaed0b.png)
11.git自带图形管理工具(gitk --all )
![](https://img.haomeiwen.com/i17993869/63d974e4f6c67d9a.png)
![](https://img.haomeiwen.com/i17993869/758f313e36ad4c68.png)
![](https://img.haomeiwen.com/i17993869/46191688a7475be3.png)
![](https://img.haomeiwen.com/i17993869/694446f652e29d5a.png)
![](https://img.haomeiwen.com/i17993869/3261600fda7ffb0f.png)
12.分支操作
![](https://img.haomeiwen.com/i17993869/d7c1645c5557d6c8.png)
![](https://img.haomeiwen.com/i17993869/7e82e4491bfe9da5.png)
![](https://img.haomeiwen.com/i17993869/615193cf0846eb10.png)
13.修改最近一次提交备注
![](https://img.haomeiwen.com/i17993869/7f853f66dc073c0a.png)
![](https://img.haomeiwen.com/i17993869/2c14e43ec00134f3.png)
![](https://img.haomeiwen.com/i17993869/a7eddaf09934bcec.png)
![](https://img.haomeiwen.com/i17993869/8231420fddccc83a.png)
![](https://img.haomeiwen.com/i17993869/79451e271faf5eda.png)
14.恢复工作区
![](https://img.haomeiwen.com/i17993869/c044bccc9f442cb7.png)
15.回滚到摸一个commit
![](https://img.haomeiwen.com/i17993869/9fcdc1a77a981c47.png)
16.比较2个分支的差别
![](https://img.haomeiwen.com/i17993869/1ace2e92c9507e03.png)
![](https://img.haomeiwen.com/i17993869/e76445d35ba2ed79.png)
![](https://img.haomeiwen.com/i17993869/d1cd82395c80ade8.png)
17.删除文件
![](https://img.haomeiwen.com/i17993869/43f1d75b98e7fc23.png)
18.处理紧急任务流程
![](https://img.haomeiwen.com/i17993869/2dec3fe0170bdfcf.png)