集中式版本控制器

  • CSV/SVN
  • 速度慢,必须联⽹
  • 只有⼀个中央数据仓库,如果中央数据仓库挂了或者⽆法访问,所有的使⽤者⽆法使⽤SVN,⽆法进⾏提交或备份操作

分布式版本控制器

  • Git
  • ⽆中央服务器,每个⼈的电脑都是⼀个完整的版本库
  • 安全性能更⾼
  • 通常有⼀台充当“中央服务器”的电脑,仅仅作为⽅便“交换”⼤家的修改

Git简介

工作区:

  • 当前所在目录就是工作区

暂存区:

  • .git不算⼯作区,只是git的版本库 版本库中只有暂存区和⾃动创建的master分⽀及指向master的⼀个指针HEAD git跟踪的是每次修改⽽不是⽂件,如果不将修改添加到暂存区是⽆法进⾏提交的

.git隐藏⽬录中⽂件介绍:

  • branches # 分支目录
  • config # 定义项目特有的配置选项
  • description # 仅供git web程序使用
  • HEAD # 指示当前的分支
  • hooks # 包含git钩子文件
  • info # 包含一个全局排除文件
  • objects # 本地仓库,存放所有的数据内容,有info和pack两个文件夹
  • refs # 存放指向数据(分支)的提交对象的指针
  • index # 保存暂存信息,在执行git init的时候,这个文件还没有

远程仓库:

  • 最常见的远程仓库就是github

安装

yum安装

[root@server1 ~]# yum install git -y

配置本地用户信息

[root@server1 my_git]# git config --global user.name "eagle"
[root@server1 my_git]# git config --global user.email "eagle@qq.com"
[root@server1 my_git]# git config --global color.ui true
[root@server1 my_git]# git config --list
user.name=eagle
user.email=eagle@qq.com
color.ui=true

创建并初始化版本库

[root@server1 ]# mkdir my_git
[root@server1 my_git]# cd my_git
[root@server1 my_git]# git init
初始化空的 Git 版本库于 /my_git/.git/
[root@server1 my_git]# ls -a
.  ..  .git
[root@server1 my_git]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs

常规使用

把文件加到本地仓库中

[root@server1 my_git]# echo "hello world" > README.txt
[root@server1 my_git]# git add README.txt 
[root@server1 my_git]# git commit -m "write a README.txt file"
[master(根提交) 854d889] write a README.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

修改文件内容,查询状态并提交

[root@server1 my_git]# echo "hello eagleslab" >> README.txt 
[root@server1 my_git]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#   修改:      README.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

查询文件内容的变动情况

[root@server1 my_git]# echo "hello linux"  >> README.txt 
[root@server1 my_git]# git diff README.txt 
diff --git a/README.txt b/README.txt给i他
index c97f01c..b09e4cc 100644
--- a/README.txt
+++ b/README.txt
@@ -1,2 +1,3 @@
 hello world
 hello eagleslab
+hello linux

查询历史记录并退回

只能回退commit的数据

[root@server1 my_git]# git log
commit ca3e26c8f13e11f487c569b3b099d27b7661d067
Author: zj <zj@qq.com>
Date:   Tue Apr 20 20:36:01 2021 +0800

    append hello linux to README.txt

commit 176a26251d17eee0ad37cefd35eeb945539cacb3
Author: zj <zj@qq.com>
Date:   Tue Apr 20 20:33:05 2021 +0800

    append hello eagleslab to README.txt

commit 854d889c17ce2aa90101a2f6d40a7be511e6b4b5
Author: zj <zj@qq.com>
Date:   Tue Apr 20 20:29:49 2021 +0800

    write a README.txt file
[root@server1 my_git]# git log --oneline
ca3e26c append hello linux to README.txt
176a262 append hello eagleslab to README.txt
854d889 write a README.txt file
[root@server1 my_git]# git reset --hard 176a262
HEAD 现在位于 176a262 append hello eagleslab to README.txt
[root@server1 my_git]# cat README.txt 
hello world
hello eagleslab

查看所有历史记录

可以看到刚才回退的操作,也可以回退刚才回退的操作

[root@server1 my_git]# git reflog
176a262 HEAD@{0}: reset: moving to 176a262
ca3e26c HEAD@{1}: commit: append hello linux to README.txt
176a262 HEAD@{2}: commit: append hello eagleslab to README.txt
854d889 HEAD@{3}: commit (initial): write a README.txt file

修改文件名称

[root@server1 my_git]# git mv readme.txt README.txt 
[root@server1 my_git]# git commit -a -m "rename readme.txt to README.txt"
[master 6599d6c] rename readme.txt to README.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename readme.txt => README.txt (100%)

删除文件

[root@server1 my_git]# git rm README.txt 
rm 'README.txt'
[root@server1 my_git]# git commit -m "delete README.txt"
[master 84b4a87] delete README.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 README.txt

停止跟踪文件

