測試環境 Ubuntu 22.04 Desktop ( 虛擬機 )
Git 的主要幾個指令可以參考 https://zh.m.wikibooks.org/zh/File:Git_data_flow_simplified.svg 的說明.
安裝 git .
root@ben-virtual-machine:~# apt install git root@ben-virtual-machine:~# git --version git version 2.34.1 |
從 Linux Local 端建立新 Project
初始化(只需要做一次)
- git config
設定使用者名稱與 Mail .root@ben-virtual-machine:~# git config --
global
user.name
"Ben"
root@ben-virtual-machine:~# git config --
global
user.email
"ben@gmail.com"
- git init
設定哪一個目錄是需要透過 git 來保管.root@ben-virtual-machine:~#
mkdir
test_code
root@ben-virtual-machine:~# cd test_code
並做初始化設定,會產生 .git 目錄來記錄相關設定.
root@ben-virtual-machine:~/test_code# git init
hint: Using
'master'
as
the name
for
the initial branch. This
default
branch name
hint: is subject to change. To configure the initial branch name to
use
in all
hint: of your
new
repositories, which will suppress this warning, call:
hint:
hint: git config --
global
init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of
'master'
are
'main'
,
'trunk'
and
hint:
'development'
. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized
empty
Git repository in /root/test_code/.git/
- git remote
設定上傳到 GitLab 的 Project 名稱(同一個目錄的 Project 只須設定一次 git remote 即可),須注意這邊我是放到 ben 使用者下面,請自行更換存在的使用者,之後可以透過 git remote show origin 檢視其設定.root@ben-virtual-machine:~/test_code# git remote add origin http:
//192.168.31.136/ben/test.git
Note : 如果設定錯誤可以透過 remove 參數移除.
root@ben-virtual-machine:~/test_code# git remote remove origin
有新增或是修改資料都需要 add , commit 與 push
- git add 與 commit (資料暫存在 Local 端)
建立一個程式碼.root@ben-virtual-machine:~/test_code# vi test.cpp
#
include
<fstream>
#
include
<iostream>
#
include
<chrono>
#
include
<ctime>
#
include
<unistd.h>
using
namespace
std;
int main() {
while
(true)
{
ofstream myFile_Handler;
// File Open
myFile_Handler.open(
"/tmp/1.txt"
, std::ios_base::app);
auto timenow = chrono::system_clock::to_time_t(chrono::system_clock::now());
// Write to the file
myFile_Handler << ctime(&timenow) << endl;
// File Close
myFile_Handler.close();
// Sleep
sleep(60);
}
}
並透過 git add 與 commit 將程式交給 git 保管. git add 的 “.” 代表現有目錄所有檔案,也可以直接指定檔案名稱.
root@ben-virtual-machine:~/test_code# git add .
root@ben-virtual-machine:~/test_code# git commit -m
"First Code"
[master (root-commit) 938dfba] First Code
1 file changed, 27 insertions(+)
create mode 100644 test.cpp
檢視其狀態 (須注意這邊程式交給 Local 端而已).
root@ben-virtual-machine:~/test_code# git status
On branch master
nothing to commit, working tree clean
- push ( 資料上傳至 GitLab 端)
上傳資料至 GitLab 端root@ben-virtual-machine:~/test_code# git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 466 bytes | 466.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote:
remote: The
private
project ben/test was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin http:
//192.168.31.136/ben/test.git
remote:
remote: To view the project, visit:
remote: http:
//192.168.31.136/ben/test
remote:
remote:
remote:
To http:
//192.168.31.136/ben/test.git
* [
new
branch] master -> master
Branch
'master'
set up to track remote branch
'master'
from
'origin'
.
root@ben-virtual-machine:~/test_code# git remote show origin
(gnome-ssh-askpass:12758): Gtk-WARNING **: 22:09:27.174: cannot open display:
error: unable to read askpass response from
'/usr/libexec/openssh/gnome-ssh-askpass'
(gnome-ssh-askpass:12761): Gtk-WARNING **: 22:09:43.137: cannot open display:
error: unable to read askpass response from
'/usr/libexec/openssh/gnome-ssh-askpass'
* remote origin
Fetch URL: http:
//192.168.31.136/ben/test.git
Push URL: http:
//192.168.31.136/ben/test.git
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@ben-virtual-machine:~/test_code# git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 248 bytes | 248.00 KiB/s, done.
From http:
//192.168.31.136/ben/test
938dfba..d76b680 master -> origin/master
Updating 938dfba..d76b680
Fast-forward
test.cpp | 1 +
1 file changed, 1 insertion(+)
帳號密碼問題
從前面的範例來看我們需要不斷地輸入使用者帳號與密碼,有辦法可以省略掉這一步驟嗎?
我們可以設定 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 AAAAB3NzaC1yc2EAAAADAQABAAABgQDKcNJjUm3c2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxP6VvRTmQLik8yLEH21LnEUfegJIka9qiJJ+vrh4hkT1zNIb1s7Ax7YaRGPA2BhNtXo9YIUAcvjaX5pZGMwQZt8DRGjvYV6/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 |