1. git 简明使用手册
文章目录
1.1. 简介
Linux 内核开源项目有着为数众多的参与者。 绝大多数的 Linux 内核维护工作都花在了提交补丁和保存归档的繁琐事务上(1991-2002年间)。 到 2002 年,整个项目组开始启用一个专有的分布式版本控制系统 BitKeeper 来管理和维护代码。
到了 2005 年,开发 BitKeeper 的商业公司同 Linux 内核开源社区的合作关系结束,他们收回了 Linux 内核社区免费使用 BitKeeper 的权力。 这就迫使 Linux 开源社区(特别是 Linux 的缔造者 Linus Torvalds)基于使用 BitKeeper 时的经验教训,开发出自己的版本系统Git.
git在当时的背景下开发出来,拥有以下特性:
- 对非线性开发模式的强力支持(允许成千上万个并行开发的分支)
- 完全分布式
- 有能力高效管理类似 Linux 内核一样的超大规模项目(速度和数据量)
1.2. git 工作流
1.3. 配置文件
1.4. 获取git仓库
1.4.1. 从现有项目或目录下导入所有文件到 Git 中
cd /home/zhangrj/git_experiment/clone
mkdir -p clone.init && cd clone.init
git init && git remote add origin /home/zhangrj/git_experiment/server/bare.repo
git pull origin master
1.4.2. 从一个服务器克隆一个现有的 Git 仓库
- 格式:
- git clone [url]
- git clone -b [branch] [url] 克隆指定分支
- git clone -b [branch] [url] -o [name] 自定义仓库名称
cd /home/zhangrj/git_experiment/clone
git clone -b master /home/zhangrj/git_experiment/server/bare.repo bare.clone.init
1.5. 提交,暂存,状态,移除,移动,提交历史
1.5.1. 检查当前文件状态
使用 git status 命令
1.5.2. 跟踪新文件
使用命令git add开始跟踪一个文件,会将文件添加到下一次的提交中,此时文件处于暂存状态.
[root@localhost bare]# echo "CONTRIBUTING" > CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# CONTRIBUTING.md
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost bare]# git add CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: CONTRIBUTING.md
#
1.5.3. 暂存已修改文件
如果你修改了一个名为 CONTRIBUTING.md 的已被跟踪的文件,然后运行 git status 命令,会看到下面内容
[root@localhost bare]# cat CONTRIBUTING.md
CONTRIBUTING
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: CONTRIBUTING.md
#
[root@localhost bare]# echo "modify" >> CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: CONTRIBUTING.md
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: CONTRIBUTING.md
#
Changes not staged for commit 这行下面,说明已跟踪文件的内容发生了变化,但还没有放到暂存区,要暂存这次更新,需要运行 git add 命令.
[root@localhost bare]# git add CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: CONTRIBUTING.md
#
[root@localhost bare]#
1.5.4. 提交更新
- 使用命令 git commit -m 提交处于暂存状态的文件
- 使用命令 git commit -a -m 跳过使用暂存区域的方式,自动把所有已经跟踪过的文件暂存起来一并提交
1.5.5. 移除
- git rm 文件名称 : 该文件就不再纳入版本管理了.
- git rm -f 文件名称 : 如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f,该选项会连带将本地文件一起删除.
- git rm --cached 文件名称 : 只是将文件从仓库删除,本地仍然还存在该文件.
[root@localhost bare]# ls
CONTRIBUTING.md
[root@localhost bare]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost bare]# git rm CONTRIBUTING.md
rm 'CONTRIBUTING.md'
[root@localhost bare]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: CONTRIBUTING.md
#
[root@localhost bare]# ls
[root@localhost bare]# git commit -m "remove file CONTRIBUTING.md"
[master 279f415] remove file CONTRIBUTING.md
Committer: root <root@localhost.localdomain>
1.5.6. 提交历史
- git log 查看提交历史
[root@localhost init]# git log --pretty=format:'%cd %cr %s' --graph
* Fri Sep 21 16:18:16 2018 +0800 4 months ago 修改下发指令的topic错误的问题
* Wed Sep 12 16:32:13 2018 +0800 4 months ago 修改反向控制,p2p报文格式,去掉转义的""
* Wed Jun 20 11:01:48 2018 +0800 7 months ago 更新CHANGELOG.md
* Wed Jun 20 10:36:29 2018 +0800 7 months ago add some fixes
* Sat May 19 10:13:55 2018 +0800 8 months ago modify default in config file and add fault judge in business/business.go
* Fri May 18 21:28:14 2018 +0800 8 months ago 修改读取配置文件的方式
* Fri May 18 13:01:36 2018 +0800 8 months ago 添加消费redis p2p消息的log
* Fri May 18 11:35:06 2018 +0800 8 months ago 修改反向控制指令逻辑
* Thu May 17 20:00:33 2018 +0800 8 months ago 修改business包的导入路径
* Thu May 17 16:34:05 2018 +0800 8 months ago 修改配置文件名称为fido.conf
* Thu May 17 16:18:16 2018 +0800 8 months ago 添加fido仓库代码
- git show shaID 查看某次提交的详细信息
[root@localhost init]# git log --abbrev-commit --pretty=oneline
424e897 修改下发指令的topic错误的问题
1cdc804 修改反向控制,p2p报文格式,去掉转义的""
1f9670a 更新CHANGELOG.md
2739a0f add some fixes
9419328 modify default in config file and add fault judge in business/business.go
57f160d 修改读取配置文件的方式
f067f2f 添加消费redis p2p消息的log
a2ceb00 修改反向控制指令逻辑
cd7a9e7 修改business包的导入路径
3a8871e 修改配置文件名称为fido.conf
a95b488 添加fido仓库代码
[root@localhost init]# git show 424e897
commit 424e897eb56eab01ddd6900d560bd0bd2487bc73
Author: zhangrj <zhangrongjie@mixlinker.com>
Date: Fri Sep 21 16:18:16 2018 +0800
修改下发指令的topic错误的问题
diff --git a/business/business.go b/business/business.go
index 3aa6fca..f2e4206 100755
--- a/business/business.go
+++ b/business/business.go
@@ -71,7 +71,7 @@ func MQttPublisher() {
Log.Error("get mqtt null publisher")
return
}
- topic := publisher.GetTopic()
+ //topic := publisher.GetTopic()
for {
select {
@@ -87,6 +87,7 @@ func MQttPublisher() {
continue
}
payload := a["payload"].(string)
+ topic := a["topic"].(string)
publisher.Publish(topic, []byte(payload))
subdebugenable, _ := Conf.Bool("redis::redis.sub.debug.enable")
if subdebugenable {
[root@localhost init]#
1.6. 撤销操作
1.6.1. 撤销操作
有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了,可以运行带有 --amend 选项的提交命令尝试重新提交
[root@localhost remote]# cat remote_test.txt
remote_test
append
[root@localhost remote]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost remote]# git log --pretty=oneline
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append
[root@localhost remote]# echo "append and amend" >> remote_test.txt
[root@localhost remote]# cat remote_test.txt
remote_test
append
append and amend
[root@localhost remote]# git add remote_test.txt
[root@localhost remote]# git commit --amend --allow-empty -m "append and amend"
[root@localhost remote]# git log --pretty=oneline
8ba306c51bcf29ce9e806b06a65b1a459ea0344e append and amend
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append
1.6.2. 取消暂存的文件
使用 git reset 命令 取消暂存的文件,将处于暂存状态的文件回到未追踪状态
[root@localhost remote]# touch reset_test.txt
[root@localhost remote]#
[root@localhost remote]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# reset_test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost remote]# git add reset_test.txt
[root@localhost remote]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: reset_test.txt
#
[root@localhost remote]# git reset reset_test.txt
[root@localhost remote]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# reset_test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost remote]#
1.6.3. 撤消对文件的修改
如果你并不想保留对文件的修改怎么办?你可以使用 git checkout – [file] 方便地撤消修改 - 将它还原成上次提交时的样子
[root@localhost remote]# cat remote_test.txt
remote_test
append
append and amend
[root@localhost remote]# echo "checkout" >> remote_test.txt
[root@localhost remote]# cat remote_test.txt
remote_test
append
append and amend
checkout
[root@localhost remote]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: remote_test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost remote]# git checkout -- remote_test.txt
[root@localhost remote]# cat remote_test.txt
remote_test
append
append and amend
[root@localhost remote]#
1.7. 打标签
1.7.1. 列出标签
使用命令 git tag 列出标签
cd /home/zhangrj/git_experiment/clone
1.7.2. 创建标签
1.7.2.1. 轻量标签/附注标签
- 轻量标签很像一个不会改变的分支 - 它只是一个特定提交的引用.
- 附注标签是存储在 Git 数据库中的一个完整对象.它们是可以被校验的;其中包含打标签者的名字,电子邮件地址,日期时间;还有一个标签信息.
cd /home/zhangrj/git_experiment/clone
git clone root@127.0.0.1:/home/zhangrj/git_experiment/server/bare.repo repo
cd repo
# 轻量标签
git tag light-tag
# 赋值标签
# -m 选项指定了一条将会存储在标签中的信息,没有指定-m,git会要求你输入信息。
git tag -a v1.4 -m 'my version 1.4'
1.7.2.2. 后期打标签
就是针对过去的提交shaid打标签
[root@localhost bare]# git tag
[root@localhost bare]# git log --pretty=oneline
d66518f7faaad8bebb8a164844cf01c1c0adcfae 12
825a4bf8c6a3d431405bc8a982ba02ea10ffb6cb append and amend
5cf375122164eb4482ca6bf36a68b37f857b635f 添加amend_test.txt文件
f507912b82acd9b2279960b46084ce07643eae77 添加新文件remote_test.txt
279f415fee6e3ee76cc91ae864ba62ac2bdfeee8 remove file CONTRIBUTING.md
cbeafac429b62acf951263e7d70faa23ea83e190 添加新文件
[root@localhost bare]# git tag -a v0.1 cbeafac429b62acf951263e7d70faa23ea83e190
[root@localhost bare]# git tag
v0.1
1.7.3. 共享标签
- 将本地tag push到远程仓库上
- git push [remote-url] [tag-name]
[root@localhost bare]# git tag
v0.1
[root@localhost bare]# git remote -v
origin root@127.0.0.1:/home/zhangrj/git_experiment/server/bare (fetch)
origin root@127.0.0.1:/home/zhangrj/git_experiment/server/bare (push)
[root@localhost bare]# git status
[root@localhost bare]# git push origin v0.1
1.7.4. 删除标签
- git tag -d [tag-name]
[root@localhost bare]# git tag
v0.1
[root@localhost bare]# git tag -d v0.1
Deleted tag 'v0.1' (was cd7aa76)
[root@localhost bare]# git tag
1.7.5. 获取远程标签
- git fetch [remote-url] tag tagname
[root@localhost bare]# git remote -v
origin root@127.0.0.1:/home/zhangrj/git_experiment/server/bare (fetch)
origin root@127.0.0.1:/home/zhangrj/git_experiment/server/bare (push)
[root@localhost bare]# git tag
[root@localhost bare]# git fetch origin tag v0.1
[root@localhost bare]# git tag
v0.1
1.8. 分支
1.9. git协议
本地协议
- 本地协议就是文件协议,通过文件地址来访问仓库
cd /home/zhangrj/git_experiment/clone
git clone /home/zhangrj/git_experiment/server/bare.repo bare.clone.local_protocol
ssh 协议
http/https协议
git协议
- 安装 git damon 软件
yum install git-daemon
- 命令行运行
git daemon --verbose --export-all --base-path=/home/repo/pub --reuseaddr --enable=receive-pack --detach
# --detach:daemon方式运行
# --base-path:指定仓库地址
# --enable=receive-pack:允许client能够push
- 客户端可以使用 git协议 拉取代码 git clone git://server-ip/XX.git
[root@localhost bare]# pwd
/home/zhangrj/git_experiment/server/bare
[root@localhost bare]# ls
branches config description HEAD hooks info objects refs
[root@localhost bare]# cd .. && git daemon --verbose --export-all --base-path=`pwd` --reuseaddr --enable=receive-pack --detach
[2] 31786
[root@localhost bare]# cd /home/zhangrj/git_experiment/clone/
[root@localhost clone]# pwd
/home/zhangrj/git_experiment/clone
[root@localhost clone]# ls
fido.repo init remote tag
[root@localhost clone]# git clone git://127.0.0.1/bare
Cloning into 'bare'...
remote: Counting objects: 23, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 23 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (23/23), done.
Resolving deltas: 100% (2/2), done.
[root@localhost clone]# ls
bare fido.repo init remote tag
1.10. 远程仓库
1.10.1. 查看远程仓库
git remote -v -v会显示需要读写远程仓库使用的Git保存的简写与其对应的URL
1.10.2. 添加远程仓库
git remote add fido http://192.168.88.4/fidis/FIDATA/fido.git --> 使用fido代替后面的url
1.10.3. 从远程仓库拉取
git fetch [remote-name]
1.10.4. 推送到远程仓库
git push [remote-name] [branch-name]
mkdir -p /home/zhangrj/git_experiment/{server,clone}
cd /home/zhangrj/git_experiment/clone
mkdir -p remote && cd remote
git init
git remote -v
git remote add bare root@127.0.0.1:/home/zhangrj/git_experiment/server/bare.repo
git remote -v
git pull bare master
或者
git fetch --all && git reset --hard bare
1.10.5. 远程仓库的重命名
git remote rename [old] [new]
1.10.6. 远程仓库的移除
git remote rm [remote-url](or remote-name)
1.11. 创建仓库服务器
- 远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改.
- 使用git的仓库大部分都是遵循开放协议的,源代码开源的;如果不想公开源代码,就只能自己搭建一台Git服务器作为私有仓库使用。
1.11.1. 示例
- 创建一个没有源代码的裸仓库
mkdir -p /home/zhangrj/git_experiment/{server,clone}
cd /home/zhangrj/git_experiment/server
git init --bare --shared bare.repo
cd /home/zhangrj/git_experiment/clone
git clone root@127.0.0.1:/home/zhangrj/git_experiment/server/bare.repo bare.clone
- 把现有仓库导出为裸仓库
cd /home/zhangrj/git_experiment/server
git clone root@127.0.0.1:/home/zhangrj/git_experiment/server/bare.repo --bare --shared bare.repo.2
cd /home/zhangrj/git_experiment/clone
git clone root@127.0.0.1:/home/zhangrj/git_experiment/server/bare.repo.2 bare.clone.2