一、Git概述
(1)定义
Git是目前世界上最先进的分布式版本控制系统。
(2)能干什么?
解决冲突、管理权限、代码备份、协同开发、版本还原、历史追查、版本记录、分支管理、代码审查
(3)集中管理型版本管理
经典的集中管理型(CVS、VSS、SVN)
特点:
- 实现了大部分开发中对版本管理的需求
- 结构简单,上手容易。
依然存在的问题
1、版本管理的服务器一旦崩溃,硬盘损坏,代码如何恢复?2、程序员上传到服务器的代码要求是完整版本,但是程序员开发过程中想做小版本的管理,以便追溯查询,怎么办?3、系统正在上线运行,时不时还要修改bug,要增加好几个功能要几个月,如何管理几个版本?4、如何管理一个分布在世界各地、互不相识的大型开发团队?
(4)Git如何解决
Git 工具
1、命令行工具:Git for windows
下载地址:https://git-for-windows.github.io/
2、 操作系统中可视化工具:TortoiseGit
下载地址: Windows Shell Interface to Git
3、 Eclipse插件: Egit
Eclipse自带,插件市场搜索最新版
4、 GitHub网站
Build software better, together
二、Git软件
1、git环境下载安装
安装完成后,还需要最后一步设置,在命令行输入如下:
Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识。
C:Usersadmin路径下的.gitconfig文件里面可以看到
--global 表示全局属性,所有的git项目都会共用属性
操作:
mrman
文件自动产生
2、Git软件操作
(1)理解工作目录,暂存区,本地仓库
- 工作区(Working Directory):就是你电脑本地硬盘目录
- 本地库(Repository):工作区有个隐藏目录.git,它就是Git的本地版本库
- 暂存区(stage):一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
(2)操作:
创建项目文件夹设置文件夹属性
创建本地版本仓库
mrman@DESKTOP-QH8S7FP MINGW64 /e
$ git init //初始化一个仓库,执行命令“ git init”
Initialized empty Git repository in E:/.git/mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
多出来一个文件:
提交文件
- 新建文件,名为“hello.txt”
hello.txt编辑内容:
111111111
- 输入命令:git add 文件名,将文件添加到暂存区(没有放在仓库)
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git add hello.txtmrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git status //查看状态
On branch masterInitial commitChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: hello.txtUntracked files:(use "git add <file>..." to include in what will be committed)$RECYCLE.BIN/Linux/ZJZL/kinggsoft/qqpcmgr_docpro/qycache/mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$
如图所示:
输入命令:git commit 提交文件到本地库
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git commit
[master (root-commit) 1a89c3f] create 11 file changed, 1 insertion(+)create mode 100644 hello.txt
若想再次提交,可以这样操作,先修改hello.txt
mrman
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git commit
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
[master warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
9696331] updata 2
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.1 file changed, 2 insertions(+), 1 deletion(-)mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$//查看提交文件有哪些
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git log
commit 96963315b2c39a52a4f97bf2bd79551d2226d87c
Author: gitacct_pm <mrman8868@163.com>
Date: Mon May 11 11:34:49 2020 +0800updata 2commit 1a89c3f50f37f89a717ca116c968bd229ce4bf6d
Author: gitacct_pm <mrman8868@163.com>
Date: Mon May 11 11:26:15 2020 +0800create 1查看文件提交记录
//行查看(查看简易信息
)
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git log --pretty=oneline
96963315b2c39a52a4f97bf2bd79551d2226d87c updata 2
1a89c3f50f37f89a717ca116c968bd229ce4bf6d create 1回退历史
//回退到上一次提交
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git reset --hard HEAD^
HEAD is now at 1a89c3f create 1//再次查看,发现updata 2没有了
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git log --pretty=oneline
1a89c3f50f37f89a717ca116c968bd229ce4bf6d create 1版本穿越
//查看历史记录的版本号
$ git reflog
1a89c3f HEAD@{0}: reset: moving to HEAD^
9696331 HEAD@{1}: commit: updata 2
1a89c3f HEAD@{2}: commit (initial): create 1//恢复 updata 2
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git reset --hard 9696331
HEAD is now at 9696331 updata 2//再次查看,发现有updata 2
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git log --pretty=oneline
96963315b2c39a52a4f97bf2bd79551d2226d87c updata 2
1a89c3f50f37f89a717ca116c968bd229ce4bf6d create 1
误删除项目文件夹中的hello.txt文本?
输入命令:git checkout 文件名,此时,仓库中的文件依然存在,所以可以从本地仓库中还原文件
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git checkout hello.txt
若想删除后的hello.txt文件,想增加到缓冲区里面
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git add hello.txtmrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git status
On branch master
Changes to be committed:(use "git reset HEAD <file>..." to unstage)modified: hello.txtUntracked files:(use "git add <file>..." to include in what will be committed)$RECYCLE.BIN/Linux/ZJZL/kinggsoft/qqpcmgr_docpro/qycache/
若想知道曾经做过哪些操作,不管是增删改查,可以这样做
mrman
(3)系统上线了,但是产品经理又提了新的需求,评估一下工期要两个月,但是同时系统正在上线运行,时不时还要修改bug,如何管理几个版本?
创建分支
//创建分支
切换分支
//切换名为“branchA”分支
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git checkout branchA
Switched to branch 'branchA'//创建word.txt文件写数据:word
mrman@DESKTOP-QH8S7FP MINGW64 /e (branchA)
$ echo "word" >> word.txt
创建word.txt文件
//创建新的,这个文件存放在仓库里面
mrman@DESKTOP-QH8S7FP MINGW64 /e (branchA)
$ git add word.txt
warning: LF will be replaced by CRLF in word.txt.
The file will have its original line endings in your working directory.mrman@DESKTOP-QH8S7FP MINGW64 /e (branchA)
$ git commit -m 'create1'
[branchA ff28a03] create1
warning: LF will be replaced by CRLF in word.txt.
The file will have its original line endings in your working directory.1 file changed, 1 insertion(+)create mode 100644 word.txt
切换主线master
mrman@DESKTOP-QH8S7FP MINGW64 /e (branchA)
$ git checkout master
Switched to branch 'master'
发现开始创建的work.txt文件没有了
切换回来branchA
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git checkout branchA
Switched to branch 'branchA'发现开始创建的work.txt文件有了
合并分支(以master为主)
//切换主线master
mrman@DESKTOP-QH8S7FP MINGW64 /e (branchA)
$ git checkout master
Switched to branch 'master'//合并分支
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git merge branchA
Updating 9696331..ff28a03
Fast-forwardword.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 word.txt
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git checkout master
Already on 'master' mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ vim word.txt123 wordmrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git add word.txtmrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git commit -m 'add 123'
[master f01119e] add 1231 file changed, 1 insertion(+), 1 deletion(-)
三、GitHub
1、GitHub是什么
HUB是一个多端口的转发器,在以HUB为中心设备时,即使网络中某条线路产生了故障,并不影响其它线路的工作。
GitHub是一个Git项目托管网站,主要提供基于Git的版本托管服务
网址:https://github.com/
注册账号的注意事项:
较长时间不使用有可能被Github冻结账号。请登录其客服页面https://github.com/contact,填写账号恢复申请。
首先,登录自己的github:输入邮箱地址+密码
验证代码编码
首先:创建项目工程
之后,我们需要在git的窗口操作命令,先执行:git reflog
mrman
增加远程地址,一般直接用origin作代号,也可以自定义,另需要执行自己的github创建仓库生成的远程:HTTPS的远程地址。执行命令:
//告诉远程仓库地址
推送到远程库
mrman@DESKTOP-QH8S7FP MINGW64 /e (master)
$ git push origin master
弹出窗口,输入密码:
加载这些信息:
$
说明连上了,区自己的github查看,如图所示:
2、从GitHub上克隆(复制)一个项目
(1)在自己github的文件数据或项目克隆到本地指定的文件目录下(通俗说下载到本地磁盘),可以这样操作
执行命令如下:
mrman
查看文件:
(2)若想要在本地磁盘文件做更改,可以这样执行操作
//先进入pt文件目录下
查看:
(3)每次输密码很烦篇
先创建ssh密钥
mrman@DESKTOP-QH8S7FP MINGW64 ~/Desktop
$ ssh-keygen -t rsa -C mrman10086@163.comGenerating public/private rsa key pair.
Enter file in which to save the key (/c/Users/mrman/.ssh/id_rsa):
Created directory '/c/Users/mrman/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/mrman/.ssh/id_rsa.
Your public key has been saved in /c/Users/mrman/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YswQszo3grw+yfXvMhZ5gTR7mCf9ll3PGCaOX3ObucY mrman10086@163.com
The key's randomart image is:
+---[RSA 2048]----+
| o |
| * |
| + B |
|.. . X = . + |
|..+ o X S = + = |
| .= = o = o + + |
|..o . o . . . + +|
|.+ = . E |
| .. . =o ...|
+----[SHA256]-----+
登录Github后,右上角点击setting
在左侧菜单中选择SSH and GPG keys, 在右边点击New SSH key
Title随便写,Key 把之前id_rsa.pub的内容复制进去,点击Add SSH key,设置ssh key 完成。
查看:
再次提交:
mrman@DESKTOP-QH8S7FP MINGW64 /e/.git/repository/pt (master)
$ git add word.txtmrman@DESKTOP-QH8S7FP MINGW64 /e/.git/repository/pt (master)
$ git commit -m "add ssh"
[master 97c8f26] add ssh1 file changed, 1 insertion(+), 1 deletion(-)mrman@DESKTOP-QH8S7FP MINGW64 /e/.git/repository/pt (master)
$ git remote add originssh git@github.com:Peng8868sky/Project20200512.gitmrman@DESKTOP-QH8S7FP MINGW64 /e/.git/repository/pt (master)
$ git push originssh masterThe authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:Peng8868sky/Project20200512.git98376c8..97c8f26 master -> master
查看:
(4)代码提交
先在idea根据登录自己的github
后如下操作:
查看已经提交进来了