git & gitlab的搭建和部署


環境准備:

三台機器,一台做客戶端(程序員上傳代碼用)192.168.1.10,一台做git服務器192.168.1.20(內存設置為4GB),一台做jenkins 192.168.1.30

git: 分布式軟件版本控制系統,獨立使用的,

1.安裝: yum -y install git

2.配置基本信息

git config --global user.name "Mr Zhao"      配置用戶名

git config --global userr.email "550418723@qq.com"   配置郵箱

git config --global core.editor vim           配置編輯器

3.查看信息

git config --list

cat  ~/.gitconfig   配置文件所在處,可以修改上面的基本信息

4. git的重要工作區域

工作區:編寫代碼的工作目錄

暫存區:.git/index,工作區和版本庫之間的緩沖地帶,允許用戶后悔的區域

版本庫:工作區有一個.git目錄,這個就是版本庫

工作區 --git add --> 暫存區-->git commit-->版本庫

5.創建倉庫

方法一: 創建項目之初創建

git init mygit   

初始化空的 Git 版本庫於 /root/mygit/.git/

方法二,在已存在的項目中創建版本庫

mkdir myweb

cd myweb

echo "hello linux" > index.html

git init

初始化空的 Git 版本庫於 /root/myweb/.git/

ls -A

.git  index.html

[root@client myweb]# git status -s
A  index.html
?? hosts     #未提交到暫存區會出現問號
[root@client myweb]# git add .    #將所有文件提交到暫存區
[root@client myweb]# git status -s
A  hosts
A  index.html


從暫存區撤出hosts

[root@client myweb]# git rm --cached hosts     #提交到暫存區后,發現又有需要修改的了,執行此命令
rm 'hosts'
[root@client myweb]# git status -s   撤出查看就變成了 ??
A  index.html
?? hosts

創建 .gitignore 忽略不需要加入到版本庫的文件hosts

[root@client myweb]# echo hosts >> .gitignore    # hosts不需要加入到版本庫,故將hosts加入到gitignore

[root@client myweb]# echo .gitignore >> .gitignore
[root@client myweb]# cat .gitignore
hosts

.gitignore
[root@client myweb]# git status -s
A  index.html
6.提交到版本倉庫

git commit   #直接敲會出現vim編輯器寫日志,,如果是空白日志,終止提交,也就是上面所定義的core vim定義的

git commit -m "init data"

[root@client myweb]# git commit -m "init data"
[master(根提交) a8474aa] init data
 1 file changed, 1 insertion(+)
 create mode 100644 index.htmlecho hosts >> .gitignore
[root@client myweb]# git status
# 位於分支 master
無文件要提交,干凈的工作區
7.刪除工作區的文件並恢復:

[root@client myweb]# rm -rf index.html
[root@client myweb]# git status
# 位於分支 master
# 尚未暫存以備提交的變更:
#   (使用 "git add/rm <file>..." 更新要提交的內容)
#   (使用 "git checkout -- <file>..." 丟棄工作區的改動)
#
#    刪除:      index.html
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

8.恢復刪除的index.html

[root@client myweb]# git checkout -- index.html
[root@client myweb]# ls
hosts  index.html
9.從版本庫刪除文件

[root@client myweb]# git ls-files
hosts1
index.html
[root@client myweb]# git rm hosts1
rm 'hosts1'
[root@client myweb]# git ls-files
index.html

[root@client myweb]# git status
# 位於分支 master
# 要提交的變更:
#   (使用 "git reset HEAD <file>..." 撤出暫存區)
#
#    刪除:      hosts1
#
10.徹底刪除 (沒有后悔葯了)

[root@client myweb]# git commit -m "rm hosts1"
[master 9898403] rm hosts1
 1 file changed, 1 deletion(-)
 delete mode 100644 hosts1
[root@client myweb]# git status
# 位於分支 master
無文件要提交,干凈的工作區
11.恢復誤刪除的文件(再通過版本庫恢復找回來)

[root@client myweb]# git log
commit 98984031e3d15efe9d77ed81a9f8c6aaa40c7ed3
Author: Mr.Zhao <550418723@qq.com>
Date:   Mon Nov 18 11:52:21 2019 +0800

