一. 項(xiàng)目創(chuàng)建及獲取
1.GitLab創(chuàng)建項(xiàng)目
Screenshot from 2017-09-01 10-48-31.png
在你的配置文件中添加SSH密鑰之前王悍,您不能通過ssh來拉動(dòng)或推送項(xiàng)目代碼。所以想通過ssh來pull或push項(xiàng)目代碼题画,需要在配置文件中添加SSH密鑰
- 生成密鑰
[hanzo@hanzo MicroServices]$ cd ~/.ssh/
[hanzo@hanzo .ssh]$ ls
known_hosts
[hanzo@hanzo .ssh]$ ssh-keygen
Generating public/private rsa key pair.
#輸入密鑰ID及密碼
Enter file in which to save the key (/home/hanzo/.ssh/id_rsa): hanzo
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in hanzo.
Your public key has been saved in hanzo.pub.
The key fingerprint is:
41:70:68:4b:31:a2:21:f0:4b:98:ce:f5:6a:7f:bb:ab hanzo@hanzo.net
The key's randomart image is:
+--[ RSA 2048]----+
|+ . . ++o |
| = o .++ |
|o +. o .. |
|o.... . . |
| o. . S |
| . |
| o |
| . . . |
| Eo++ |
+-----------------+
[hanzo@hanzo .ssh]$ ls
hanzo hanzo.pub known_hosts
#查看密鑰
[hanzo@hanzo .ssh]$ cat hanzo.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoMRqkTbTwj6GABWmvnpTeWegUVul9TyBj7l9SBA4F2DFqdYtBglcYEeS6B+aHqBTDnofHRS/FrMvldtpuzSGO9tJwVk74JY7c7zcn2VCitSWrGmTHUrXahtwNmIkDDGf7aId9Frlj8rgc9YOs0HcafBDAR4UylYCRtFUVu9tfdgcMP1RfWU/2rgSNEUqxDvWYh0uzjKVptQJRxOwGARqY52EPIfpd86UXQb1fIZKMqHkkQwtJwMMoQ1g5XMsp+ae39IocX0pG4zoZZ490arcFno0HXU023X/wy+N1hy9Fr20Le6w7fPwPUVjR3DSldlY4Jd7iQwandZvA89b3rJAf hanzo@hanzo.net
[hanzo@hanzo .ssh]$ ssh-add -l
2048 41:70:68:4b:31:a2:21:f0:4b:98:ce:f5:6a:7f:bb:ab hanzo@hanzo.net (RSA)
- 配置密鑰
然後點(diǎn)擊帶有下劃線add an SSH默辨,進(jìn)入設(shè)置頁面,把hanzo.pub中的內(nèi)容複製進(jìn)去苍息,點(diǎn)“Add key”:
Screenshot from 2017-09-01 10-58-15.png
2.獲取項(xiàng)目
在GItLab創(chuàng)建完成之後缩幸,因爲(wèi)項(xiàng)目是空的壹置,所以會(huì)顯示相關(guān)獲取操作命令。
- Git全局設(shè)置
git config --global user.name "hanzo"
git config --global user.email "guixiong97@sina.cn"
該設(shè)置存在於~/.gitconfig
- 創(chuàng)建一個(gè)新倉庫
#第一行命令執(zhí)行完之後表谊,直接在你bash的當(dāng)前目錄生成test-git目錄
git clone git@gitlab.hanzo.net:hanzo/test-git.git
cd test-git
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master #選擇性的發(fā)佈你的master分支
- 文件夾存在
cd existing_folder
git init #將當(dāng)前目錄轉(zhuǎn)化爲(wèi)Git版本庫
git remote add origin git@gitlab.hanzo.net:hanzo/test-git.git
git add . #將當(dāng)前文件夾下的內(nèi)容添加至索引中
git commit -m "Initial commit"
git push -u origin master
- 倉庫存在
cd existing_repo
git remote add origin git@gitlab.hanzo.net:hanzo/test-git.git
git push -u origin --all #發(fā)佈本地所有分支至遠(yuǎn)程(僅一次)
git push -u origin --tags #發(fā)佈本地所有標(biāo)籤至遠(yuǎn)程(僅一次)
git remote add origin 命令爲(wèi)添加一個(gè)名爲(wèi)origin的新遠(yuǎn)程版本庫到倉庫中新建的父版本庫钞护,可以用git remote show origin提取關(guān)於origin遠(yuǎn)程版本庫的所有信息。
查看遠(yuǎn)程倉版本庫的定義:
[hanzo@hanzo test-git]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"] #版本庫
url = git@gitlab.hanzo.net:hanzo/test-git.git #版本庫地址
#源引用:目標(biāo)引用爆办,*匹配分支名难咕,
#+表示不會(huì)在傳輸過程中進(jìn)行正常的快進(jìn)安全檢查
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"] #版本庫分支
remote = origin
merge = refs/heads/master
3.查看分支
#push之前查看
[hanzo@hanzo test-git]$ git show-branch -a
[master] Add README
#發(fā)佈至遠(yuǎn)程
[hanzo@hanzo test-git]$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitlab.hanzo.net:hanzo/test-git.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
#push之後查看
[hanzo@hanzo test-git]$ git show-branch -a
* [master] Add README
! [origin/master] Add README
--
*+ [master] Add README