1. Git简介
- linus用C语言编写
- 2005年诞生
- 分布式版本管理系统
- 速度快,适合大规模,跨地区多人协同开发
2. 本地版本管理
本地版本管理2.1 集中式
集中式2.2 分布式
分布式3. Git生态
- Git分布式版本管理系统
- Gitlab git私库解决方案
- Github git公有库解决方案
4. Git安装
- CentOS
yum install git(这种方法安装版本太旧) - Ubuntu
apt-get install git - Windows安装git bash
- Linux编译安装(注意不要使用git1.8以下版本,推荐使用2.7以上版本
CentOS示例:
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y ---安装依赖
[root@localhost ~]# wget https://github.com/git/git/archive/v2.7.4.zip -O git-2.7.4.zip ---下载git
[root@localhost ~]# unzip git-2.7.4.zip ---解压
[root@localhost ~]# cd git-2.7.4/
[root@localhost git-2.7.4]# make prefix=/usr/local/git all ---编译
[root@localhost git-2.7.4]# make prefix=/usr/local/git install ---安装
[root@localhost git-2.7.4]# vi /etc/profile
export $PATH=/usr/local/git/bin/$PATH ---在最后面添加这行
:wq
[root@localhost git-2.7.4]# source /etc/profile
[root@localhost git-2.7.4]# git --version
git version 2.7.4
初始化git:
[root@localhost test]# mkdir test
[root@localhost test]# cd test/
[root@localhost test]# git init
[root@localhost test]# git config --global user.name "zheng"
[root@localhost test]# git config --global user.email zheng@qq.com
[root@localhost test]# git config --list
user.name=zheng
user.email=zheng@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
5. Git原理
四个区域 四种状态 常用命令示例1:
root@localhost ~]# cd test/
[root@localhost test]# touch index.html
[root@localhost test]# vi index.html
<h1>hello world</h1>
:wq
[root@localhost test]# git status
On branch master
Initial commit
Untracked files: ---未被追踪文件
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost test]# git add index.html ---添加进代码库
[root@localhost test]# git status
On branch master
Initial commit
Changes to be committed: ---在这个下面的就为暂存区里
(use "git rm --cached <file>..." to unstage) ---文件已经在暂存区里,要撤回来的话用“git rm --cached <file>”
new file: index.html
[root@localhost test]# git commit -m "first commit" ---提交到本地仓库
[master (root-commit) b613f50] first commit
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@localhost test]# git status
On branch master
nothing to commit, working directory clean ---工作目录为空
[root@localhost test]# git log
commit b613f505626d37c74b262ba5a345ef41740785fb ---ID
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
示例2:
创建第二个文件:
[root@localhost test]# touch test.html
[root@localhost test]# vi test.html
Hello World
:wq
[root@localhost test]# git add test.html
[root@localhost test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test.html
[root@localhost test]# touch news.html
news
:wq
[root@localhost test]# git add news.html
[root@localhost test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: news.html
new file: test.html ---假设这个没开发完,需要撤回来
[root@localhost test]# git rm --cached test.html ---撤回来
rm 'test.html'
[root@localhost test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: news.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.html ---被拉回去工作目录了
[root@localhost test]# git commit -m "news" ---把没问题的news提交
[master 8ce4e28] news
1 file changed, 1 insertion(+)
create mode 100644 news.html
[root@localhost test]# git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.html
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost test]# vi test.html ---修改bug
Hello World
fixed bug ---修改内容
:wq
[root@localhost test]# git add test.html
[root@localhost test]# git commit -m "test"
[master 8880be9] test
1 file changed, 2 insertions(+)
create mode 100644 test.html
[root@localhost test]# git log
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
网友评论