git版本控制器应用实例

安装git

[root@git-server ~]# yum -y install git
[root@git-server ~]# git --version 
git version 1.8.3.1
添加邮件和用户  所有机器都要添加用户和邮箱
[root@git-server ~]# git config --global user.email "estarhaohao@163.com"
[root@git-server ~]# git config --global user.name "mahaohao"
[root@git-server ~]# git config --global color.ui true    语法高亮
[root@git-server ~]# cat /root/.gitconfig 
[user]
	email = estarhaohao@163.com
	name = mahaohao
[color]
	ui = true

创建版本库

[root@git-server ~]# mkdir -p /test-git
[root@git-server ~]# useradd git
[root@git-server ~]# passwd git
Changing password for user git.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@git-server ~]# cd /test-git/

创建裸库

[root@git-server test-git]# git init --bare testgit 
Initialized empty Git repository in /test-git/testgit/
[root@git-server test-git]# ls
testgit
[root@git-server test-git]# chown -R git.git /test-git/
[root@git-server test-git]# ll /test-git/
total 0
drwxr-xr-x 7 git git 119 May  6 20:21 testgit

客户端密钥

安装git
[root@git-client ~]# yum -y install git
配置密钥,免密登录
[root@git-client ~]# ssh-keygen 
[root@git-client ~]# ssh-copy-id -i git@192.168.234.101
克隆仓库
[root@git-client ~]# git clone git@192.168.234.101:/test-git/testgit
Cloning into 'testgit'...
warning: You appear to have cloned an empty repository.
[root@git-client ~]# ls
testgit

模拟代码上传到仓库
上传代码文件按测试

[root@git-client testgit]# rz
[root@git-client testgit]# ls
wechat.py
添加文件到寄存区
[root@git-client testgit]# git add wechat.py
注: 这里可以使用 git add * 或者 git add -A
提交代码文件到仓库有分支
[root@git-client testgit]# git commit -m "version1.1.0" 
[master (root-commit) b6a6953] version1.1.0
 1 file changed, 84 insertions(+)
 create mode 100644 wechat.py
-m  描述   可以写一些版本信息
查看git状态
[root@git-client testgit]# git status 
# On branch master    分支位于mastrer
nothing to commit, working directory clean   
修改代码文件按再看git状态
[root@git-client testgit]# echo 123 >> wechat.py 
[root@git-client testgit]# 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:   wechat.py
#
no changes added to commit (use "git add" and/or "git commit -a")
先add 
[root@git-client testgit]# git add wechat.py
在提交
[root@git-client testgit]# git commit -m "version1.2.1" wechat.py
[master e3d0cc8] version1.2.1
 1 file changed, 1 insertion(+)
在查看git状态
[root@git-client testgit]# git status 
# On branch master
nothing to commit, working directory clean

版本回退
查看当前的版本

[root@git-client testgit]# git log 
commit e3d0cc88515360030ee9019c512d2c56bda964a4    版本的id号
Author: mahaohao <estarhaohao@163.com>
Date:   Wed May 6 21:15:45 2020 +0800

    version1.2.1   当前的版本

commit b6a6953c66077f5d6414f65a74a3105fb1577b1c
Author: mahaohao <estarhaohao@163.com>
Date:   Wed May 6 21:10:42 2020 +0800

    version1.1.0

一般都用版本的id号来回退版本
回退到上一个版本

[root@git-client testgit]# git reset --hard HEAD^
HEAD is now at b6a6953 version1.1.0

通过id号来回退

[root@git-client testgit]# git reset --hard e3d0cc88515
HEAD is now at e3d0cc8 version1.2.1

消失的id号可以用git reflog 查看

[root@git-client testgit]# git reflog
e3d0cc8 HEAD@{0}: reset: moving to e3d0cc88515
b6a6953 HEAD@{1}: reset: moving to HEAD^
e3d0cc8 HEAD@{2}: commit: version1.2.1
b6a6953 HEAD@{3}: commit (initial): version1.1.0

删除文件
没有上传到寄存区可以直接删除

[root@git-client testgit]# ls
clean.sh  wechat.py
[root@git-client testgit]# rm -rf clean.sh 
[root@git-client testgit]# git status 
# On branch master
nothing to commit, working directory clean

已经上传到寄存区

[root@client testgit]# git add clean.sh
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   clean.sh
[root@git-client testgit]# git rm --cache clean.sh 
或
[root@git-client testgit]# git rm --f clean.sh
rm 'clean.sh'
[root@git-client testgit]# ls
clean.sh  wechat.py
[root@git-client testgit]# rm -rf clean.sh 
[root@git-client testgit]# ls
wechat.py
[root@git-client testgit]# git status 
# On branch master
nothing to commit, working directory clean

将代码上传到仓库master分支

[root@git-client testgit]# git add clean.sh 
[root@git-client testgit]# git commit -m "clean1.3.2"
[master 25f8cfb] clean1.3.2
 1 file changed, 33 insertions(+)
 create mode 100644 clean.sh
[root@git-client testgit]# git push origin master
Counting objects: 9, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 2.02 KiB | 0 bytes/s, done.
Total 9 (delta 1), reused 0 (delta 0)
To git@192.168.234.101:/test-git/testgit
 * [new branch]      master -> master

测试

客户端删除掉自己的库然后克隆
[root@git-client ~]# ls
testgit
[root@git-client ~]# rm -rf testgit/
[root@git-client ~]# ls
[root@git-client ~]# git clone git@192.168.234.101:/test-git/testgit
Cloning into 'testgit'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 9 (delta 1), reused 0 (delta 0)
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.
[root@git-client ~]# ls
testgit
[root@git-client ~]# cd testgit/
[root@git-client testgit]# ls
clean.sh  wechat.py

创建分支合并分支

[root@git-client testgit]# git status 
# On branch master
nothing to commit, working directory clean
创建分支
[root@git-client testgit]# git branch dev 
[root@git-client testgit]# git status 
# On branch master
nothing to commit, working directory clean
查看所在的分支
[root@git-client testgit]# git branch 
  dev
* master
切换分支
[root@git-client testgit]# git checkout dev 
Switched to branch 'dev'
[root@git-client testgit]# git branch 
* dev      *  代表当前所在分支
  master
[root@git-client testgit]# ls
clean.sh  wechat.py
[root@git-client testgit]# vim port.txt
[root@git-client testgit]# ls
clean.sh  port.txt  wechat.py
[root@git-client testgit]# git add port.txt 
[root@git-client testgit]# git commit -m "add dev"
[dev 03a52f0] add dev
 1 file changed, 1 insertion(+)
 create mode 100644 port.txt
[root@git-client testgit]# git checkout master 
Switched to branch 'master'
[root@git-client testgit]# ls
clean.sh  wechat.py
[root@git-client testgit]# git checkout dev 
Switched to branch 'dev'
[root@git-client testgit]# ls
clean.sh  port.txt  wechat.py

合并分支

[root@git-client testgit]# git merge dev 
Updating 25f8cfb..03a52f0
Fast-forward
 port.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 port.txt
[root@git-client testgit]# git branch 
  dev
* master
[root@git-client testgit]# ls
clean.sh  port.txt  wechat.py

删除分支

[root@git-client testgit]# git branch -d dev 
Deleted branch dev (was 03a52f0).
[root@git-client testgit]# git branch 
* master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值