【12.24】代码管理平台 git 2

本文详细介绍了Git的远程分支管理,包括如何查看远程分支、创建和推送本地分支。同时,讲解了Git的标签管理,如何创建、查看、删除和推送标签。此外,还介绍了如何设置Git别名以简化常用命令。最后,简单概述了如何搭建私有的Git服务器。
摘要由CSDN通过智能技术生成

22.10 远程分支管理

使用分支的原则:

  • master 分支非常重要,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。

  • 创建一个 dev 分支,专门用作开发,当发布到线上之前,才会把 dev 分支合并到 master

  • 开发人员应该在 dev 的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到 dev 分支
    在这里插入图片描述
    dev分支合并bob分支的命令是:
    git checkout dev //先切换到dev分支,然后
    git merge bob

  • 远程分支:
    git ls-remote origin 查看远程分支

[root@arslinux-01 tmp]# git clone https://github.com/axxxxxx4xxxx/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 15 (delta 0), reused 12 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
[root@arslinux-01 tmp]# cd studygit/
[root@arslinux-01 studygit]# git branch
* master
[root@arslinux-01 studygit]# git ls-remote origin
38e08903596878b892452d53aa96dda7b76a7c64	HEAD
38e08903596878b892452d53aa96dda7b76a7c64	refs/heads/dev
38e08903596878b892452d53aa96dda7b76a7c64	refs/heads/master

——本地新建的分支如果不推送到远程,对其他人就是不可见的
——git clone 的时候默认只把 master 分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用 git checkout -b 分支名 origin/分支名,本地和远程分支的名称要一致

git checkout -b 分支名 origin/分支名 本地创建和远程分支对应的分支

[root@arslinux-01 studygit]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
[root@arslinux-01 studygit]# git branch
* dev
  master

——对于 git push 分支分两种情况:
1)当本地分支和远程分支一致时
git push 会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用 git push origin 分支名
2)当本地分支比远程分支多
默认 git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用 git push origin 分支名 如果推送失败,先用 git pull 抓取远程的新提交

[root@arslinux-01 studygit]# echo "ssssssss">2.txt
[root@arslinux-01 studygit]# echo "bbbbbbbb">>2.txt
[root@arslinux-01 studygit]# cat 2.txt 
ssssssss
bbbbbbbb
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[dev e7340f2] add 2.txt
 1 file changed, 2 insertions(+), 5 deletions(-)
[root@arslinux-01 studygit]# git push origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxxx/studygit.git
   38e0890..e7340f2  dev -> dev
[root@arslinux-01 studygit]# git branch dev2
[root@arslinux-01 studygit]# git checkout dev2 
切换到分支 'dev2'
[root@arslinux-01 studygit]# echo "aaaaaaaaa" > 3.txt
[root@arslinux-01 studygit]# git add 3.txt
[root@arslinux-01 studygit]# git commit -m "add 3.txt"
[dev2 bdf6b7d] add 3.txt
 1 file changed, 1 insertion(+)
 create mode 100644 3.txt
[root@arslinux-01 studygit]# git push origin dev2
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xxxxxxx4xxxx/studygit.git
   e7340f2..bdf6b7d  dev2 -> dev2

22.11 标签管理

标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态。也可以随时恢复到该状态
git tag v1.0 给master打一个标签v1.0
git tag 可以查看所有的标签
git show v1.0 查看标签信息

[root@arslinux-01 studygit]# git checkout master
切换到分支 'master'
[root@arslinux-01 studygit]# git tag v1.0
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git show v1.0
commit 38e08903596878b892452d53aa96dda7b76a7c64
Author: arslinux <zyxxxxxxxx@qq.com>
Date:   Thu Jul 25 22:37:14 2019 +0800

    change2 2.txt

diff --git a/2.txt b/2.txt
index 77d3d2a..07ced64 100644
--- a/2.txt
+++ b/2.txt
@@ -1,4 +1,5 @@
 73737372hhdjdjd
 thank you
 go go go
+dlj;adkjf;adfas
 ore wa neko ga suki

