2011-08-22 47 views
9

我需要將git推送到遠程,並且該推送需要是當前的工作副本。因爲,推送源是一個Web主機。所以,推送的文件需要被當前使用。我相信沒有人會在主機:)git force push current工作目錄

這個問題是跟進的這2個問題, Is it possible to have a git repo inside another git repowhat difference does --bare switch make when initing a git repo?

編輯編輯文件:裸露的回購協議是不是我想要的,因爲文件需要直接在遠程機器上使用,需要在我放置它們的位置找到。

EDIT2:這是我的理解是裸露的回購不保留文件hieararchy,並保留回購的數據在根目錄

回答

10

這裏是如何在網絡上創建一個git回購一步一步主機將允許直接推送,將其設置爲本地回購的遠程服務器,並推送更改。請注意,您需要ssh訪問您的虛擬主機,並安裝了git。

在您的虛擬主機(以SSH方式登錄):

# create an empty repo on your web host; note its path 
mkdir site 
cd site 
git init 

# configure it to allow pushes to the current checked-out branch 
# without complaining (direct push) 
git config receive.denyCurrentBranch ignore 
與您當地的回購(運行回購這些裏面Git命令)

現在:

# create the reference to the web host remote for pushing 
# /path/to/the/repo is the path to the repo you made above on the web host: 
# it will be an absolute path OR relative to your ssh chroot (depends on the host) 
git remote add deploy ssh://[email protected]_host/path/to/the/repo 

# finally, push your changes! 
git push deploy master 

現在,你的變化是推送到Web主機上的簽出回購。但是,它們不會反映在工作目錄中(也就是說,您的更改不會立即生效)。這是因爲你已經直接推送到活動分支。要完成了,你需要或者

  • 手動執行git checkout -f更新工作目錄
  • 做出git post-receive鉤來自動執行。

要使用post-receive掛鉤進行即時自動部署,請檢查此git site deployment howto。

+0

嗯,我所需要的一切都是recieve.denyCurrentBranch。謝謝:) – yasar

+0

@ yasar11732,請務必閱讀有關保持工作目錄最新的注意事項。沒有它,你推動的變化將反映在Git的歷史記錄中,但不一定是你的網站的檢出分支。 – shelhamer

+0

phew,這對我來說是一項艱鉅的工作,但仍然將其取消。謝謝! – yasar

4

雖然這是相當古老,它是非常灰心,推到一個工作樹。 https://git.wiki.kernel.org/index.php/GitFaq#Unexpected_behavior

更好的方法是創建一個裸露的回購鏡像網站的工作樹。

所有裸回購是,是沒有工作樹或沒有檢出文件的存儲庫。層次結構存在所有考慮事項並可以檢出。換句話說,它只能作爲主機進行更改。

不知道你與你的網站,我會承擔它的工作目錄層次是利用chroot的家 一個標準的網站佈局EG:/ home/user中/ WWW

使用SSH與服務器安裝的git:

創建當前網站的工作樹

cd /home/user/public_html 
git init 
git add . 
git commit -m "Initial Commit" 

從本地系統創建一個純倉庫到遠程推

mkdir /home/user/deploy.git 
cd /home/user/deploy.git 
git init --bare 

鏈接你的工作樹庫和裸露的部署庫

cd /home/user/public_html 
git remote add deploy /home/user/deploy.git 
git remote show deploy 
* remote deploy 
    URL: /home/user/deploy.git 
git push deploy master 

現在成立了一個新的回購您的本地系統上

git clone ssh://[email protected]/home/user/deploy.git 
git branch -a 
*master 
    remotes/origin/HEAD 
    remotes/origin/master 

現在我們設置2鉤立即更改您的網絡遠程回購當你推動它或如果別人你給予訪問推動它。由於混帳配置receive.denyCurrentBranch不理會帶來麻煩,從長遠來看

在遠程服務器上啓用部署更新後的

cd /home/user/deploy.git/hooks 
mv post-update.sample post-update 
vi post-update 

更改您的post-update掛鉤到以下和保存

#!/bin/sh 
echo "Pulling changes into public_html [deploy post-update]" 
cd /home/user/public_html || exit 
unset GIT_DIR 
git pull deploy master 
exec git update-server-info 

現在,我們設置您的Web工作樹,以推動其部署的更改,如果有事情承諾它。

cd /home/user/public_html/.git/hooks 
mv post-commit.sample post-commit 
vi post-commit 

然後改變post-commit鉤子以下

#!/bin/sh 
echo "Pushing changes to deploy [public_html post-commit]" 
git push deploy 

你還有如果你需要檢出您的網絡工作樹的選項。 當您推送本地系統的主服務器時,這將允許您將更改從部署部署到Web的工作樹。 您可以在不影響網絡工作樹的情況下分支,重新綁定,還原等,而不用擔心衝突標記,而只需使用裸露的部署存儲庫。 如果您需要更多控制承諾的內容,則可以使用post-receive而不是更新後的版本。

希望這可以幫助別人尋找和OP一樣的東西。

5

這個問題真的很老,但我找到了比這裏列出的解決方案更好的解決方案(至少對我來說)。

manualgit config,作爲Git的2.3.0:

另一種選擇是「updateInstead」將更新工作目錄(必須是乾淨的),如果推到當前分支。此選項用於同步工作目錄,當通過交互式ssh無法輕鬆訪問一個工作目錄時(例如,實時網站,因此要求工作目錄清潔)。

因此git config receive.denyCurrentBranch updateInstead(在接收存儲庫上)爲我完美工作。它允許我推動自動更新接收儲存庫的工作目錄(只要該推送發生時該工作目錄是乾淨的)。