【Linux】【Services】【VersionControl】Git基础概念及使用
1. 简介
1.1. 版本控制工具:
本地版本控制系统:
集中化版本控制系统:CVS,SVN
分布式版本控制系统: BitKeeper,Git
1.2. 官方网站:
https://git-scm.com
1.3. Git基本结构
工作区:Workding Directory
暂存库:Staging Area
版本库: Repository
1.4. Git仓库包含索引(暂存)和对象库(版本库)
1.5. 相关文档
免费的ebook: https://git-scm.com/book/zh/v2
1.6.Git的对象类型
块(blob)对象:文件的每个版本表现为一个块(blob)
树(tree)对象:一个目录代表一层目录信息
提交(commit)对象:用于保存版本库一次变化的元数据,包括作者、邮箱、提交日期、日志;每个提交对象都指定一个目录树对象
标签(tag)对象:用于给一个特定对象一个易读的名称;
1.7. Git的文件分类
已追踪的(tracked):已经在版本库中,或者已经使用git add命令添加至索引中的文件;
被忽略的(Ignored):在版本库中通过“忽略文件列表”明确声明为被忽略的文件;
未追踪的(untracked):上述两类之外的其他文件;
2. Git简单使用
2.1. git init
~]cd /root
~]mkdir myproject
~]cd myproject
~]git init
~]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags 9 directories, 13 files
2.2. git config
##添加全局配置参数
git config --global user.sex male
git config --global user.name eric ##添加本地配置参数
git config --add user.mail eric@123 ##这个是全局参数,在用户家目录下
~]# pwd
/root
~]# ls -al .gitconfig
-rw-r--r-- 1 root root 20 Aug 7 04:13 .gitconfig
~]# cat .gitconfig
[user]
name = eric
sex = male
~]# git config -l --global
user.name=eric
user.sex=male ##这个是本地参数,在项目目录下
~]# pwd
/root/myproject/.git
~]# ls -al config
-rw-r--r-- 1 root root 116 Aug 7 04:57 config
~]# git config -l --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.mail=eric@123
~]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
mail = eric@123
#还有一个系统级的在/etc/gitconfig
~]# git config -l --system
2.3. git add
git add . #暂存当前目录所有
git add DIR #暂存指定DIR
2.4. git commit
提交的标识:
引用:ID,reference,SHA1,绝对提交名
符号引用:symbolic reference,symref;
本地特性分支名称、远程跟踪分支名称、标签名;
名称:
refs/heads/REF:本地特性分支名称
refs/remotes/REF:远程跟踪分支名称
refs/tags/REF:标签名
git会自动维护几个特定目的的特殊符号引用:
HEAD:始终指向当前分支的最近提交;或检出到其他分支时,目标分支的最近提交;
ORIG_HEAD:合并操作时,新生成的提交之前的提交保存于此引用中;
FETCHED_HEAD:
MERGE_HEAD:合并操作时,其他分支的上一次提交;
#add不add都可以commit,但是commit之前要指定email和user
~]# git commit -m "v0.1" *** Please tell me who you are. Run git config --global user.email "you@example.com"
git config --global user.name "Your Name" #再次提交
~]# git commit -m "v0.1"
[master (root-commit) 3c56c9b] v0.1
1 file changed, 1 insertion(+)
create mode 100644 readme.md
2.5. git clone
#可以clone本地或者远程的项目
~]# git clone myproject/ Newmyproject
Cloning into 'Newmyproject'...
done.
2.6. git status
# 已经提交或者刚创建
~]# git status
# On branch master
nothing to commit, working directory clean # 修改一下
~]# echo "installation" >> install.sh
~]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# install.sh
nothing added to commit but untracked files present (use "git add" to track) # 保存一下
~]# git add install.sh
~]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: install.sh
# #提交一下
~]# git commit -m "v0.2"
[master 19cb183] v0.2
1 file changed, 1 insertion(+)
create mode 100644 install.sh
~]# git status
# On branch master
nothing to commit, working directory clean
2.7. 文件相关
git ls-files:默认显示索引中的文件列表的原始文件名;
-s:显示暂存的文件信息,;权限、对象名、暂存号及原始文件名;
-o:显示未被追踪的文件
--unmerged:只查看未合并的文件
git cat-file
git rm:删除工作目录中的文件,及索引中的映射;
--cached:只删除索引中的映射
git mv:改变工作目录中的文件名,及索引中的映射
~]# git ls-files -s
100644 3100e23cd38ef12583bfc792b5c62aea7e9dbaca 0 install.sh
100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0 readme.md ~]# git cat-file -p 3100e23cd38ef12583bfc792b5c62aea7e9dbaca
installation
2.8. git log
git log --graph --pretty=oneline --abbrev-commit
~]# git log
commit 19cb183c7270e8fe4015f11e7d712372ba40c3b4
Author: eric <eric@1234.com>
Date: Tue Aug 7 07:38:17 2018 +0200 v0.2 commit 3c56c9bd0bf1600982e0af01540e96207276f136
Author: eric <eric@1234.com>
Date: Tue Aug 7 05:11:50 2018 +0200 v0.1
2.9. git diff
~]# git diff --color 3c56c9bd0bf1600982e0af01540e96207276f136 19cb183c7270e8fe4015f11e7d712372ba40c3b4
diff --git a/install.sh b/install.sh
new file mode 100644
index 0000000..3100e23
--- /dev/null #老文件
+++ b/install.sh #新文件
@@ -0,0 +1 @@
+installation
2.10. git reset:撤销此前的操作;
--soft:将HEAD引用指向给定的提交,但不影响索引和工作目录;
--mixed:将HEAD引用指向指定的提交,并将索引内容改变为指定提交的快照;但不改变工作目录;
--hard:将HEAD引用指向给定的提交、将索引内容改变为指定提交的快照,并改变工作目录中的内容反映指定提交的内容
2.11. git branch:列出、创建及删除分支
git branch BRANCH_NAME [START_COMMIT]
git branch -d BRANCH_NAME
2.12. git show-branch:查看分支及其相关的提交
2.13. git checkout:在分支间切换
git checkout <branch>:检出分支
2.14. git merge BRANCK_NAME #需要checkout到master
3. git服务器
3.1. 支持的协议:本地协议(local)、HTTP/HTTPS协议、SSH协议、Git协议
3.2. 本地协议:
git clone file:///path/to/repo.git /path/to/local/server git clone /path/to/repo.git /path/to/local/server
3.3. Git协议:由git-deamon程序提供,监听在tcp的9418端口;仅支持“读”操作,无任何认证功能;
git://host/path/to/repo.git git://host/~user/path/to/repo.git
3.4. SSH协议:
ssh://[USER@]host[:port]/path/to/repo.git ssh://[USER@]host[:port]/~USERNAME/path/to/repo.git [USER@]host/path/to/repo.git
3.5. HTTP/HTTPS协议:在1.6.5之前的版本为哑http协议,只能读不能写,不能认证,在1.6.6之后的版本为智能http协议,可读可写可认证
http://host/path/to/repo.git #这是一个映射的地址,并不是服务器真实的路径
3.6.引用远程版本库:
远程版本库是定义在配置文件中的一个实体,在配置文件中体现为[remote "NAME"],它由两部分组成,URL+refspec(定义一个版本库与其他版本库的名称空间的映射关系)
+source:destination
refs/heads/NAME:本地分支
refs/remotes/NMAE:远程跟踪分支 [remote "publis"]
url = http://HOST/pub/repo_name.git
push = + refs/heads/*:refs/remote/origin/* remote.publish.url
remote.publish.push
3.7. git remote命令:管理远程仓库
4. git服务器
4.1. 安装必要的包
yum install -y git-daemon httpd
4.2. 检查httpd安装
# 修改http配置文件
~]# sed "s/\<ServerName/ServerName YOURSERVERIP:80/g" /etc/httpd/conf/httpd.conf # alias,cgi,env这三个模块必须要有
~]# httpd -M |grep -Ei "\<(alias|cgi|env)"
alias_module (shared)
env_module (shared)
cgi_module (shared)
~]# systemctl start httpd
4.3. 检查git-deamon安装
~]# cat /usr/lib/systemd/system/git@.service
[Unit]
Description=Git Repositories Server Daemon
Documentation=man:git-daemon(1) [Service]
User=nobody
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
StandardInput=socket
~]# systemctl start git.socket
【Linux】【Services】【VersionControl】Git基础概念及使用的更多相关文章
- Git基础概念与Flow流程介绍
目录 Git相关 基本概念 常见客户端 TortoiseGit Sourcetree Intellij Idea 命令行 常用命令 存储区域 命令之 add & commit &pus ...
- Git基础-第2章
简单的Git基础概念: repository: 仓库 track: 跟踪 stage: 暂存 commit: 提交 push: 推送 pull: 拉取 一.获取Git仓库 ...
- Git基础和入门
一.Git基础概念 Git功能简单概述 可以随时回滚到之前的代码版本(git reset --hard ): 协同开发时不会覆盖别人的代码(分支): 留下修改记录(git log): 发版时可以方便的 ...
- linux设备驱动归纳总结(二):模块的相关基础概念【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-59415.html linux设备驱动归纳总结(二):模块的相关基础概念 系统平台:Ubuntu 10 ...
- linux设备驱动归纳总结(一)内核的相关基础概念【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-59413.html linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxx ...
- (转载)小白的linux设备驱动归纳总结(一):内核的相关基础概念---学习总结
1. 学习总结 小白的博客讲的linux内核驱动这一块的东西比较基础,因此想通过学习他的博客,搭配着看书的方式来学习linux内核和驱动.我会依次更新在学习小白的博客的过程的感悟和体会. 2.1 内核 ...
- 【Linux开发】linux设备驱动归纳总结(二):模块的相关基础概念
linux设备驱动归纳总结(二):模块的相关基础概念 系统平台:Ubuntu 10.04 开发平台:S3C2440开发板 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 【Linux开发】linux设备驱动归纳总结(一):内核的相关基础概念
linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- Git详解之二 Git基础
Git 基础 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置: ...
随机推荐
- Laravel 中输出 SQL 语句的到 log 日志
在 AppServiceProvider.php 中的 boot 方法中添加如下代码 即可 public function boot() { //数据库监听 DB::listen(function ( ...
- pip 安装使用 ImportError: No module named setuptools 解决方法
安装过程详见这篇博客: http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ 安装后运行到:python setup.py ins ...
- 【java+selenium3】select 下拉选 (八)
一.select 下拉框 1.下拉选的处理类:Select 如果页面元素是一个下拉框,我们可以将此web元素封装成Select对象. Select select = new Select(WebEle ...
- 远程连接linux | Xshell和Xftp下载安装
为什么需要远程登录linux 公司开发时候, 具体的情况是这样的: Linux 一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的 Linux 服务器.这时我们就需要远程登录到Linux ...
- PCB各层介绍
在PCB设计中用得比较多的图层: mechanical 机械层 keepout layer 禁止布线层 Signal layer 信号层 Internal plane layer 内部电源/接地层 t ...
- 津门杯WriteUP
最近很浮躁,好好学习 WEB power_cut 扫目录 index.php <?php class logger{ public $logFile; public $initMsg; publ ...
- vscode 导入第三方jar包(添加外部JAR)
添加 jar包 至根目录下lib文件夹,在 .classpath 文件内添加 jar 路径. 注意:新添加的 jar路径 在"src"和"bin"之间,否则无法 ...
- 菜鸡的Java笔记 - java 双向一对多映射
双向一对多映射 two-way 开发要求: 根据数据表的结构进行简单java类的转换: 要求实现如下的输出信息: 可以根据课程取得全部参与 ...
- 使用jiava打印一个三角形
public class ForDemo { public static void main(String[] args) { /* 打印一个5行高的三角形,首先将三角形分成三部分: 第一部分是前面的 ...
- JVM-学习笔记持续更新
1.Java虚拟机的基本结构 (1)类加载子系统与方法区: 类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放在一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行 ...