close

安裝git server

  • 安裝相關套件
$ sudo apt-get update
$ sudo apt-get install git-core -y
# 如果不需要gitflowr架構可以不需要安裝
$ sudo apt-get install git-flow -y
# 安裝完成後查看git版本
$ sudo git --version
  • 設定git預設帳戶
$ sudo git config --global user.email "xxx@xxx.xxx"
$ sudo git config --global user.user "xxxxx"
# 查看設定的指令
$ sudo git config --list

 

設定SSH 建立專案

Add youself to SSH Authentication:

Add yourself to the ssh auth agent, if you haven't already.

把密鑰加入本地端

ssh-add path/to/your/EC2.pem

Set up destination directory:

SSH into the remote directory, and create a barebones remote repo directory.

ssh ec2Username@long-crazy-amazon-ip.com 
mkdir repo-name.git && cd repo-name.git 
git init --bare

Set up your local to push to our new remote:

cd repo-name 
git init git add . 
git commit -m "Initial git commit message" 
git remote add origin ec2Username@long-crazy-amazon-ip.com:/path/to/your/repo-name.git 
git config --global remote.origin.receivepack "git receive-pack" # needed for aws ec2 stuff.
git push origin master

Cloning repository

The origin url can be used for cloning too.

git clone ec2Username@long-crazy-amazon-ip.com:/path/to/your/repo-name.git 

 

部署同步Pull遠端目錄

git checkout -f 的用意其實跟 git checkout HEAD . 的作用一樣,強制將最後一次做的修改(Commit)給 Pull 下來更新,如果遇到本地端的更新,則會將現有修改全部恢復成上次的修改(Commit),然後將檔案全部抓下來,既有(非 git 系統內的檔案,如系統產生的 log)不會受到影響。

$ cd ~
$ mkdir git
$ cd git/
$ mkdir repo-name.git 
$ cd repo-name.git/
$ git init --bare
$ git --bare update-server-info
$ git config core.worktree /var/wwww/html/repo-name.git
$ git config core.bare false
$ git config receive.denycurrentbranch ignore
$ echo "git checkout -f" >> hooks/post-receive
$ chmod +x hooks/post-receive
$ chmod g+rwx -R .

然後我們從本地端來 Clone 這個 Git!

git clone hinablue@192.168.2.100:/home/hinablue/git/staging.git  
$ cd staging/
$ git add .
$ git commit -m "commit 當然要用中文"
$ git push origin master

伺服器端的 core.worktree 就是我們 Commit 上去之後,利用 post-receive 來執行 git checkout -f 的指令。所以,我們在本地端 Push 資料上去的時候,他就會自動 Pull 一份到我們設定的 worktree 裡面。如果你有開 branch 的話,再你還沒有合併到 master 之前,你在 branch 的 Push 並不會影響到遠端上面的資料的。

arrow
arrow
    全站熱搜

    suker0409 發表在 痞客邦 留言(0) 人氣()