创建仓库时,在本地仓库根目录,创建.gitignore文件,写入忽略规则。.gitignore只能忽略那些原来没有被track的文件。已经被追踪的过的文件,需要先用 git rm --cached语句删除追踪,再添加到.gitignore中防止下次被追踪。该操作会删除远程仓库中对应的文件或文件夹。

[root@server1 my_git]# touch .gitignore
[root@server1 my_git]# git rm README.txt --cached
rm 'README.txt'
[root@server1 my_git]# ls
README.txt
[root@server1 my_git]# vim .gitignore
README.txt

分支

一般在实际的项目开发中,尽量保持master分支是非常稳定的,仅用于发布新版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后再合并到master分支上面,所以团队的合作分支看起来会像很多树枝一样。

常规使用

列出分支

# 列出所有本地分⽀
$ git branch
# 列出所有远程分⽀
$ git branch -r
# 列出所有本地分⽀和远程分⽀
$ git branch -a

新建⼀个分⽀

# 新建⼀个分⽀,但依然停留在当前分⽀
$ git branch [branch-name]
# 新建⼀个分⽀,并切换到该分⽀
$ git checkout -b [branch]
# 新建⼀个分⽀,指向指定commit
$ git branch [branch] [commit]
# 新建⼀个分⽀,与指定的远程分⽀建⽴追踪关系
$ git branch --track [branch] [remote-branch]

切换分支

# 切换到指定分⽀,并更新⼯作区
$ git checkout [branch-name]
# 切换到上⼀个分⽀
$ git checkout

建⽴追踪关系

在现有分⽀与指定的远程分⽀之间

$ git branch --set-upstream [branch] [remote-branch]

合并指定分⽀到当前分⽀

$ git merge [branch]

选择⼀个commit,合并进当前分⽀

$ git cherry-pick [commit]

删除分⽀

# 删除分⽀
$ git branch -d [branch-name]
# 删除远程分⽀
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

标签使用

打标签

# 给当前状态打标签
$ git tag -a v1.0 -m " version 1.0"
# 指定某次状态打标签
$ git tag -a v2.0 [哈希值] -m "version 2.0"

检出版本v2.0

$ git checkout v2.0

查看标签

# 查看所有标签信息
git tag
# 查看指定标签信息
git show v1.0

删除标签

$ git tag -d v1.0

以标签回滚

$ git reset --hard v2.0

列出所有tag

$ git tag

新建⼀个tag在当前commit

$ git tag [tag]

新建⼀个tag在指定commit

$ git tag [tag] [commit]

删除本地tag

$ git tag -d [tag]

删除远程tag

$ git push origin :refs/tags/[tagName]

查看tag信息

$ git show [tag]

提交指定tag

$ git push [remote] [tag]

提交所有tag

$ git push [remote] --tags

新建⼀个分⽀,指向某个tag

$ git checkout -b [branch] [tag]

远程同步

# 下载远程仓库的所有变动
$ git fetch [remote]
# 显示所有远程仓库
$ git remote -v
# 显示某个远程仓库的信息
$ git remote show [remote]
# 增加⼀个新的远程仓库,并命名
$ git remote add [shortname] [url]
# 取回远程仓库的变化,并与本地分⽀合并
$ git pull [remote] [branch]
# 上传本地指定分⽀到远程仓库
$ git push [remote] [branch]
# 强⾏推送当前分⽀到远程仓库,即使有冲突
$ git push [remote] --force
# 推送所有分⽀到远程仓库
$ git push [remote] --all

撤销

# 恢复暂存区的指定⽂件到⼯作区
$ git checkout [file]
# 恢复某个commit的指定⽂件到暂存区和⼯作区
$ git checkout [commit] [file]
# 恢复暂存区的所有⽂件到⼯作区
$ git checkout .
# 重置暂存区的指定⽂件,与上⼀次commit保持⼀致,但⼯作区不变
$ git reset [file]
# 重置暂存区与⼯作区,与上⼀次commit保持⼀致
$ git reset --hard
# 重置当前分⽀的指针为指定commit,同时重置暂存区,但⼯作区不变
$ git reset [commit]
# 重置当前分⽀的HEAD为指定commit,同时重置暂存区和⼯作区,与指定commit⼀致
$ git reset --hard [commit]
# 重置当前HEAD为指定commit,但保持暂存区和⼯作区不变
$ git reset --keep [commit]
# 新建⼀个commit,⽤来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应⽤到当前分⽀
$ git revert [commit]
# 暂时将未提交的变化移除,稍后再移⼊
$ git stash
$ git stash pop

其他git操作