——tag 是针对 commit 来打标签的,所以可以针对历史的 commit 来打 tag

  • git log --pretty=oneline 查看历史 commit
  • git log --pretty=oneline --abbrev-commit 查看历史 commit,用简写 commit
  • git tag v版本号 commit值 针对历史 commit 打标签
  • git tag -a v版本号 -m “描述” commit值 对标签进行描述
  • git tag -d v版本号 删除标签
  • git push origin v版本号 推送指定标签到远程
  • git push --tag v版本号 推送所有标签到远程
  • git tag -d v版本号 删除本地标签
  • git push origin:refs/tags/v版本号 删除远程标签
[root@arslinux-01 studygit]# git log --pretty=oneline
38e08903596878b892452d53aa96dda7b76a7c64 change2 2.txt
cce24941a0e74d9c94cd05f1a59e6993ac54ca01 Update 2.txt
b2c0aa1d1b2749eeb0958ca772c378790651dd0f change README.md
6e4549cbe9f7d415eb219018b3fbbfc2e1e42c98 add 2.txt
f6afa16c437cf2917d220aace0e01963575b7383 first commit
[root@arslinux-01 studygit]# git log --pretty=oneline --abbrev-commit
38e0890 change2 2.txt
cce2494 Update 2.txt
b2c0aa1 change README.md
6e4549c add 2.txt
f6afa16 first commit

[root@arslinux-01 studygit]# git tag v0.8 6e4549cbe9f7d
[root@arslinux-01 studygit]# git tag
v0.8
v1.0

[root@arslinux-01 studygit]# git tag -a v0.1 -m "first tag" f6afa16c4
[root@arslinux-01 studygit]# git show v0.1
tag v0.1
Tagger: arslinux <zy77xp2316@qq.com>
Date:   Sat Jul 27 22:18:25 2019 +0800

first tag

commit f6afa16c437cf2917d220aace0e01963575b7383
Author: arslinux <zy77xp2316@qq.com>
Date:   Thu Jul 25 22:07:51 2019 +0800

    first commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..eb8db93
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# studygit

[root@arslinux-01 studygit]# git tag -d v0.1
已删除 tag 'v0.1'(曾为 7db3981)

[root@arslinux-01 studygit]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
 * [new tag]         v1.0 -> v1.0
[root@arslinux-01 studygit]# git push --tag origin
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
 * [new tag]         v0.8 -> v0.8

[root@arslinux-01 studygit]# git tag -d v0.8
已删除 tag 'v0.8'(曾为 6e4549c)
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git push origin :refs/tags/v0.8
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com': 
To https://github.com/arsenal4life/studygit.git
 - [deleted]         v0.8

22.12 git 别名

  • git config --global alias.别名 命令 设置别名
  • git config --list 查看所有配置
  • git config --list |grep alias 查看别名
  • git config --global --unset alias.别名 取消别名
[root@arslinux-01 studygit]# git config --global alias.ci commit
[root@arslinux-01 studygit]# echo "dafafa" >4.txt
[root@arslinux-01 studygit]# git add 4.txt
[root@arslinux-01 studygit]# git ci -m "add 4.txt"
[master 993ce4b] add 4.txt
 1 file changed, 1 insertion(+)
 create mode 100644 4.txt
[root@arslinux-01 studygit]# git config --global alias.br branch
[root@arslinux-01 studygit]# git br
  dev
  dev2
* master
[root@arslinux-01 studygit]# git config --global alias.co checkout
[root@arslinux-01 studygit]# git co dev
切换到分支 'dev'
[root@arslinux-01 studygit]# git config --list
user.email=zxxxxxxxx@qq.com
user.name=arslinux
push.default=simple
alias.ci=commit
alias.br=branch
alias.co=checkout
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/arsenal4life/studygit.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.dev.remote=origin
branch.dev.merge=refs/heads/dev
[root@arslinux-01 studygit]# git config --list|grep alias
alias.ci=commit
alias.br=branch
alias.co=checkout
git config 的配置再 /root/.gitconfig 中定义
[root@arslinux-01 studygit]# git config --global --unset alias.br
[root@arslinux-01 studygit]# git br
git:'br' 不是一个 git 命令。参见 'git --help'。

