一、Git的安装
1、Windows
下载安装包,安装目录可默认,也可自定义,其他直接默认下一步。
2、Linux
//安装git软件
[johnny@localhost ~]$ yum install -y git
//查看git版本
[johnny@localhost ~]$ git --version
//查看git帮助
[johnny@localhost ~]$ git --help
二、Git本地仓库创建及初始化
# 项目不存在,初始化
[johnny@localhost ~]$ cd 某个文件夹
[johnny@localhost ~]$ git init your_project
[johnny@localhost ~]$ cd your_project
# 项目存在,初始化
[johnny@localhost ~]$ cd 项目所在文件夹
[johnny@localhost ~]$ git init
// 若有仓库目录下有.git,说明初始化成功
[johnny@localhost ~]$ ls -al
三、文件提交到本地仓库
// 修改本地仓库中的用户名和邮箱,便于更换电脑后还可以用自己的用户名和邮箱进行代码提交;
// 可以不是github的用户名和邮箱
[johnny@localhost ~]$ git config --global user.name "Johnny"
[johnny@localhost ~]$ git config --global user.email "johnny@163.com"
// 查看用户名和邮箱是否修改成功
[johnny@localhost ~]$ git config --list
// 提交文件到暂存区
[johnny@localhost ~]$ git add demo.txt
// 提交所有已修改文件到暂存区
[johnny@localhost ~]$ git add -u
// 查看当前的状态
[johnny@localhost ~]$ git status
// 提交txt文件到版本库
[johnny@localhost ~]$ git commit -m "提交txt文件"
// 制定文件名,提交txt文件到版本库
[johnny@localhost ~]$ git commit -m "提交txt文件" demo.txt
四、文件修改后提交
修改demo.txt文件内容
// 查看上个版本的不同,新添加了代码前有+号
[johnny@localhost ~]$ git diff
// 提交文件到暂存区
[johnny@localhost ~]$ git add demo.txt
// 提交全部
[johnny@localhost ~]$ git add .
[johnny@localhost ~]$ git add --all
// 查看当前的状态
[johnny@localhost ~]$ git status
// 提交修改后txt文件到版本库
[johnny@localhost ~]$ git commit -m "提交修改后的txt文件"
四、重命名
# 重命名法1:
[johnny@localhost ~]$ mv readme readme.md
[johnny@localhost ~]$ git status
[johnny@localhost ~]$ git add readme.md
[johnny@localhost ~]$ git rm readme
[johnny@localhost ~]$ git status
# 清理暂存区数据
[johnny@localhost ~]$ git reset --hard
# 重命名法2:
[johnny@localhost ~]$ git mv readme readme.md
五、版本回退
// 回退到上个版本
[johnny@localhost ~]$ git reset --hard HEAD^
// 回退到上上个版本
[johnny@localhost ~]$ git reset --hard HEAD^^
……
// 回退到上n个版本
[johnny@localhost ~]$ git reset --hard HEAD~n
// 查看所有的提交历史
[johnny@localhost ~]$ git reflog
8d9fad6 HEAD@{0}: commit: add app flask
23b3a46 HEAD@{1}: commit: push the floder venv
2976e1e HEAD@{2}: merge dev: Fast-forward
0116b33 HEAD@{3}: checkout: moving from dev to master
……
// 回退到指定版本,XXX可以是8d9fad6、23b3a46……
[johnny@localhost ~]$ git reset --hard XXX
// 修改了但不想提交用此命令
[johnny@localhost ~]$ git checkout -- demo.txt
[johnny@localhost ~]$ git reset HEAD demo.txt
[johnny@localhost ~]$ git rm test.txt
[johnny@localhost ~]$ git commit test.txt -m "删除test.txt"
六、日志
// 查看历史
[johnny@localhost ~]$ git log
// -p,用来显示每次提交的内容差异;-2,仅显示最近两次提交
[johnny@localhost ~]$ git log -p -2
// 每次提交的简略的统计信息
[johnny@localhost ~]$ git log --stat
// 指定每个提交放在一行显示
[johnny@localhost ~]$ git log --pretty=oneline
# 简洁地查看提交历史
[johnny@localhost ~]$ git log --oneline
# 简洁地查看最近3次的提交历史
[johnny@localhost ~]$ git log -n3 --oneline
七、分支的操作
// 新建分支,等价于:git branch dev;git checkout dev
[johnny@localhost ~]$ git checkout -b dev
// 也可以创建并切换到新分支dev
[johnny@localhost ~]$ git switch -c dev
// 切换到分支office
[johnny@localhost ~]$ git checkout dev
// 查看所有分支
[johnny@localhost ~]$ git branch -a
// 删除分支dev
[johnny@localhost ~]$ git branch -d dev
// 查看当前分支
[johnny@localhost ~]$ git branch
[johnny@localhost ~]$ git add demo_branch.txt
[johnny@localhost ~]$ git commit -m "提交文件demo_branch.txt"
// 切换到主干分支master
[johnny@localhost ~]$ git checkout master
// 也可以用switch切换到主干分支master
[johnny@localhost ~]$ git switch master
// 合并分支到主干分支master
[johnny@localhost ~]$ git merge dev
八、上传github
1、在github上新建一个仓库
获取:git@github.com:XXX/flask_project.git
2、获取linux服务器的公钥
[johnny@localhost ~]$ pwd
// 用注册GitHub的邮箱生成公钥
[johnny@localhost ~]$ ssh-keygen -t rsa -C "XXX@163.com"
……(中间需要输入的操作可直接回车跳过)
[johnny@localhost ~]$ ls -al
……
drwx------. 2 johnny root 57 1月 7 16:05 .ssh
……
[johnny@localhost ~]$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXYicR149BjSGJ8DUDOYkkwnBh1aC2n53Di6JDN6q3Iiuijov7ILB2mgF18YKzNzhylybqBysIHzI+ryzBhhb/XXXXXXXXXs0sUKCrZ56vtIstWrTG9VstjLRAN0fGTGCX4H9320OF1ycwg9m+gAUozpiFQ2tWfwzU0kn+aYLcTXgEjA49dDfZg66kfThQgKpfZrf0l4GvmS14qC0r+DT1bHmm6QJ0z5oMfU8w9k51BI9wST0ObLhbYDJnfK2T/zgAgdJfkHufYOD8BdUevxHbnO6x2m0wNh3Zzn+2jgFTUdAsd3zWmLrEZt5i+rP4J+5PvbVqYhgIINBPDkQrd XXX@163.com
3、github添加的公钥
4、提交代码到github
// 添加远程仓库别名
[johnny@localhost ~]$ git remote add origin git@github.com:XXX/gtirepolist.git
// 查询别名
[johnny@localhost ~]$ git remote
// 删除别名
[johnny@localhost ~]$ git remote rm origion
// 给别名重命名
[johnny@localhost ~]$ git remote rename origion origion2
// 推到github上
[johnny@localhost ~]$ git push -u origin master
九、拉取代码
// 下载(克隆代码到本地)
[johnny@localhost ~]$ git clone git@github.com:XXX/gtirepolist.git
// 同步代码到本地,以防提交出现冲突报错
[johnny@localhost ~]$ git pull
// 提交文件到暂存区
[johnny@localhost ~]$ git add demo_master.txt
// 查看当前的状态
[johnny@localhost ~]$ git status
// 提交txt文件到版本库
[johnny@localhost ~]$ git commit -m "提交txt文件"
// 查看当前分支
[johnny@localhost ~]$ git branch
// 切换分支
[johnny@localhost ~]$ git check master
// 合并dev到master
[johnny@localhost ~]$ git merge dev
// 提交到master
[johnny@localhost ~]$ git push -u origin master