赞
踩
git属于分布式版本控制系统
没有中心代码仓库,所有机器之间的地位同等(每台机器上都有相同的代码)
客户端并不只提取最新版本的文件,而是把原始的代码仓库完整地克隆下来。
优点:
a.由于任何人每次提取操作,实际上都是一次对代码仓库的完整备份,因此近乎所有的操作都可以在本地执行,速度就是相当的快,并且可以在网络断开的时候操作仍然不受影响,可以频繁的进行提交更新,等到有网络的时候再上传到远程的仓库就可以了。
b.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仓库都会使用这个配置。
- &*git-server
- [root@git-server ~]# yum install -y git
- [root@git-server ~]# git --version
- git version 1.8.3.1
- [root@git-server ~]# git config --global user.email "tom@163.com" #邮箱
- [root@git-server ~]# git config --global user.name "tom" #用户
-
- &*client
- [root@client ~]# yum install -y git
- [root@client ~]# git config --global user.email "jack@163.com"
- [root@client ~]# git config --global user.name "jack"
- # cat /root/.gitconfig #查看邮箱信息
- # git config --global color.ui true #语法高亮
- # git config --list #查看全局配置
- 创建一个空目录:在中心服务器上创建
- [root@git-server ~]# mkdir /git-test
- [root@git-server ~]# useradd git #创建一个git用户用来运行git
- [root@git-server ~]# passwd git #给用户设置密码git
- [root@git-server ~]# cd /git-test/
- [root@git-server git-test]# git init --bare testgit
- Initialized empty Git repository in /git-test/testgit/
- [root@git-server ~]# chown git.git /git-test -R #修改权限
-
- 仓库创建完成后查看库目录:
- [root@git-server git-test]# cd testgit/
- [root@git-server testgit]# ls
- branches config description HEAD hooks info objects refs
- 先配置免密登录
- [root@client ~]# ssh-keygen #生成秘钥
- [root@client ~]# ssh-copy-id -i git@192.168.111.4 #将秘钥传输到git服务器中的git用户
-
- 克隆git仓库
- [root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
- Cloning into 'testgit'...
- warning: You appear to have cloned an empty repository.
- [root@client ~]# ls #查看仓库已经克隆下来了
- anaconda-ks.cfg testgit
- [root@client ~]# cd testgit/
- [root@client testgit]# vim test.txt #随便写点东西
- [root@client testgit]# git add test.txt
- 注: 这里可以使用 git add * 或者 git add -A
- [root@client testgit]# git commit -m "test1"
- [master (root-commit) 2b51ff9] test1
- 1 file changed, 2 insertions(+)
- create mode 100644 test.txt
- 注:-m:描述
- [root@client testgit]# git status
- # On branch master #分支位于master
- [root@client testgit]# echo '1122334' >> test.txt
- [root@client testgit]# git status
- # 位于分支 master
- # 尚未暂存以备提交的变更:
- # (使用 "git add <file>..." 更新要提交的内容)
- # (使用 "git checkout -- <file>..." 丢弃工作区的改动)
- #
- # 修改: readme.txt
- #
- 修改尚未加入提交(使用 "git add" 和/或 "git commit "
- [root@client testgit]# git add -A
- [root@client testgit]# git commit -m "add2"
- [master 73bf688] add2
- 1 file changed, 1 insertion(+)
- [root@client testgit]# git status
- # On branch master
- nothing to commit, working directory clean
- [root@client testgit]# git log
- 显示的哪个版本在第一个就是当前使用的版本。
- [root@vm20 gittest]# git reflog
- 2a85982 HEAD@{0}: reset: moving to 2a859821a2385e136fe83f3a206b287eb0eb8c18
- f5bc8c1 HEAD@{1}: commit: test-version2
- 2a85982 HEAD@{2}: commit (initial): test-version1
-
- [root@client testgit]# git reset --hard f5bc8c1
- HEAD is now at f5bc8c1 test-version2
- 在工作区的文件可直接删除,若文件在暂存区 则需要先从暂存区移除再删除
-
- [root@client testgit]# touch test.txt
- [root@client testgit]# git status
- # On branch master
- #
- # Initial commit
- #
- # Untracked files:
- # (use "git add <file>..." to include in what will be committed)
- #
- # test.txt
- nothing added to commit but untracked files present (use "git add" to track)
-
- [root@client testgit]# git add test.txt
- [root@client testgit]# git status
- # On branch master
- #
- # Initial commit
- #
- # Changes to be committed:
- # (use "git rm --cached <file>..." to unstage)
- #
- # new file: test.txt
- #
- [root@client testgit]# git rm --cache test.txt #从暂存区移除
- rm 'test.txt'
- [root@client testgit]# ls
- test.txt
- [root@client testgit]# git status
- # On branch master
- #
- # Initial commit
- #
- # Untracked files:
- # (use "git add <file>..." to include in what will be committed)
- #
- # test.txt
- nothing added to commit but untracked files present (use "git add" to track)
- [root@client testgit]# rm -rf test.txt
- [root@client testgit]# git status
- # On branch master
- #
- # Initial commit
- #
- nothing to commit (create/copy files and use "git add" to track)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- [root@client testgit]# vim a.txt #创建一个新文件
- [root@client testgit]# cat a.txt
- hello world
- [root@client testgit]# git add a.txt
- [root@client testgit]# git commit -m "add"
- [root@client testgit]# git push origin master #上传到中心仓库master分支
- Counting objects: 11, done.
- Compressing objects: 100% (4/4), done.
- Writing objects: 100% (11/11), 828 bytes | 0 bytes/s, done.
- Total 11 (delta 0), reused 0 (delta 0)
- To git@192.168.246.214:/git-test/testgit/
- * [new branch] master -> master
在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件
- [root@client testgit]# cd
- [root@client ~]# rm -rf testgit/
- [root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
- Cloning into 'testgit'...
- remote: Counting objects: 11, done.
- remote: Compressing objects: 100% (4/4), done.
- remote: Total 11 (delta 0), reused 0 (delta 0)
- Receiving objects: 100% (11/11), done.
- [root@client ~]# cd testgit/
- [root@client testgit]# ls
- a.txt
- [root@client testgit]# cat a.txt
- hello world
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。