写在前面
一开始我们先聊一聊版本控制,什么是版本控制呢?
版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统。具体大类分为:
- 本地版本控制系统
- 集中式版本控制系统SVN
- 分布式版本控制系统 Git
Why Git ?
为什么我们要食用 Git 呢?来看看他的优势吧!
Git 诞生于2005,Linux开源社区
- 速度
- 简单的设计
- 允许上千个并行分支
- 分布式
都是他的优点。
在进行简单的介绍之后,接下来我们就可以开始进行食用了。
下载、安装和配置
去到https://git-scm.com/官网下载即可

在终端中使用 git --version
查看git的版本

使用 git config --global user.name ""
和 git config --global user.email ""
创建用户和邮箱
使用 git config --list (或git config -l)
查看用户列表


创建"仓库"

在IDEA的Terminal中直接输入git init
用ls -la
查看就发现了目录文件下面已经存在了.git的文件,这些文件是隐藏的。
使用git init
直接加名称,在创建文件的同时进行"仓库"的搭建

或者从github上直接克隆、下载到git
复制地址之后 使用git clone https://github.com/xxxxx
进行克隆到本地

基本用法
git status ==> 查看仓库状态
下图显示No commits yet
,说明还没有历史节点,并说明了以下文件都属于比较危险的状态
所以我们需要进行新建


git add . ==> 将所有文件添加至暂存区
之后再次使用git status
命令查看状态,发现之前红色的文件都变色了,说明他们暂时安全了

git commit -m "版本描述" ==> 提交版本或备注描述信息
在提交完版本描述之后,我们再次使用git status
查看状态,提示我们nothing to commit,working tree clean
。说明所有的更改都保存了
在这个时候,也说明。我们已经有一颗"后悔药"了!,即是我们已经有一个可回溯节点了。

git log ==> 查看详细日志信息
git log -p
可以查询具体修改了什么
在输入 git log
之后,我们可以看到 commit 之后有很长一段字符,可以理解为这个节点的"名字"、"身份证号",用来绝对定义这个节点。

回溯!吃"后悔药"!
git reset --hard 456ba1a84e007da35e5e611737aeec9de2581932
(commit后面的"身份证",ps:7位以上即可)

如上图,我希望将 blog demo v1.2 版本的代码回溯到 v1.1 的时候
输入 git reset --hard 456ba1a84e007da35e5e611737aeec9de2581932
之后可以看到提示
HEAD is now at cca2043 blog demo v1.1
版本

点击Version Control也可以查看版本间的区别和改动

如果有文件被误删掉,可以使用git checkout
来找回
git checkout xxx
(误删的文件名)
三种状态

modified 已修改 ==> staged 已暂存 ==> committed 已提交
staged 是缓冲阶段,以防错误引入了较大的文件,防止错误的引入

tag标签
我们的代码版本,有小修小改的过程,但当我们的代码大版本确立的时候,就相当于一个里程碑节点,这个时候就需要tag标签用来注解
git tag -a 标签名 -m "备注"
并使用 git tag
查看有多少tag标签
使用git show
查看 tag 标签的详细信息,诸如是谁添加的,添加了什么内容等

如果之前有小的版本我也想用来添加tag标签,但他们的时间节点已经是过去式了,怎么办呢?
这时使用 git tag -a v0.5 -m "备注" + "身份证号"
可以给之前历史记录的回溯节点添加tag标签
我们也可以使用 git checkout v1 来回溯名称为v1的tag标签节点
分支与合并分支
类似于在不同的时间线创造一个平行世界,在一个节点创造出一个分支
使用 git branch "分支名称"
创建分支
在项目中,需要进行分支开发之后再整合功能的时候需要用到分支
分支也可以用来处理一个节点的 bug 问题,找到中间的节点 不影响后面正在开发的时间线,可以用来"救火"

