簡體   English   中英

如何將目錄從一個存儲庫推送到另一個存儲庫的主分支?

[英]How to push a directory from one repository to the master branch of another repository?

我有兩個存儲庫。 我需要將目錄從一個存儲庫推送到另一個存儲庫的主分支。

My_Working_Dir
  file1
  file2
  Dir1
    file3
    file4

remote_branch/master
   file3
   file4

我喜歡將Dir1目錄從第一個存儲庫推送到第二個存儲庫的master分支。 關於如何執行此操作的任何想法?

非常感謝。

您不推送目錄 通常,您會推送提交 (此處使用“大部分”一詞是因為您通常也可能會推送標簽,包括帶注釋的標簽)。

這意味着對“如何推送此目錄”的字面回答是您不能這樣做。 但是,有幾種選擇,具體取決於所需的結果類型,最可能的是:

  1. 克隆另一個存儲庫,並檢出其master分支:

     cd $HOME/dir-where-you-work git clone git://host/other-repo 
  2. 將目錄及其所有文件從此存儲庫復制到另一個克隆中:

     cp -r project/MyWorkingDir/Dir1 other-repo 
  3. 將結果提交到另一個克隆中:

     cd other-repo git add Dir1 git commit [write up a good commit message] 
  4. 推動結果

如果您想要某種不同的結果,還可以執行其他更復雜的操作,但是我將在此保留。

首先,我創建了一個測試項目作為app/ :-

[arup@~]$ mkdir app/
[arup@~]$ ls | grep app
app
[arup@~]$ cd app
[arup@app]$ touch a.txt
[arup@app]$ cat > a.txt
This is a test file.
^C
[arup@app]$ cat a.txt
This is a test file.
[arup@app]$ git init
Initialized empty Git repository in /home/arup/app/.git/
[arup@app]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       a.txt
nothing added to commit but untracked files present (use "git add" to track)
[arup@app]$ git commit -m "first commit"
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       a.txt
nothing added to commit but untracked files present (use "git add" to track)
[arup@app]$ git add -A
[arup@app]$ git commit -m "first commit"
[master (root-commit) 5392f72] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[arup@app]$ git remote add origin git@github.com:aruprakshit/test-app_1.git
[arup@app]$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:aruprakshit/test-app_1.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

現在,我創建了另一個應用程序/app2 :-

[arup@~]$ mkdir app2
[arup@~]$ cd app2
[arup@app2]$ touch README.md
[arup@app2]$ git init
Initialized empty Git repository in /home/arup/app2/.git/
[arup@app2]$ git add README.md
[arup@app2]$ git commit -m "first commit"
[master (root-commit) d044fe9] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
[arup@app2]$ git remote add origin git@github.com:aruprakshit/test-app_2.git
[arup@app2]$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:aruprakshit/test-app_2.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[arup@app2]$ ls
README.md

現在,您可以在app2/下看到我只有README.md文件。 現在,我將從第一個項目將文件a.txt推送到此存儲庫。 所以我在下面做了:

[arup@app]$ git push -f git@github.com:aruprakshit/test-app_2.git
Counting objects: 3, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:aruprakshit/test-app_2.git
 + d044fe9...5392f72 master -> master (forced update)
[arup@app]$

現在檢查文件是否進入第二個項目:

[arup@app2]$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:aruprakshit/test-app_2
 * branch            master     -> FETCH_HEAD
 + d044fe9...5392f72 master     -> origin/master  (forced update)
Merge made by the 'recursive' strategy.
 a.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[arup@app2]$ ls
a.txt  README.md
[arup@app2]$ cat a.txt
This is a test file.
[arup@app2]$

您可以閱讀git-push(1)手冊頁文檔。

閱讀GIT URL

通常,URL包含有關傳輸協議,遠程服務器的地址以及存儲庫路徑的信息。 根據傳輸協議,某些信息可能不存在。

Git支持ssh,git,http和https協議(此外,ftp和ftps可用於獲取,rsync可用於獲取和推送,但這些效率低下且已棄用;請勿使用它們)。

以下語法可以與它們一起使用:

  • ssh:// [user @] host.xz [:port] /path/to/repo.git/

  • git://host.xz [:port] /path/to/repo.git/

  • http [s]://host.xz [:port] /path/to/repo.git/

  • ftp [s]://host.xz [:port] /path/to/repo.git/

等等...

我做的是git push -f git@github.com:aruprakshit/test-app_2.git ,在這里我從一個存儲庫的主服務器發送到另一個存儲庫的分支。 但是您可以創建一個單獨的分支 ,然后將對特定分支的更改也推送到第二個倉庫的master分支。

有幾種方法可以實現這一目標。 這取決於情況和所需的結果。

  • 這兩個存儲庫是否有共享的歷史記錄?
  • 您希望file3和file4被合並或覆蓋嗎?
  • 您是否要在源存儲庫中保留對file3和file4的更改歷史記錄?
  • 這兩個存儲庫相關嗎,或者僅僅是這兩個文件?

首先,克隆遙控器。 我們將其稱為目標存儲庫。

如果您不想保留歷史記錄,只需將file3和file4復制到目標存儲庫中,然后像往常一樣提交即可。 你完成了!

從這里開始,我將假定它們沒有共享的歷史記錄,您希望合並,並且希望保留歷史記錄。

然后,使源存儲庫成為目標的遠程目錄。 進入目標存儲庫並git remote add source <source url> Git遙控器將采用各種URL,並且還將采用文件路徑。 你可以做git remote add source /path/to/My_Working_Dir

然后使用git fetch source獲取另一個存儲庫。 現在,您的目標系統信息庫具有要使用的源代碼副本。 任何共享的歷史記錄將被共享。

下一個問題是讓git理解源中的Dir1 / file3是目標中的file3。 與往常一樣,有幾種方法可以完成此操作。 我要使用這種技術,因為它最簡單。

下一步將在兩個存儲庫的歷史記錄中創建一個連接點。 這將保留文件的文件歷史記錄。 沒有該連接,git無法完成其工作。 git merge -s ours --no-commit source/master -s是用於進行合並的策略。 ours意思是忽略除“我們的”更改(即目的地)以外的所有內容。 這樣可以防止源中的所有其他文件被合並,因此合並將不包含任何更改。 --no-commit表示執行合並但不提交(合並只是具有多個父項的特殊提交)。 合並不應包含任何更改,但是它將為存儲庫提供一個共同的歷史點。

我們實際上希望從源中獲取Dir1 / file3和Dir1 / file4。 git read-tree -m -u source/master:Dir1將復制它們並將其添加到暫存區。 您將把file3和file4更改為源版本。 Git在合並方面做得不好,因為它們缺乏共同的歷史。 如果您不喜歡git如何合並它們,則可以進行所需的任何修改並git add它們。

像其他任何提交一樣提交合並。 您應該在提交日志中記錄您已移動了Dir1 / *,以及在合並期間執行了哪些其他操作以及原因。

正常推動。 您正在推送源存儲庫的全部歷史記錄,因此可能會很大。

您可以使用git remote rm source刪除遠程。 由於合並,將保留歷史記錄。

如果源存儲庫很大,而其其他內容基本上無關緊要,則還有其他使用git filter-branch將源存儲庫縮減為file3和file4歷史記錄的技術。 幸運的是,您可以使用此技術,以后再進行更復雜的修剪。 我讓別人來掩飾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM