美文网首页
Git - GitLab創建項目及獲取(一)

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

作者: 鬼王丶 | 来源:发表于2017-09-01 15:48 被阅读0次

一. 項目創建及獲取

1.GitLab創建項目

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,進入設置頁面,把hanzo.pub中的內容複製進去,點“Add key”:
    Screenshot from 2017-09-01 10-58-15.png

2.獲取項目

在GItLab創建完成之後,因爲項目是空的,所以會顯示相關獲取操作命令。

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

該設置存在於~/.gitconfig

  • 創建一個新倉庫
#第一行命令執行完之後,直接在你bash的當前目錄生成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 #選擇性的發佈你的master分支
  • 文件夾存在
cd existing_folder
git init #將當前目錄轉化爲Git版本庫
git remote add origin git@gitlab.hanzo.net:hanzo/test-git.git
git add . #將當前文件夾下的內容添加至索引中
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 #發佈本地所有分支至遠程(僅一次)
git push -u origin --tags #發佈本地所有標籤至遠程(僅一次)

git remote add origin 命令爲添加一個名爲origin的新遠程版本庫到倉庫中新建的父版本庫,可以用git remote show origin提取關於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 #版本庫地址
    #源引用:目標引用,*匹配分支名,
    #+表示不會在傳輸過程中進行正常的快進安全檢查
    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
#發佈至遠程
[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

相关文章

网友评论

      本文标题:Git - GitLab創建項目及獲取(一)

      本文链接:https://www.haomeiwen.com/subject/cjmhjxtx.html