Git使用
文章目录
1.什么是git
1.1 Git是一个分布式的版本控制软件
- 软件,类似于QQ、office等安装到电脑上才能使用的工具
- 版本控制,类似于毕业论文、写文案、视频剪辑等,需要反复修改和保留原历史数据
- 分布式
- 文件拷贝
- 本地版本控制
- 集中式版本控制
- 分布式版本控制
1.2 为什么要做版本控制
要保留之前的所有版本,以便回滚和修改
2.安装Git
详见:https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git
安装git软件有很多种方式:
1.windows
https://git-scm.com/download/win
2.linux
[root@git ~]# yum install git -y
3.macos
http://git-scm.com/download/mac
3.git status 没有颜色显示
在Windows系统中安装了git客户端之后在git bash中用git命令 git status,git diff等时,修改过的文件自动显示为红色。在linux中git安装后颜色是不自动设置的。
下面的命令设置git的颜色:
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git的基本使用
初始化代码目录:git init
配置个人信息:用户名、邮箱
git config --global user.email "you@example.com"
git config --global user.name "Your name"
提交文件:
git add .
git commit -m "注明提交了什么"
修改文件名称:
git mv file1 file4
git status
查看文件前后的变化 git diff
本地目录和暂存区的比对:git diff file
暂存区和仓库的比对:git diff --cached file
回退:
案例1:本地目录----->暂存区(本地文件误操作未提交至暂存区进行回退):
> file
git status
git checkout file 暂存区的目录覆盖本地工作目录内容(如 >file清空了文件内容)
案例2:本地仓库覆盖---暂存区覆盖-->本地仓库(本地文件误操作已提交至暂存区进行回退):
> file #误操作清空文件
git add .
git status
回退
git reset HEAD file
git status
git checkout -- file
git status
cat file
案例3:如果多次提交内容到远程仓库,怎么回退?
echo "111" >file
git add .
git commit -m "增加了111"
echo "222" >file
git add .
git commit -m "新增222"
问题:要退到111的状态,怎么办?
git log --oneline #找到commit id
git reset --hard commit id
#查看所有提交的历史记录
git reflog
#查看分支
git branch
#创建dev分支
git branch dev
#切换分支
git checkout dev
#查看分支
git branch
* dev #带*表示为主分支
master
#在dev分支合并master
git merge master -m "dev合并master内容"
#删除dev分支
git branch -d dev
git标签的使用
#打标签
git tag -a "v1" -m "描述信息"
#查看标签
git tag -l
#查看tag中有什么
git show v1.0
#指定commit id打标签
git tag -a "v1.1" commit id -m "描述信息"
#删除标签
git tag v1.0 -d
4."抖音创业史"一个人写代码
想要让git对一个目录进行版本控制需要以下步骤:
- 进入要管理的文件夹
- 执行初始化命令
git init
- 管理目录下的文件状态
git status
#注意:新增的文件和修改过后的文件都是红色
- 管理制定文件(红变绿)
#第一阶段:写一个index.html
[root@git dy]# vim index.html
<h1> 抖音初创阶段 </h1>
git add 文件名
git add .
- 个人信息配置:用户名、邮箱[一次即可]
git config --global user.email "you@example.com"
git config --global user.name "Your name"
- 生成版本提交文件
#提交文件加上注释
[root@git dy]# git commit -m "一个月完成了抖音前端页面"
- 查看版本记录
[root@git dy]# git log
#第二阶段,新增短视频功能
[root@git dy]# vim index.html
<h1> 抖音初创阶段 </h1>
<h1> 增加了短视频功能 </h1>
[root@git dy]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@git dy]# git add .
[root@git dy]# git commit -m "增加了短视频功能"
[master 53ed846] 增加了短视频功能
1 file changed, 1 insertion(+)
[root@git dy]# git log
commit 53ed846c48416465bb6d7135ad8f82217f495238
Author: Your Name <you@example.com>
Date: Mon Sep 6 21:37:15 2021 +0800
增加了短视频功能
commit 218a516d637e4960f732fe62ef08ef7a46e1a14b
Author: Your Name <you@example.com>
Date: Mon Sep 6 21:31:29 2021 +0800
一个月完成了抖音前端页面
- git回退
#第三阶段:增加了约功能
[root@git dy]# vim index.html
<h1> 抖音初创阶段 </h1>
<h1> 增加了短视频功能 </h1>
<h1> 增加了约功能 </h1>
[root@git dy]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@git dy]# git add .
[root@git dy]# git commit -m "增加了约饭功能"
[master dff11c1] 增加了约饭功能
1 file changed, 1 insertion(+)
#因为增加约功能,被有关部门查处,需要回退
- git回退
#回退命令
git reset --hard 版本标记
[root@git dy]# git log
commit dff11c12c15c1af69db31e85c82f79699f67f176
Author: Your Name <you@example.com>
Date: Mon Sep 6 22:48:27 2021 +0800
增加了约饭功能
commit 53ed846c48416465bb6d7135ad8f82217f495238
Author: Your Name <you@example.com>
Date: Mon Sep 6 21:37:15 2021 +0800
增加了短视频功能
commit 218a516d637e4960f732fe62ef08ef7a46e1a14b
Author: Your Name <you@example.com>
Date: Mon Sep 6 21:31:29 2021 +0800
一个月完成了抖音前端页面
[root@git dy]# git reset --hard 53ed846c48416465bb6d7135ad8f82217f495238
HEAD is now at 53ed846 增加了短视频功能
[root@git dy]# cat index.html
<h1> 抖音初创阶段 </h1>
<h1> 增加了短视频功能 </h1>
[root@git dy]#
- 查看历史记录
[root@git dy]# git reflog
#因为一些原因,又可以上下约功能
[root@git dy]# git reflog
53ed846 HEAD@{0}: reset: moving to 53ed846c48416465bb6d7135ad8f82217f495238
dff11c1 HEAD@{1}: commit: 增加了约饭功能
53ed846 HEAD@{2}: commit: 增加了短视频功能
218a516 HEAD@{3}: commit (initial): 一个月完成了抖音前端页面
[root@git dy]# git reset --hard dff11c1
HEAD is now at dff11c1 增加了约饭功能
[root@git dy]# cat index.html
<h1> 抖音初创阶段 </h1>
<h1> 增加了短视频功能 </h1>
<h1> 增加了约功能 </h1>
5.git命令总结
git init #初始化一个目录为版本库
git add #将没有被管理的文件,加入git进行管理
git commit #将内容提交到版本库中
git log #查看提交的历史记录
git reflog #查看所有的历史提交记录
git reset --hard 版本号 #回退到指定的提交版本记录
6.商城&&紧急bug修复
6.1 分支
分支可以给使用者提供多个环境可以开发,意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。
6.2 紧急bug修复方案
命令总结
- 查看分支
git branch
- 创建分支
git branch 分支名称
- 切换分支
git checkout 分支名称
- 分支合并(可能产生冲突)
git merge 要合并的分支名称
#注意切换分支在合并
- 删除分支
git branch -d 分支名称
- 查看历史记录
[root@git dy]# git reflog
#图形记录展示
[root@git dy]# git log --graph --pretty=format:"%h %s"
6.3 工作流
7.github命令总结
-
上传代码
-
给远程仓库起名
git remote add origin 远程仓库地址
git remote -v #查看远程连接地址
- 向远程推送代码
git push origin 分支
- 在新电脑上第一次获取代码
#克隆远程仓库代码
git clone 远程仓库地址(内部已实现git remote add origin 远程仓库地址)
- 切换分支
git checkout 分支
- 下载代码
git clone 地址
- 在新电脑上进行开发
1. 切换到dev分支进行开发
git checkout dev
2. 把master分支合并到dev(仅一次)
git merge master
3. 修改代码
4. 提交代码
git add .
git commit -m 'xx'
git push origin dev
回老电脑上继续写代码
5. 切换到dev分支进行开发
git checkout dev
6. 拉代码
git pull origin dev
7. 继续开发
8. 提交代码
git add .
git commit -m 'xx'
git push origin dev
9. 添加远程连接(别名)
git remote add origin 地址
git remote -v
10. 拉取代码
git pull origin dev
等价于
git fetch origin
git merge origin/dev
- 加入github仓库的工作流
8.git标签
8.1 git标签作什么用
当Git仓库内的数据有改善或者功能更新时,我们经常 会打一个类似于软件版本号的标签tag,这样通过标签就可以将版本库中的 某次commit给记录下来,便于我们后续将特定时期的数据取出来用。简单 来说:标签也是版本库的一个快照。
8.2 为什么要使用git标签
Git有commit,为什么还要引入tag?“请把上周一 的那个版本打包发布,commit号是6a5819e…”, “一串乱七八糟的数字不好 找!”如果换一个办法: “请把上周一的那个版本打包发布,版本号是v1.2”。按照 tag v1.2查找commit就行!”所以,tag就是一个让人容易记住的名字,它跟某个commit 绑在一起
8.3 git标签的基本使用
- 1.对当前最新提交的代码创建标签,-a标签名称,-m标签描述
#1.将当前最新代码进行标签
git tag -a "v1.1" -m "描述信息"
#2.创建标签,指定commitID
git tag -a v1.0.1 30e2840 -m "Messages
- 2如何查看刚才打的标签
git tag #git log -l
v1.0.0
v1.0.1
- 3.提交标签到github
#提交所有tag到github
git push origin --tags
#提交某一个tag到github
git push origin v1.1
#删除标签
git tag -d v1.1
git push origin --tags
9.免密登录
- URL中体现
原来的地址:https://github.com/oldxu/treenb.git
修改的地址:https://用户名:密码@github.com/oldxu/treenb.git
git remote add origin https://用户名:密码@github.com/oldxu/treenb.git
git push origin master
- ssh实现
1. 生成公钥和私钥(默认放在 ~/.ssh目录下,id_rsa.pub公钥、id_rsa私钥)
ssh-keygen
2. 拷贝公钥的内容,并设置到github中。
3. 在git本地中配置ssh地址
git remote add origin
git@github.com:WuPeiqi/dbhot.git
4. 以后使用
git push origin master
- git自动管理凭证
10.git忽略文件
让Git不再管理当前目录下的某些文件。.gitigonore
通常情况下有如下文件可能需要忽略
vi .gitigonore
#里面写需要忽略的文件
git add .
git commit -m "XXXXXX"
#不会将想要忽略的文件上传到gitlab
-
1.程序运行时产生的垃圾文件
-
2.程序运行时产生的缓存文件
-
3.程序本地开发使用的图片文件
-
4.程序连接数据一类的配置文件
*.h
!a.h
files/
*.py[c|a|d]
更多参考:https://github.com/github/gitignore