commit aedf50f5a42a93aa930d6888946e4aa8b3fff863
Author: Mr.Zhao <550418723@qq.com>
Date:   Mon Nov 18 11:49:39 2019 +0800


[root@client myweb]# git checkout aedf50f5a42a93aa930d6888946e4aa8b3fff863
Note: checking out 'aedf50f5a42a93aa930d6888946e4aa8b3fff863'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD 目前位於 aedf50f... hosts1
[root@client myweb]# ls
hosts1  index.html

git tag 標記

tag可以為模擬過一次設置標記

如將某一次提交設置為軟件版本號

[root@client myweb]# git tag 2.0
[root@client myweb]# git tag
1.0
2.0
git 分支

git

[root@client myweb]# git add .
[root@client myweb]# git commit -m "add passwd"
# 頭指針分離自 a8474aa
無文件要提交,干凈的工作區
[root@client myweb]# ls
hello  index.html  passwd
[root@client myweb]# git checkout b1
切換到分支 'b1'
[root@client myweb]# ls
hello  index.html  passwd
[root@client myweb]# git checkout b1
切換到分支 'b1'
[root@client myweb]# ls
hello  index.html  passwd
[root@client myweb]# git checkout master
切換到分支 'master'
[root@client myweb]# ls
index.html
[root@client myweb]# git branch 查看分支
git branch 查看分支
[root@client myweb]# git rm passwd
rm 'passwd'
[root@client myweb]# git commit -m *
[master aecac25] *
 5 files changed, 24 deletions(-)
 delete mode 100644 a.txt
 delete mode 100644 hello
 delete mode 100644 hi.txt
 delete mode 100644 index.html
 delete mode 100644 passwd
[root@client myweb]# git status
# 位於分支 master
無文件要提交,干凈的工作區
gitlab服務器
環境准備

gitlab服務器(至少4GB內存),首先給機器擴大內存

安裝docker 

1.配置yum 源(華為雲yum源)

wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo

 2.安裝docker並啟動

yum -y install docker

systemctl start docker

systemctl enable docker

3.將鏡像導入

docker load < gitlab_zh.tar  (從真機/linuxsoft/上拷貝)

 4.部署gitlab

1.將docker 宿主機的ssh 端口號改為2022

port 2202

systemctl restart sshd

 ssh -p2022 root@192.168.1.20  #以修改后的端口來登錄

登陸上去后  ss -nutlp | grep 2022      查看端口是否變了

2.啟動容器

[root@git ~] docker run -d -h gitlab --name gitlab -p 443:443 -p 80:80 -p 22:22 --restart always -v /srv/gitlab/config:/etc/gitlab -v /srv/gitlab/logs:/var/log/gitlab -v /srv/gitlab/data:/var/opt/gitlab gitlab_zh:latest

3.查看容器狀態,當容器的狀態為healthy時,容器才是可用的

 docker ps
1e7286081c47        gitlab_zh:latest    "/assets/wrapper"   4 minutes ago       Up 4 minutes (healthy)   0.0.0.0:22->22/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   gitlab
4.gitlab 應用

1.登錄服務器,設置root密碼

192.168.1.20,設置root密碼,用戶root登錄,輸入修改的密碼(密碼必須設置復雜,如1234.com)

 

 gitlab中重要的概念

1.群組group:對應一個開發團隊

2.用戶,成員member:對應用戶賬戶,可以將用戶加入到組或者項目

3.項目:對應軟件項目

 

 

 1.創建群組.叫devops

 

 2.新建一個用戶,新建用戶時,不能設置密碼,點創建后,在右上角的編輯處設置密碼

 

 

 進入編輯后,設置密碼為 zhaoxuan123 ,輸入兩遍

 3.創建項目,為devops組創建項目,項目名稱為myweb

 

 

 點擊項目左邊欄展開,設置-->成員->要要請的成員

為項目授權->將普通用戶加入到項目->設置為主程序員

 

 

 切換為普通用戶,上傳代碼

新用戶首次登錄時,需要改密碼,

 新用戶為 zx  不是Zhao.Xuan

修改后用普通用戶登錄進去,查看你的代碼

 

 

程序員上傳代碼到gitlab上的devops倉庫

先cd到myweb目錄下

