当前位置:   article > 正文

Git的部署与使用_git server 创建仓库

git server 创建仓库

1、Git的介绍

git属于分布式版本控制系统

没有中心代码仓库,所有机器之间的地位同等(每台机器上都有相同的代码)

客户端并不只提取最新版本的文件,而是把原始的代码仓库完整地克隆下来。

优点:

a.由于任何人每次提取操作,实际上都是一次对代码仓库的完整备份,因此近乎所有的操作都可以在本地执行,速度就是相当的快,并且可以在网络断开的时候操作仍然不受影响,可以频繁的进行提交更新,等到有网络的时候再上传到远程的仓库就可以了。

b.git的分支模型,相当的轻量级,被称为“必杀技”。

1.git的部署

环境:

git-server  192.168.111.4   充当中心代码仓库服务器

client         192.168.111.9   git可视化工具

所有机器关闭防火墙和selinux:systemctl stop firewalld && setenforce 0

因为Git是分布式版本控制系统,所以,每个机器都必须注册:你的名字和Email地址。
注:git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置。

  1. &*git-server
  2. [root@git-server ~]# yum install -y git
  3. [root@git-server ~]# git --version
  4. git version 1.8.3.1
  5. [root@git-server ~]# git config --global user.email "tom@163.com" #邮箱
  6. [root@git-server ~]# git config --global user.name "tom" #用户
  7. &*client
  8. [root@client ~]# yum install -y git
  9. [root@client ~]# git config --global user.email "jack@163.com"
  10. [root@client ~]# git config --global user.name "jack"
  1. # cat /root/.gitconfig #查看邮箱信息
  2. # git config --global color.ui true #语法高亮
  3. # git config --list #查看全局配置

1.创建版本库

  1. 创建一个空目录:在中心服务器上创建
  2. [root@git-server ~]# mkdir /git-test
  3. [root@git-server ~]# useradd git #创建一个git用户用来运行git
  4. [root@git-server ~]# passwd git #给用户设置密码git
  5. [root@git-server ~]# cd /git-test/

