GitLab – 下載 Project 到 Linux Local 端

Loading

測試環境 CentOS 8 ( 虛擬機 )

我的 Gitlab 上面有一個 test 的 Project , 要如何在 Linux Client 端下載下來.

git config

初始化(只需要做一次) , 透過 git config 設定使用者名稱與 Mail .

[root@localhost ~]# git config --global user.name "Ben"
[root@localhost ~]# git config --global user.email "ben@gmail.com"

Git Clone

如果是要從 GitLab 下載一個 Project 到 Local 端,可以使用 Git Clone.

[root@localhost ~]# git clone http://192.168.31.136/ben/test
Cloning into 'test'...
Username for 'http://192.168.31.136': ben
Password for 'http://ben@192.168.31.136':
warning: redirecting to http://192.168.31.136/ben/test.git/
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 1), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (7/7), done.
Resolving deltas: 100% (1/1), done.
[root@localhost ~]# cd test
[root@localhost test]# ll -a
total 16
drwxr-xr-x 3 root root 4096  六   6 16:20 ./
drwx------ 8 root root 4096  六   6 16:19 ../
drwxr-xr-x 8 root root 4096  六   6 16:20 .git/
-rw-r--r-- 1 root root  503  六   6 16:20 test.cpp

Git add , commit 與 push

Git 的主要幾個指令可以參考 https://zh.m.wikibooks.org/zh/File:Git_data_flow_simplified.svg 的說明.

在 Linux Client 端有新增或是修改資料都需要 add , commit 與 push

  • git add 與 commit (資料暫存在 Local 端)
    建立一個 HTML 程式碼.

    [root@localhost test]# vi main.html
    <head>Ben Test</head>
    

    並透過 git add 與 commit 將程式交給 git 保管. git add 的 “.” 代表現有目錄所有檔案,也可以直接指定檔案名稱.

    [root@localhost test]# git add .
    
    [root@localhost test]# git commit -m "Second Code"
    [master 6414d18] Second Code
     Committer: root <root@localhost.localdomain>
    Your name and email address were configured automatically based
    on your username and hostname. Please check that they are accurate.
    You can suppress this message by setting them explicitly. Run the
    following command and follow the instructions in your editor to edit
    your configuration file:
    
        git config --global --edit
    
    After doing this, you may fix the identity used for this commit with:
    
        git commit --amend --reset-author
    
     1 file changed, 1 insertion(+)
     create mode 100644 main.html
    

    檢視其狀態 (須注意這邊程式交給 Local 端而已).

    [root@localhost test]# git status
    On branch master
    nothing to commit, working tree clean
    
  • push ( 資料上傳至 GitLab 端)
    上傳資料至 GitLab 端

    [root@localhost test]# git push -u origin master
    
    (gnome-ssh-askpass:16196): Gtk-WARNING **: 01:11:41.468: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Username for 'http://192.168.31.136': ben
    
    (gnome-ssh-askpass:16197): Gtk-WARNING **: 01:11:46.897: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Password for 'http://ben@192.168.31.136':
    warning: redirecting to http://192.168.31.136/ben/test.git/
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 4 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To http://192.168.31.136/ben/test
       f205c6b..6414d18  master -> master
    branch 'master' set up to track 'origin/master'.
    

    可以透過網頁看到程式碼的確已上傳.

    或是透過指令看狀態.

    root@localhost test]# git remote show origin
    
    (gnome-ssh-askpass:16250): Gtk-WARNING **: 01:14:18.518: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Username for 'http://192.168.31.136': ben
    
    (gnome-ssh-askpass:16251): Gtk-WARNING **: 01:14:23.682: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Password for 'http://ben@192.168.31.136':
    warning: redirecting to http://192.168.31.136/ben/test.git/
    * remote origin
      Fetch URL: http://192.168.31.136/ben/test
      Push  URL: http://192.168.31.136/ben/test
      HEAD branch: master
      Remote branch:
        master tracked
      Local branch configured for 'git pull':
        master merges with remote master
      Local ref configured for 'git push':
        master pushes to master (up to date)
    
  • git pull
    如果程式碼有在其他地方被修改時可以透過 pull 從 GitLab 下載.

    [root@localhost test]# git pull
    
    (gnome-ssh-askpass:16273): Gtk-WARNING **: 01:15:23.092: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Username for 'http://192.168.31.136': ben
    
    (gnome-ssh-askpass:16276): Gtk-WARNING **: 01:15:27.489: cannot open display:
    error: unable to read askpass response from '/usr/libexec/openssh/gnome-ssh-askpass'
    Password for 'http://ben@192.168.31.136':
    warning: redirecting to http://192.168.31.136/ben/test.git/
    Already up to date.
    
  • 帳號密碼問題

    範例使用 http 來下載需輸入使用者帳號與密碼,我們透過 SSH 的方式來下載避免之後的指令需重複輸入帳密.

    我們可以設定 SSH 的 Key,在 Linux 端使用指令產生 SSH 的 Public 與 Provate Key ,並將 Public Key 存放至 Gitlab 即可以,關於 SSH 公/私金鑰 請參考 – https://benjr.tw/98344

    透過 ssh-keygen 來產生 SSH 公私鑰 ,這邊不使用 passhrase (需要一串密碼來解開私鑰) , -C 後面名稱可以使用 E-Mail 或是姓名皆可.

    [root@localhost ~]# ssh-keygen -t rsa -C ben@gmail.com
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:jbZqlgzXm8VHXyGzUkjmn/huqmxm6XzB4wAcSB6yg2k ben@gmail.com
    The key's randomart image is:
    +---[RSA 3072]----+
    |   ..o.   .o.    |
    |  o +...  o. + . |
    | E o .. .  .. + .|
    |.   .  o o .+.. .|
    |       .S.oo.+ . |
    |    . ...oo+o .  |
    |     + ..++.o.   |
    |      =.== o..   |
    |     o. ==o.o.   |
    +----[SHA256]-----+
    

    SSH 產生的 公/私金鑰 皆儲存在家目錄的 .ssh 目錄裡面.

    [root@localhost ~]# ll .ssh/
    id_rsa       id_rsa.pub   known_hosts
    

    id_rsa.pub 就是公鑰,內容要複製到 GitLab 網頁並儲存起來.

    [root@localhost ~]# cat .ssh/id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7YaRGPA2BhNtXo9YIUAcvjaX5pZGMwQZt8DRGjvYV6/0= ben@gmail.com
    

    我這邊使用 ben 身分登入 GitLab 網頁,並點選 User Settings / SSH Keys .

    把剛剛的 id_rsa.pub 內容貼到網頁的 Key 裡面 ,並點選 Add Key 儲存起來.

    這樣在使用者端就不再需要帳號密碼了.

    先把原先透過 http 下載的方式改成 SSH .

    [root@localhost test_code]# git remote remove origin
    [root@localhost test_code]# git remote add origin git@192.168.31.136:ben/test.git
    

    這樣下次下指令就不需要再輸入帳號與密碼了(第一次須接受 Gitlab 的 Public Key)

    [root@localhost test_code]# git pull
    The authenticity of host '192.168.31.136 (192.168.31.136)' can't be established.
    ECDSA key fingerprint is SHA256:M5PIEiSAGg6j2yGHGck5dNY4C22uL9XHzBHClMJh0lk.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added '192.168.31.136' (ECDSA) to the list of known hosts.
    From 192.168.31.136:ben/test
     * [new branch]      master     -> origin/master
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
        git pull <remote> <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> master
    
    沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料