您指的是这其中的某一个么?
	branch
	var
  • 查询 log 小技巧:(颜色区分显示)
    git config --global alias.lg “log --color --graph --pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ --abbrev-commit”
    在这里插入图片描述

22.13 搭建 git 服务器

github毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。Gitlab是个不错的选择。
1、安装 git,arslinux-02 做为服务端

[root@arslinux-02 ~]# yum install -y git

2、添加 git 用户,并设置 shell 为 /usr/bin/git-shell,目的是为了不让 git 用户远程登陆

[root@arslinux-02 ~]# useradd -s /usr/bin/git-shell git

3、创建 authorized_keys 文件,并更改属主、属组和权限,用来存客户端机器上的公钥

[root@arslinux-02 ~]# cd /home/git/
[root@arslinux-02 git]# mkdir .ssh
[root@arslinux-02 git]# touch .ssh/authorized_keys
[root@arslinux-02 git]# chmod 600 .ssh/authorized_keys 
[root@arslinux-02 git]# chown -R git:git .ssh

4、将公钥添加到服务端 authorized_keys 文件中,并在客户端尝试连接

[root@arslinux-02 git]# vim .ssh/authorized_keys
[root@arslinux-01 ~]# ssh git@192.168.194.132
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.194.132 closed.

5、服务端创建 git 仓库,创建裸仓库

[root@arslinux-02 git]# cd /data/
[root@arslinux-02 data]# mkdir /data/gitroot
[root@arslinux-02 data]# cd /data/gitroot/
[root@arslinux-02 gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[root@arslinux-02 gitroot]# ls
sample.git
[root@arslinux-02 gitroot]# chown -R git:git sample.git/
  • git init --bare sample.git // 会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
  • 以上操作是在git服务器上做的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的

6、客户端上克隆远程仓库

[root@arslinux-01 ~]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一个空版本库。
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ll -a
总用量 4
drwxrwxr-x   3 root root   18 7月  28 12:19 .
dr-xr-x---. 10 root root 4096 7月  28 12:19 ..
drwxrwxr-x   7 root root  119 7月  28 12:19 .git

7、创建新文件到仓库,推送到远程

[root@arslinux-01 sample]# cp /etc/init.d/mysqld .
[root@arslinux-01 sample]# git add mysqld
[root@arslinux-01 sample]# git commit -m "add mysqld"
[master(根提交) 7f37e19] add mysqld
 1 file changed, 378 insertions(+)
 create mode 100755 mysqld
[root@arslinux-01 sample]# git push
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.84 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
 * [new branch]      master -> master

如果 git push 提示没有分支,可以使用 git push origin master 来操作,会在裸仓库创建新分支
8、再推送新文件就不会提示了

[root@arslinux-01 sample]# echo "dafasdfasdf" >222.txt
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "add 222.txt"
[master a9de871] add 222.txt
 1 file changed, 1 insertion(+)
 create mode 100644 222.txt
[root@arslinux-01 sample]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
   7f37e19..a9de871  master -> master

9、不妨到 /tmp/ 目录下,克隆服务端的 sample.git,可以看到,客户端新建文件已经推到服务端了

[root@arslinux-01 sample]# cd /tmp/
[root@arslinux-01 tmp]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), 4.09 KiB | 0 bytes/s, done.
[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld

10、如果有多个服务器连接 git 服务端,而且其他服务器对服务端做了更改,那么客户端可以 git pull 来更新仓库(/tmp/sample/ 和 /data/sample/ 就当做两个服务器)

[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# echo "435678" >> 222.txt 
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "ch 222.txt"
[master 4e5ad08] ch 222.txt
 1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# git push
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
   a9de871..4e5ad08  master -> master
[root@arslinux-01 sample]# cd
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 192.168.194.132:/data/gitroot/sample
   a9de871..4e5ad08  master     -> origin/master
更新 a9de871..4e5ad08
Fast-forward
 222.txt | 1 +
 1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# cat 222.txt 
dafasdfasdf
435678

这样就可以非常方便协同操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值