Git命令使用手册
一、安装工具(windows环境)

Git-2.22.0-64-bit.exe下载:https://git-scm.com/downloads
TortoiseGit-2.8.0.0-64bit.msi下载:https://tortoisegit.org/download/
安装TortoiseGit出现错误:2502/2503错误

解决办法:

以管理员身份运行打开命令提示符,输入msiexec /package “安装包路径”,回车,就直接进入安装页面,按步骤安装即可。比如:
msiexec /package “D:\download\TortoiseGit-2.8.0.0-64bit.msi”
配置git.exe,选择git.exe,check,安装完成。
Android studio配置git:
File->setting->Version Control->GitHub

点击test验证连接是否成功。
二、设置git账号和相关属性
鼠标右键打开Git Base Here
git version 查看版本信息
git help 查看帮助
1.配置name 和 email
git config --global user.name "wuhuihui_home"

2.创建一个目录: mkdir 目录名
跳转到当前目录:cd 目录名
显示当前目录: pwd
wuhuihui@PC-201812110149 MINGW64 /d/testgit
$ git init Initialized empty Git repository in d:/testgit/
//这样就在d:/testgit/目录下自动生成.git文件夹,再在该目录下新建readme.txt,并输入“测试”保存,开始添加和提交readme.txt文件
wuhuihui@PC-201812110149 MINGW64 /d/testgit
$ git add readme.txt
wuhuihui@PC-201812110149 MINGW64 /d/testgit (master)
$ git commit -m "readme.txt" //日志信息
[master (root-commit) 8265127] readme.txt
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
wuhuihui@PC-201812110149 MINGW64 /d/testgit (master)
3.查看文件状态:git status
查看文件的不同:git diff

4.修改文件,在readme.txt文件中增加换行输入“1111”
提交修改后的文件需要先add,再commit
git add命令实际上就是把要提交的所有修改放到暂存区(Stage),而git commit 命令则是一次性把暂存区里面的东西一次性提交到分支(master),所以 git commit 只是负责把暂存区里面的东西提交。
git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别

5.查看提交版本日志:git log

6.版本回退:git reset
wuhuihui@PC-201812110149 MINGW64 /d/testgit (master)
$ git reset --hard HEAD^
HEAD is now at 8265127 readme.txt //回退完成,显示当前版本首id
^怎么输入:切换到英文输入法,shift+6
回退到上一个版本,回退上上一个版本^,以此类推,如果要回退到1000个版本之前用$ git reset --hard HEAD~1000
版本回退后,log日志仅保存到当前版本
那么此时readme.txt的内容成了“测试”
如果界面没有清空,可以通过git reflog命令查看之前每次提交的记录

如果我们想要把刚回退的版本恢复到最后提交的版本,可以通过
命令: git reset --hard 版本号,回到指定版本号

那么此时readme.txt的内容成了“测试 111”

用命令git reset HEAD <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区
版本log 发生变化

当文件已修改,但是文件没有add,想要回到修改前的内容,使用命令:git checkout -- readme.txt 让这个文件回到最近一次git commit或git add时的状态
7.删除文件rm file

rm file只是把缓存区的文件删除,并没有完全删除,在没有commit前是可以通过git checkout命令恢复该文件。
git checkout -- test.txt
8.新建远程仓库
①创建SSH Key。在C:\Users\Administrator\下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Git Bash,创建SSH Key:
ssh-keygen -t rsa -C "youremail@163.com"
连着几个回车


id_rsa和id_rsa.pub两个文件是SSH Key的秘钥对,密钥对最直观的作用:让你方便的登录到 SSH 服务器,而无需输入密码。由于你无需发送你的密码到网络中,SSH 密钥对被认为是更加安全的方式。id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
②登录GitHub:setting->

SSH and GPG keys->new SSH Key->

add new SSH keys->
title 随意命名,在Key文本框里粘贴id_rsa.pub文件的内容


Secure Shell (SSH) 是一个允许两台电脑之间通过安全的连接进行数据交换的网络协议。通过加密保证了数据的保密性和完整性。SSH采用公钥加密技术来验证远程主机,以及(必要时)允许远程主机验证用户。
GitHub允许添加多个Key。如果需要在不同电脑提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。
③在GitHub Create repository:testgit.git

③添加远程库origin
git remote add origin https://github.com/wuhuihui2016/testgit.git
把本地库的所有内容推送到远程库
git push -u origin master
由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。


那么初始化完成,现在开始本地提交通过命令: git push origin master就可以完成
在本地修改test.txt内容,增加文字

提交文件


④克隆项目到桌面:git clone https://github.com/wuhuihui2016/testgit.git

8.分支就像通道,通道多了,代码提交互不影响,工作效率会提高
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
①创建分支whh
git branch whh //创建分支whh
git checkout -b whh //创建分支whh并切换

此时分支指向whh,新建文件testwhh.txt,输入任意内容,使用add commit提交到当前分支上

这时你会发现,whh分支上包含有testwhh.txt三个文件,而master分支只有readme.txt test.txt两个文件,这时我们需要合并分支:git merge <name>
切换到master分支,合并whh分支

查看分支合并情况:git log --graph --pretty=oneline --abbrev-commit

合并失败出现问题时,需要解决冲突问题。
合并完成后就可删除whh了,其实就是删除一个指针,指针master 和 whh 指的是同一个地方,所以可以删除一个。
删除分支whh:$ git branch -d whh
git log --graph命令查看分支合并图
分支合并时通常会选择快进模式(Fast Forward),但是删除分支后会丢失分支信息,那么需要保持删除分支的信息,做一个新的提交(commit ),采用 --no -ff 的方式合并分支(git merge)
git merge --no-ff -m "git merger with no-ff mode" whh
查看远程库的信息:git remote
推送主分支:git push origin master.
推送link分支:git push origin whh.
⑨标签:是版本库的一个快照

使用命令看历史记录:git log --pretty=oneline --abbrev-commit

如果标签忘记打了,怎么办?
只需要找到需要打标签的commit id ,id号为:68186aa

git show <tagname> 查看标签详细信息
创建带说明的标签使用命令:git tag -a <name> -m <描述> <commit id>

删除标签:git tag -d <tagname>

推送标签到远程,使用命令:git push origin <tagname>

推送所有未推送的标签:git push origin --tag
从远程删除标签:git push origin :refs/tags/<tagname>

git的基本操作到这里就完成了。
参考文章:Git使用教程(Window)学习笔记 - 广源的博客 - CSDN博客
https://blog.csdn.net/qq_36243942/article/details/81169913
每天进步一点点(2019-06-13):
我的技术分享文集目录 https://www.jianshu.com/p/23485c157d76