gitlab上傳公鑰和項目代碼

gitlab上傳項目需要ssh公鑰免交互,本文介紹gitlab設置公鑰和上傳項目的操作。

一、創建祕鑰對

[root@localhost ~]# ssh-keygen -t rsa #一路回車即可
gitlab上傳公鑰和項目代碼
二、上傳祕鑰

[root@localhost ~]# cat ~/.ssh/id_rsa.pub #查看公鑰
gitlab上傳公鑰和項目代碼
三、上傳本地代碼

1、要先設置用戶和郵箱

[root@localhost opt]# git config --global user.name "root"

[root@localhost opt]# git config --global user.email "[email protected]"

[root@localhost opt]# git config --list

user.name=root

[email protected]

2、創建一個本地代碼倉庫

[root@localhost opt]# mkdir push_code

[root@localhost opt]# cd push_code/

3、初始化該倉庫

[root@localhost push_code]# git init

初始化空的 Git 版本庫於 /opt/pull_code/.git/

[root@localhost push_code]# echo "this is test push code" > push.txt

[root@localhost push_code]# ls

pull.txt

4、獲取分支

#這個分支信息,可以在web界面clon裏看到

[root@localhost push_code]# git remote add origin http://192.168.0.8:81/test/hellow.git

5、刷新遠程倉庫分支

[root@localhost push_code]# git fetch origin --prune

#獲取所有遠程分支

#現在處於master分支

[root@localhost push_code]# git branch -a

0a

  • master

    remotes/origin/devops

    remotes/origin/master

6、提交代碼

#提交文件到本地倉庫

[root@localhost push_code]# git add push.txt #添加文件

[root@localhost push_code]# git commit -m "this is test push" #添加說明

[root@localhost push_code]# git push origin master #推送代碼至master分支

#小坑

[root@localhost push_code]# git push origin master

Username for 'http://192.168.0.8:81': root

Password for 'http://[email protected]:81':

To http://192.168.0.8:81/test/hellow.git

! [rejected] master -> master (non-fast-forward)

error: 無法推送一些引用到 'http://192.168.0.8:81/test/hellow.git'

提示:更新被拒絕,因爲您當前分支的最新提交落後於其對應的遠程分支。

提示:再次推送前,先與遠程變更合併(如 'git pull')。詳見

提示:'git push --help' 中的 'Note about fast-forwards' 小節。

#解決:重新拉取遠程代碼倉庫信息,與遠程倉庫代碼信息保持一致

[root@localhost push_code]# git pull http://192.168.0.8:81/test/hellow.git

Username for 'http://192.168.0.8:81': root

Password for 'http://[email protected]:81':

來自 http://192.168.0.8:81/test/hellow

  • branch HEAD -> FETCH_HEAD

已經是最新的!

7、切換分支

[root@localhost push_code]# git checkout devops

分支 devops 設置爲跟蹤來自 origin 的遠程分支 devops。

切換到一個新分支 'devops'

[root@localhost push_code]# git branch -a

  • devops

    master

    remotes/origin/devops

    remotes/origin/master

其他分支上傳代碼方式相同重複步驟6即可

8、拉取指定分支代碼

git clone -b devops http://192.168.0.8:81/test/hellow.git #-b指定拉取分支的代碼

9、創建分支

git branch slave 創建slave分支

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章