在 master下使用 git merge "分支名"
用来合并分支
然后需要在IDEA中手动删除多余的关于提示的代码 "========="
分支 救火之前...

合并分支 救火之后...

远程仓库
在 local 本地创建一套仓库,并且在很远很远的其他地方远程备份另一套仓库,内容都是一样的,甚至可以在添加后续节点的时候,也一样同步过去。

在github上我们创建一个远程仓库 并获得一个url地址



git remote add "远程名称" "远程地址url"
例如这里 我们使用 git remote add github https://github.com/evenyao/easy-blog-demo.git
git remote 可以显示所有连接的远程仓库

使用 git push -u "远程名" "分支名"
上传代码
并确认github用户名/密码

然后我们刷新 github 页面,发现代码已经上传上去了,也可以看到之前我们创建的仓库版本

然后在需要容灾的时候 使用 git clone "远程仓库url" 进行拷贝
多人远程协作
git clone "远程仓库url" "用户标记"
例如git clone https://github.com/evenyao/easy-blog-demo.git even
git clone https://github.com/evenyao/easy-blog-demo.git root
在 even 和 root 的协作过程当中,even修改了文件,添加了工程中的一项代码
在even进行git add. && git commit -m "v1.1"
添加版本上传之后,root需要使用 git pull 将even修改之后代码拖下来
然后再次进行修改,并也使用git add. && git commit -m "v1.2"
添加版本上传之后,even之后也使用 git pull 将root修改的代码拖下来
SSH
当我们创建一个新github项目,也可以使用https地址作为克隆的站点地址
另外一种方式就是SSH,但这种方法必须要进行私钥公钥的配置

我们先点击头像 选择Settings

选择左边栏个人设置中的SSH and GPG keys

在 SSH keys 中 可以点击 generating SSH keys 查看指引
新建 则是选择 New SSH key

在Generating a new SSH key and adding it to the ssh-agent中可以查看完整的创建流程

创建流程指引

演示操作
使用
ssh-keygen -t rsa -b 4096 -C "invictus@vip.qq.com"
开始进行设置流程
创建完毕之后我们
cd
到这个目录 ls
查看当前 .ssh 文件夹下面的文件 显示有 id_rsa
和 id_rsa.pub

使用cat id_rsa.pub
命令显示出公钥的相信信息
并将下面的公钥全部复制

将其粘贴到 SSH key 栏中,然后选择 Add SSH key (当然也不要忘了添加Title)

这样一个 SSH key 就算添加完成了
我们也可以通过 git 进行ssh的 clone 和 push了

小插曲
当在git push
easy-blog-demo-v2.0 这个项目的时候,出现了该图的报错,查询之后发现是因为本地仓库和远程仓库不同步导致的报错。提示中的 git pull ...
为我们指明了方向
于是我们 git pull
发现了 git branch --set-upstream-to=origin/<branch> master
我们输入git branch --set-upstream-to=origin/master
之后
再一次进行 git push
就成功了

总而言之
总而言之的意思就是 其实上面的东西都可以不看...
一般使用的时候的操作
- 先
cd Desktop
到桌面,然后git clone
将项目clone
到本地
cd Desktop
git clone https://github.com/evenyao/xxx-xxx.git
- 待进行修改、添加、删除完毕之后,重新
push
cd Desktop
cd "xxx-xxx" //cd到该项目文件夹
git init
git add .
git commit -m "null"
git remote add origin https://github.com/evenyao/xxx-xxx.git
git push origin master
在输入git remote add origin https://github.com/evenyao/xxx-xxx.git
的时候如果出现报错fatal: remote origin already exists.
输入git remote rm origin
之后,再重新进行git remote add origin https://github.com/evenyao/xxx-xxx.git
,然后git push origin master
即可。
如果出现! [rejected] master -> master (fetch first)
报错,重新clone之前的github项目,然后将改动的文件夹覆盖进去。再重新进行上述操作即可。
网友评论