2009-01-23 31 views
5

我有一個遠程git倉庫和本地克隆。比方說,我失去了我的本地.git目錄,然後添加和刪除一些文件到本地工作目錄。使用git時,如何將精確的工作目錄推送到遠程?

在某些時候,我想重新初始化本地回購,將它連接到遠程,並最終將我的本地工作目錄推到遠程,就像它一樣(也就是說,我想擁有所有的添加/刪除的文件在遙控器中是相同的)

我該怎麼做到這一點?

這是我目前的解決方案,我不喜歡(並且可能不適用於所有情況)。

GIT中的init

git的遠程添加原點[SOME_URL]

GIT中添加。 #將所有的文件在工作目錄

git的承諾-m 「添加文件」

(在這一點上,我現在的想法是:

  • 做一個分支,

  • 遠程取入它,

  • 'git的差異主分支> my_patch'

  • 應用該補丁應用到分支,

  • 從分支到遠程推,

  • 拉入主,

  • 和殺滅分支。)

顯然我的想法非常複雜和難看。有任何想法嗎?

回答

9

這樣的事情(假設你是從迷失的.git時刻開始的)?

# Make the current directory a git repository. 
# This puts you on a master branch with no commits. 
git init 

# Add a reference to the remote repository. 
git remote add origin urlurlurl 

# Fetch the remote refs. 
git fetch 

# Without touching the working tree, move master branch 
# from nowhere onto the remote master. 
git reset origin/master 

# Stage all changes between the new index and the current tree. 
git add -A 

# Make a commit of these changes. 
git commit -m "New working tree as a patch on remote master" 
0

對不起,如果我誤解了你,但我想你只是想做標準的拉和推? Have a read of this。 Git會爲你處理任何合併,如果有衝突,你可以輕鬆地將它們整理出來。

+0

不,不幸的是我不認爲標準的推拉會適用於我。假設文件'foo.txt'存在於遠程倉庫中,但不存在於我的本地目錄中(請記住,它不在源代碼控制之下)。如果我做'git init',添加遠程,並拉 ​​- 它會創建foo.txt! – 2009-01-23 04:58:30

+0

對不起,我一定誤會了你。是的,它會創建foo.txt。 Git存儲庫都是彼此的副本。 – dylanfm 2009-01-23 05:06:10

3

我不認爲有必要在你的情況下創建一個新的分支。下面是我的回答: 執行以下操作在當前的局部破損回購:

git init 
git add . 
git commit -m 'add files' #create the local master branch 
git remote add myproj [some_url] 
git fetch myproj 
git branch -r #check the remote branches 
git diff master..myproj/master #view the diffs between your local and remote repos 
#now you are sure there is no conflict, 
#so you merge the remote to your local master branch 
git pull myproj master 
git push myproj #push your local master branch to remote 
#now your upstream and downstream is sync'ed 
1

這是我目前的解決方案(假設我已經在包含當前文件的目錄,但沒有得到一個git回購)

git clone -n [git_url] tmp_git_dir 
mv tmp_git_dir/.git . 
rm -rf tmp_git_dir 
git add . 
git commit -a -m "commit message" 

這看起來更簡單,並會根據需要自動添加和刪除文件。我認爲它也更有效率。

我不知道 - 有沒有辦法將git repo克隆到當前目錄(基本上我只想在當前目錄中放置.git目錄,所以我不必做mv和rm)?