一.版本控制系统概述
1.什么是版本控制系统?
将每一次文件的变化,集中在一个系统中加以版本记录,以便后期查阅特定文件版本历史记录的系统。
2. 常见版本控制系统svn与git区别
svn为集中版本控制系统的代表
git为分布式版本控制系统的代表
简单的理解为:SNV依赖网络,git不依赖网络.
3.git代码提交原理
3. git版本控制系统使用
1.yum安装git
yum install -y git
#配置git用户
git config --global user.name "zhangxianwei"
#配置git使用邮箱
git config --global user.email "837404975@qq.com"
#语法高亮
git config --global color.ui true
2.git初始化
2.1初始化工作目录
[root@m01 ~]# mkdir git_data
[root@m01 ~]# cd git_data/
[root@m01 git_data]# git init
重新初始化现存的 Git 版本库于 /root/git_data/.git/
[root@m01 git_data]# ll -a
总用量 8
drwxr-xr-x 3 root root 18 9月 27 15:40 .
dr-xr-x---. 14 root root 4096 9月 27 15:40 ..
drwxr-xr-x 7 root root 119 9月 27 15:41 .git
2.2查看隐藏文件
[root@m01 git_data]# ll .git
总用量 12
drwxr-xr-x 2 root root 6 9月 27 15:40 branches #分支目录
-rw-r--r-- 1 root root 92 9月 27 15:41 config #定义项目特有的配置选项
-rw-r--r-- 1 root root 73 9月 27 15:40 description
-rw-r--r-- 1 root root 23 9月 27 15:40 HEAD
drwxr-xr-x 2 root root 242 9月 27 15:40 hooks
drwxr-xr-x 2 root root 21 9月 27 15:40 info
drwxr-xr-x 4 root root 30 9月 27 15:40 objects #存放所有数据内容(仓库),有info和pack两个子文件夹
drwxr-xr-x 4 root root 31 9月 27 15:40 refs
[root@m01 ~]# git config
usage: git config [options]
Config file location
--global use global config file
--system use system config file
--local use repository config file
三.git基础命令总结
git基础命令 | 解释 |
---|---|
git config | git 配置 |
git init | git 初始化 |
git status | 查看状态 |
git add file | 添加文件到暂存区域 |
git rm --cached file | 从暂存区域撤回到工作目录 |
git reset HEAD b.txt | 从暂存区域撤回到工作目录 |
git commit -m "add new a.txt " a.txt | 提交到仓库 |
git checkout – b.txt | 从暂存区恢复到工作区域 针对暂存区域的文件进行恢复(不管有没有提交到仓库) |
git mv c.txt d.txt | git重命名 |
git diff | 比较工作区域与暂存区域文件是否相同 |
git diff --cached | 比较暂存区域与本地仓库 |
git log | 查看git commit的操作 |
git log --oneline | 查看git commit的操作 简化清晰 |
git log -p | 显示具体的文件改动 |
git log -p -1 | 查看最近一次的文件改动 |
git reset --hard commit | 从本地仓库撤回 |
分支命令 | |
---|---|
git branch dev | 创建dev分支 |
git checkout dev | 切换分支 |
git merge dev | 合并dev分支 |
标签命令 | |
---|---|
git tag -a “v1.1” -m “全新升级1.1” | 默认给最新commit打标签 |
git tag -l | 查看所有标签 |
git show v1.1 | 查看标签对应版本 |
git tag -a “v1.1” 187e808 -m “全新3,0版本升级” | z指定版本打上标签 |
git tag -d v1.1 | 删除标签 |
远程仓库命令 | |
---|---|
git remote add origin + 网址 | 将本地仓库与远程仓库关联 |
git remove -v | 产看管理的远程仓库 |
git push origin master | 推送master分支到origin |
git pull origin dev | l拉取远程仓库到本地dev分支 |
git clone origin master | 克隆远端仓库到本地 |
git push -u origin --tags | 推送本地仓库所有标签 |
四.git实战
实战一. git如何提交目录文件到本地仓库
[root@gitlab demo]# echo "62-v1" > file.txt
[root@gitlab demo]# git status
#将文件添加至暂存区
[root@gitlab demo]# git add file.txt
[root@gitlab demo]# git status
[root@gitlab demo]# git commit -m "file-v1 commit" #将暂存区内容提交至本地仓库
实战二. 如何比对本地工作目录文件内容、暂存区文件内容、本地仓库文件内容之间的差异?
[root@gitlab demo]# git diff file.txt #本地与暂存对比
[root@gitlab demo]# git add .
[root@gitlab demo]# git diff file.txt
[root@gitlab demo]# git diff --cached file.txt #暂存区与本地仓库对比
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "file-v3 commit"
[root@gitlab demo]# git diff --cached file.txt
实战三. 提交内容至暂存区、或本地仓库,想回退怎么办?
1.本地提交至暂存区向回退? (当误操作本地目录的内容后,可以通过暂存区覆盖本地内容)
[root@gitlab demo]# git diff file.txt
[root@gitlab demo]# > file.txt
[root@gitlab demo]# cat file.txt
[root@gitlab demo]# git checkout -- file.txt
[root@gitlab demo]# cat file.txt
2.暂存区提交到本地仓库向回退怎么办?
[root@gitlab demo]# vim file.txt
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "file-v4 commit" #提交了多次至本地仓库
[root@gitlab demo]# git log
3.如果多次提交,多次回退?
[root@gitlab demo]# git reflog #查看所有的历史提交变更记录
[root@gitlab demo]# git reset --hard 187e808
四. git分支是干什么的
1.基于master的位置点,创建了一个新的dev分支
[root@gitlab demo]# git branch dev
[root@gitlab demo]# git branch
dev
\* master
2.dev分支操作如下:
[root@gitlab demo]# echo "oldxu" > file-2
[root@gitlab demo]# echo "oldli" > file-3
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "dev-create new 2 file" #dev更新了位置点
3.回到master分支,更新master的位置点
[root@gitlab demo]# git checkout master
[root@gitlab demo]# echo "62-v5" >> file.txt
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "Master Update V5"
4.切换dev分支,合并master
[root@gitlab demo]# git checkout dev
[root@gitlab demo]# git merge master #站在dev分支合并master -->安全
--------------->测试检查ok
5.如果分支合并出现了冲突怎么办?
1.master上:
[root@gitlab demo]# cat new
AAA
BBB
YYY
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "new file master"
2.dev上:
git checkout dev
[root@gitlab demo]# cat new
AAA
BBB
ZZZ
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "new file dev"
6.切换回master主干分支,合并dev分支
[root@gitlab demo]# git checkout master
[root@gitlab demo]# git merge dev #站在master分支合并dev
--------------->测试检查ok--->部署
五.git的tag标签是干什么?
commitID
123456 <----v1.0
978765 <----v2.0
978765 <----v2.1
978765 <----v2.2
1.如何打标签:
[root@gitlab demo]# git tag -a "v1.1" -m "全新升级1.1"
2.如何查看标签
[root@gitlab demo]# git tag -l
[root@gitlab demo]# git show v1.1
3.如何删除标签
[root@gitlab demo]# git tag -d v1.1
4.如何给指定的CommitID打上标签?
[root@gitlab demo]# git tag -a "v3.0" 187e808 -m "全新3,0版本升级"
[root@gitlab demo]# git reset --hard v3.0 #方便后续的回退,或者记录当时的状态
六.git远程仓库如何使用?
实战一.如何将本地仓库与远程Gitee进行关联?
1.注册gitee
2.创建一个远程仓库?
3.配置使用远程仓库
3.1) Git 全局设置:
git config --global user.name "oldxu"
git config --global user.email "552408925@qq.com"
3.2) 将本地已有的git的仓库与远程关联
cd existing_git_repo
git remote add origin https://gitee.com/oldboy_oldxu/rainbow.git
3.3) 本地仓库代码推送至远程仓库? (确保本地的所有资源已提交至本地仓库)
git push -u origin master
3.4) 推送不同的分支?
git checkout dev
git push -u origin dev
3.5) 推送tag?
git push -u origin --tags #推送本地所有的tag
git push -u origin v1.1 #指定推送某个本地的tag
实战二、将https方式修改为ssh密钥访问通讯?
1.删除与远程https仓库的关联?
[root@gitlab demo]# git remote remove origin
[root@gitlab demo]# git remote -v
2.添加新的关联?-->SSH方式
[root@gitlab demo]# git remote add origin git@gitee.com:oldboy_oldxu/rainbow.git
[root@gitlab demo]# git remote -v
origin git@gitee.com:oldboy_oldxu/rainbow.git (fetch)
origin git@gitee.com:oldboy_oldxu/rainbow.git (push)
3.在本地机器上生成一个密钥对,然后将公钥放入远程仓库?
[root@gitlab demo]# ssh-keygen
[root@gitlab demo]# cat ~/.ssh/id_rsa.pub #复制public公钥内容
找个右上角用户-->设置-->SSH公钥-->添加Key
4.测试提交代码是否还需要输入密码
[root@gitlab demo]# git checkout master
[root@gitlab demo]# touch file-dddd
[root@gitlab demo]# git add .
[root@gitlab demo]# git commit -m "ddd"
[root@gitlab demo]# git push -u origin master
实战三、如果现在开发的这个项目有同事想加入进来怎么办?
Linux
MacOS
ssh-keygen
yum install git -y
Windows?
1.下载安装一个git软件
http://192.168.0.128/git/Git-2.10.0-64-bit.exe
https://gitforwindows.org/
2.打开 Git Bash工具
3.使用git bash 工具 创建ssh密钥对
ssh-keygen
4.克隆项目
$ git clone git@gitee.com:oldboy_oldxu/rainbow.git
实战四. 新加入的同事提交了内容,其他成员看不见怎么办?
1.登录开发B--->WIndows 修改代码进行变更操作:
touch README
git add .
git commit -m "Windows push README"
git push origin master
2.登录开发A--->Linux
git pull origin master #获取到开发B提交的代码