2.创建裸库

  1. [root@git-server git-test]# git init --bare testgit
  2. Initialized empty Git repository in /git-test/testgit/
  3. [root@git-server ~]# chown git.git /git-test -R #修改权限
  4. 仓库创建完成后查看库目录:
  5. [root@git-server git-test]# cd testgit/
  6. [root@git-server testgit]# ls
  7. branches config description HEAD hooks info objects refs

 3.client克隆库

  1. 先配置免密登录
  2. [root@client ~]# ssh-keygen #生成秘钥
  3. [root@client ~]# ssh-copy-id -i git@192.168.111.4 #将秘钥传输到git服务器中的git用户
  4. 克隆git仓库
  5. [root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
  6. Cloning into 'testgit'...
  7. warning: You appear to have cloned an empty repository.
  8. [root@client ~]# ls #查看仓库已经克隆下来了
  9. anaconda-ks.cfg testgit

2.git的使用

1.创建文件模拟代码提交到仓库

1.在testgit目录下创建一个测试文件test.txt
  1. [root@client ~]# cd testgit/
  2. [root@client testgit]# vim test.txt #随便写点东西
 2.把文件添加到暂存区:使用 "git add" 建立跟踪
  1. [root@client testgit]# git add test.txt
  2. 注: 这里可以使用 git add * 或者 git add -A
3.提交文件到仓库分支 
  1. [root@client testgit]# git commit -m "test1"
  2. [master (root-commit) 2b51ff9] test1
  3. 1 file changed, 2 insertions(+)
  4. create mode 100644 test.txt
  5. 注:-m:描述
4.查看git状态 
  1. [root@client testgit]# git status
  2. # On branch master #分支位于master
 5.修改文件后再此查看状态:
  1. [root@client testgit]# echo '1122334' >> test.txt
  2. [root@client testgit]# git status
  3. # 位于分支 master
  4. # 尚未暂存以备提交的变更:
  5. # (使用 "git add <file>..." 更新要提交的内容)
  6. # (使用 "git checkout -- <file>..." 丢弃工作区的改动)
  7. #
  8. # 修改: readme.txt
  9. #
  10. 修改尚未加入提交(使用 "git add" 和/或 "git commit "
6.先add再次提交commit
  1. [root@client testgit]# git add -A
  2. [root@client testgit]# git commit -m "add2"
  3. [master 73bf688] add2
  4. 1 file changed, 1 insertion(+)
  5. [root@client testgit]# git status
  6. # On branch master
  7. nothing to commit, working directory clean

2.版本回退

1.查看版本
  1. [root@client testgit]# git log
  2. 显示的哪个版本在第一个就是当前使用的版本。
2.切换指定版本(根据版本号)
  1. [root@vm20 gittest]# git reflog
  2. 2a85982 HEAD@{0}: reset: moving to 2a859821a2385e136fe83f3a206b287eb0eb8c18
  3. f5bc8c1 HEAD@{1}: commit: test-version2
  4. 2a85982 HEAD@{2}: commit (initial): test-version1
  5. [root@client testgit]# git reset --hard f5bc8c1
  6. HEAD is now at f5bc8c1 test-version2
3.删除文件
  1. 在工作区的文件可直接删除,若文件在暂存区 则需要先从暂存区移除再删除
  2. [root@client testgit]# touch test.txt
  3. [root@client testgit]# git status
  4. # On branch master
  5. #
  6. # Initial commit
  7. #
  8. # Untracked files:
  9. # (use "git add <file>..." to include in what will be committed)
  10. #
  11. # test.txt
  12. nothing added to commit but untracked files present (use "git add" to track)
  13. [root@client testgit]# git add test.txt
  14. [root@client testgit]# git status
  15. # On branch master
  16. #
  17. # Initial commit
  18. #
  19. # Changes to be committed:
  20. # (use "git rm --cached <file>..." to unstage)
  21. #
  22. # new file: test.txt
  23. #
  24. [root@client testgit]# git rm --cache test.txt #从暂存区移除
  25. rm 'test.txt'
  26. [root@client testgit]# ls
  27. test.txt
  28. [root@client testgit]# git status
  29. # On branch master
  30. #
  31. # Initial commit
  32. #
  33. # Untracked files:
  34. # (use "git add <file>..." to include in what will be committed)
  35. #
  36. # test.txt
  37. nothing added to commit but untracked files present (use "git add" to track)
  38. [root@client testgit]# rm -rf test.txt
  39. [root@client testgit]# git status
  40. # On branch master
  41. #
  42. # Initial commit
  43. #
  44. nothing to commit (create/copy files and use "git add" to track)

3.将代码上传到仓库的master分支

  1. [root@client testgit]# vim a.txt #创建一个新文件
  2. [root@client testgit]# cat a.txt
  3. hello world
  4. [root@client testgit]# git add a.txt
  5. [root@client testgit]# git commit -m "add"
  6. [root@client testgit]# git push origin master #上传到中心仓库master分支
  7. Counting objects: 11, done.
  8. Compressing objects: 100% (4/4), done.
  9. Writing objects: 100% (11/11), 828 bytes | 0 bytes/s, done.
  10. Total 11 (delta 0), reused 0 (delta 0)
  11. To git@192.168.246.214:/git-test/testgit/
  12. * [new branch] master -> master

3.测试

在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件

  1. [root@client testgit]# cd
  2. [root@client ~]# rm -rf testgit/
  3. [root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
  4. Cloning into 'testgit'...
  5. remote: Counting objects: 11, done.
  6. remote: Compressing objects: 100% (4/4), done.
  7. remote: Total 11 (delta 0), reused 0 (delta 0)
  8. Receiving objects: 100% (11/11), done.
  9. [root@client ~]# cd testgit/
  10. [root@client testgit]# ls
  11. a.txt
  12. [root@client testgit]# cat a.txt
  13. hello world
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/570653
推荐阅读
相关标签
  

闽ICP备14008679号