git init                                                  # 初始化本地git仓库(创建新仓库)
git config --global user.name "xxx"                       # 配置用户名
git config --global user.email "xxx@xxx.com"              # 配置邮件
git config --global color.ui true                         # git status等命令自动着色
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global --unset http.proxy                    # remove  proxy configuration on git
git clone git+ssh://git@192.168.53.168/VT.git             # clone远程仓库
git status                                                # 查看当前版本状态(是否修改)
git add abc                                               # 添加abc文件至index
git add .                                                 # 增加当前子目录下所有更改过的文件至index
git commit -m 'xxx'                                       # 提交
git commit --amend -m 'xxx'                               # 合并上一次提交(用于反复修改)
git commit -am 'xxx'                                      # 将add和commit合为一步
git rm xxx                                                # 删除index中的文件
git rm -r *                                               # 递归删除
git log                                                   # 显示提交日志
git log -1                                                # 显示1行日志 -n为n行
git log -5
git log --stat                                            # 显示提交日志及相关变动文件
git log -p -m
git show dfb02e6e4f2f7b573337763e5c0013802e392818         # 显示某个提交的详细内容
git show dfb02                                            # 可只用commitid的前几位
git show HEAD                                             # 显示HEAD提交日志
git show HEAD^                                            # 显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本
git tag                                                   # 显示已存在的tag
git tag -a v2.0 -m 'xxx'                                  # 增加v2.0的tag
git show v2.0                                             # 显示v2.0的日志及详细内容
git log v2.0                                              # 显示v2.0的日志
git diff                                                  # 显示所有未添加至index的变更
git diff --cached                                         # 显示所有已添加index但还未commit的变更
git diff HEAD^                                            # 比较与上一个版本的差异
git diff HEAD -- ./lib                                    # 比较与HEAD版本lib目录的差异
git diff origin/master..master                            # 比较远程分支master上有本地分支master上没有的
git diff origin/master..master --stat                     # 只显示差异的文件,不显示具体内容
git remote add origin git+ssh://git@192.168.53.168/VT.git # 增加远程定义(用于push/pull/fetch)
git branch                                                # 显示本地分支
git branch --contains 50089                               # 显示包含提交50089的分支
git branch -a                                             # 显示所有分支
git branch -r                                             # 显示所有原创分支
git branch --merged                                       # 显示所有已合并到当前分支的分支
git branch --no-merged                                    # 显示所有未合并到当前分支的分支
git branch -m master master_copy                          # 本地分支改名
git checkout -b master_copy                               # 从当前分支创建新分支master_copy并检出
git checkout -b master master_copy                        # 上面的完整版
git checkout features/performance                         # 检出已存在的features/performance分支
git checkout --track hotfixes/BJVEP933                    # 检出远程分支hotfixes/BJVEP933并创建本地跟踪分支
git checkout v2.0                                         # 检出版本v2.0
git checkout -b devel origin/develop                      # 从远程分支develop创建新本地分支devel并检出
git checkout -- README                                    # 检出head版本的README文件(可用于修改错误回退)
git merge origin/master                                   # 合并远程master分支至当前分支
git cherry-pick ff44785404a8e                             # 合并提交ff44785404a8e的修改
git push origin master                                    # 将当前分支push到远程master分支
git push origin :hotfixes/BJVEP933                        # 删除远程仓库的hotfixes/BJVEP933分支
git push --tags                                           # 把所有tag推送到远程仓库
git fetch                                                 # 获取所有远程分支(不更新本地分支,另需merge)
git fetch --prune                                         # 获取所有原创分支并清除服务器上已删掉的分支
git pull origin master                                    # 获取远程分支master并merge到当前分支
git mv README README2                                     # 重命名文件README为README2
git reset --hard HEAD                                     # 将当前版本重置为HEAD(通常用于merge失败回退)
git rebase
git branch -d hotfixes/BJVEP933                           # 删除分支hotfixes/BJVEP933(本分支修改已合并到其他分支)
git branch -D hotfixes/BJVEP933                           # 强制删除分支hotfixes/BJVEP933
git ls-files                                              # 列出git index包含的文件
git show-branch                                           # 图示当前分支历史
git show-branch --all                                     # 图示所有分支历史
git whatchanged                                           # 显示提交历史对应的文件修改
git revert dfb02e6e4f2f7b573337763e5c0013802e392818       # 撤销提交dfb02e6e4f2f7b573337763e5c0013802e392818
git ls-tree HEAD                                          # 内部命令:显示某个git对象
git rev-parse v2.0                                        # 内部命令:显示某个ref对于的SHA1 HASH
git reflog                                                # 显示所有提交,包括孤立节点
git show HEAD@{5}
git show master@{yesterday}                               # 显示master分支昨天的状态
git log --pretty=format:'%h %s' --graph                   # 图示提交日志
git show HEAD~3
git show -s --pretty=raw 2be7fcb476
git stash                                                 # 暂存当前修改,将所有至为HEAD状态
git stash list                                            # 查看所有暂存
git stash show -p stash@{0}                               # 参考第一次暂存
git stash apply stash@{0}                                 # 应用第一次暂存
git grep "delete from"                                    # 文件中搜索文本“delete from”
git grep -e '#define' --and -e SORT_DIRENT
git gc
git fsck
# 生成一个可供发布的压缩包
$ git archive