git remote help add  查看幫助

 [root@client myweb]# git remote add origin http://192.168.1.20/devops/myweb.git    #推送本地代碼到gitlab
[root@client myweb]# git push -u origin --all
Username for 'http://192.168.1.20': zx
Password for 'http://zx@192.168.1.20':  zhaoxuan123  #手動輸入密碼
Counting objects: 20, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (20/20), 1.90 KiB | 0 bytes/s, done.
Total 20 (delta 4), reused 0 (delta 0)
To http://192.168.1.20/devops/myweb.git
 * [new branch]      master -> master
 * [new branch]      new_branch_name -> new_branch_name
分支 master 設置為跟蹤來自 origin 的遠程分支 master。
分支 new_branch_name 設置為跟蹤來自 origin 的遠程分支 new_branch_name。

刷新頁面,看是否上傳成功

打標簽上傳

[root@client myweb]# git push -u origin --tags
Username for 'http://192.168.1.20': zx
Password for 'http://zx@192.168.1.20':  zhaoxuan123   手動輸入密碼
Total 0 (delta 0), reused 0 (delta 0)
To http://192.168.1.20/devops/myweb.git
 * [new tag]         1.0 -> 1.0
 * [new tag]         2.0 -> 2.0
 刷新gitlab頁面,看看是否有標簽

如果add時,有輸入錯誤,可以把它刪掉

[root@client myweb]# git remote remove prigin

 使用ssh實現免密登錄

 1.點擊頁面右上角用戶->設置

 2.把你的ssh秘鑰生成后放在秘鑰框里

ssh-keygen -t rsa -C "your.550418723@qq.com" -b 4096

[root@client myweb]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDX2vjopcFPloFk9bdjzSdmXY1bPf14bD5T6RBDR/nqZ44H/OcLeb8fhVlZlj0Y9OF4KOer33f/CWU/3CwKDPNw/LEfVxq6TGiot0s1YvLznp3phPPpyTaxPGOjgpb5Hu0aQ14N64g2o/VGtO6ZnEompyJR4n5xdwMcZYi0gFtg5H60atnZuYqHi/+/wBDsFj1voFJb72yrrH3T06jKphIBCLDj0Kq4F8rZwK74GSmt87M2cjynita628ci07NuXzhLukVEhz7oNspy1p4H9gnEMk65sFzNEtAqd+O05BKm9vZxVYW7kXmPCxtgR1JlL3S1xyZIoiaKHBIbHbyMMSYfuTHiUtvRkdeSfNUbTSvheIL8pIydEZhQudmNL+GnMWYXEXQa9VGbeLA4WxxsPsO4fN2dNwIchgUQSABaoxnjFfG5p5lTx5mwrVUfNvpGeKn4Ra/zSXOkT+rWBQq3L9lZegOOPePvQ941LgebQBhEgaX5bXVjOtJ+u9yiJTwIh+VTjZbj/Vq9dMR0B/r+DV9eAw8zmHQ5+QuGoOtXCW+9wgWLgPZAZtmOuLV7ZPt2hW5CUidt2Qw7jpf+Ynx7cFS72CyFhzwY5gGAa7oAZyc6R5VFD9TpEOtzQDdmqc9P1W+AfxcDUbjvpcGppzWQcv/vhswt+fEIQkAfY8b9SUJypQ== your.550418723@qq.com

 

 
刪除http上傳代碼的方式,並改為ssh上傳代碼的方式

[root@client myweb]# git remote remove origin
[root@client myweb]# git remote add origin git@gitlab:devops/myweb.git

上傳代碼

[root@client myweb]# echo "i love you" > aaa.html
[root@client myweb]# git add .
[root@client myweb]# git commit -m aaa.html
[master 635a42d] aaa.html
 1 file changed, 1 insertion(+)
 create mode 100644 aaa.html
[root@client myweb]# git push     #上傳到gitlab

通過http下載代碼

[root@client myweb]# cd /tmp/
[root@client tmp]# git clone http://192.168.1.20/devops/myweb.git
正克隆到 'myweb'...
remote: Counting objects: 29, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 29 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (29/29), done.
[root@client tmp]# cd myweb/
[root@client myweb]# ls
1111.xtt
同步代碼

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM