Git - GitLab創(chuàng)建項目及獲取(一)

一. 項目創(chuàng)建及獲取

1.GitLab創(chuàng)建項目

Screenshot from 2017-09-01 10-48-31.png

在你的配置文件中添加SSH密鑰之前,您不能通過ssh來拉動或推送項目代碼。所以想通過ssh來pull或push項目代碼,需要在配置文件中添加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)
  • 配置密鑰
    然後點擊帶有下劃線add an SSH,進入設(shè)置頁面,把hanzo.pub中的內(nèi)容複製進去,點“Add key”:
    Screenshot from 2017-09-01 10-58-15.png

2.獲取項目

在GItLab創(chuàng)建完成之後,因爲(wèi)項目是空的,所以會顯示相關(guān)獲取操作命令。

  • Git全局設(shè)置
git config --global user.name "hanzo"
git config --global user.email "guixiong97@sina.cn"

該設(shè)置存在於~/.gitconfig

  • 創(chuàng)建一個新倉庫
#第一行命令執(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ā)佈本地所有分支至遠程(僅一次)
git push -u origin --tags #發(fā)佈本地所有標(biāo)籤至遠程(僅一次)

git remote add origin 命令爲(wèi)添加一個名爲(wèi)origin的新遠程版本庫到倉庫中新建的父版本庫,可以用git remote show origin提取關(guān)於origin遠程版本庫的所有信息。

查看遠程倉版本庫的定義:

[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)引用,*匹配分支名,
    #+表示不會在傳輸過程中進行正常的快進安全檢查
    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ā)佈至遠程
[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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容