close

架設git server (Ubuntu Server 20.04)
(Login with root user.)

1. Install git packages
# apt-get update
# apt-get install git-core openssh-client

2. Check has installed and its version
# git version
 
3. Create a new Linux user for managing the Git repositories
# useradd -r -s /bin/bash -U -m -d /home/gitRepository git

4. Switch to the log-in session of git user by running:
# su - git

5. To create the SSH directory and file for holding the authorized ssh key for git user, run the following commands:
# mkdir -p ~/.ssh
# chmod 700 ~/.ssh
# touch ~/.ssh/authorized_keys
# chmod 600 ~/.ssh/authorized_keys

6. Once, the server was successfully set-up, it’s time to create a new git repository:
# git init --bare ~/linuxways.git/
# tree /home/gitRepository/Automation.git/

7. Configuring Git repository
Now, you have to add your local user’s public SSH key to the authorized_keys file of the git user.
If you already have generated an SSH key for your local user, you can skip the following step:
# ssh-keygen -t rsa
# cat .ssh/id_rsa.pub

8. Assuming that you already had an unversioned directory, for example, ~/go.
# mkdir /tmp/go
# cd /tmp/go
# git init .

9. Next, you have to add a git remote to track your local repository on Git server:
# git remote add origin git@git-server-ip-address:~/linuxways.git

10. Verify your Git server was successfully installed and configured:
# cd /tmp/go
# touch README
# git add .
# git commit -m "Add file Readme"
# git status
# git log
# git push origin master
# tree /home/gitRepository/Automation.git/
Now, git Server was all ready to work.


Ref: 
https://linuxways.net/ubuntu/how-to-setup-git-server-on-ubuntu-20-04/
https://www.bruceli.net/tw/2011/02/04/install-git-server-on-ubuntu-linux.html
https://backlog.com/git-tutorial/tw/reference/config.html


--------------------------------------------------------------------------------
Others: 
把現有的git檔案,Commnit交給遠端(git Server的repository)進行管理tracking
cd /path/to/your/projects/new_project
加到git remote
# git remote add origin git@git-server-ip-address:~/linuxways.git
# cd /tmp/go
# touch README
# git add .
# git commit -m "Add file Readme"
# git status
# git log
# git push origin master


初始化git 設定用戶名稱/電子郵件
# git config --global user.name git
# git config --global user.email gary@jabil.com
不加上 --global的話,此設定只會對特定的數據庫有效。

設定輸出顏色
# git config --global color.ui auto
Back To Top
顯示設定清單
$ git config --global --list

--------

使用非git帳號的建立方式
Create a folder for git
# mkdir /home/git
# mkdir /home/git/repositories  // 建立git倉庫儲存目錄
# chown git:git /home/git/repositories  // 設定只允許git使用者才能訪問此目錄
# chmod 755 /home/git/repositories  // 設定目錄訪問的可讀寫或執行的許可權

使用git帳號的建立方式
# su - git (切換帳號)
# mkdir /home/gitRepositories/Automation/ // 建立git Repositories底下一個管理的目錄
# mkdir /home/gitRepositories/Automation/new_project.git  // 建立一new_project.git用於git使用倉庫儲存目錄
# git --bare init /home/gitRepositories/Automation/new_project.git  // git初始化

--------

在git server端建立project:開了一個專案叫new_project
# su - git
# cd ~/
# mkdir new_project.git
# cd new_project.git
# git --bare init

在local端,假設我的project資料夾是/path/to/your/projects/new_project,進行git initial(初始化):
cd /path/to/your/projects/new_project
git init

把我們遠端的repository加到git remote:
git remote add origin git@git-server-ip-address:~/linuxways.git

git remote add origin ssh://your_login@your.host/var/git/new_project.git/
這邊路徑要注意,別打成ssh://your_login@your.host:/var/git/new_project.git 
會爆以下錯誤:
ssh: Could not resolve hostname : Name or service not known
fatal: The remote end hung up unexpectedly

把我們local的程式碼丟上去:(每次commit的例行步驟)

git add .
git commit -m '註解'
git push origin master

--------

常用git指令小筆記:
git add 檔案名稱 #加到本次commit
git add -i #加檔案的互動介面
git status # 看準備要commit的檔案
git grep 關鍵字 # 在專案中搜尋關鍵字
git diff # 看本次變動
git log # 看log
git rm 檔案名稱 # 從準備要commit的檔案清單中去除,加-f參數強制刪除
git remote # 會列出來remote列表。
總之就是git remote add new_remote ssh://… 。
新增以後,打這個指令就會出現new_remote。
名稱可以自己取,但如果需求單純的話,一般都習慣取作origin。

--------
更改分支名稱
git branch -M main
git push -uf origin main

arrow
arrow

    吾給力 發表在 痞客邦 留言(0) 人氣()