如何将目录从一个存储库推送到另一个存储库的主分支?

时间:2015-01-03 18:39:04

标签: git git-push

我有两个存储库。我需要将目录从一个存储库推送到另一个存储库的主分支。

My_Working_Dir
  file1
  file2
  Dir1
    file3
    file4

remote_branch/master
   file3
   file4

我喜欢将Dir1目录从第一个存储库推送到第二个存储库的主分支。关于如何做到这一点的任何想法?

提前多多感谢。

3 个答案:

答案 0 :(得分:0)

您不要推送目录。大多数情况下,您推送提交(“大多数”这个词在这里,因为您通常也可以推送标签,包括带注释的标签)。

这意味着“如何推送此目录”的字面答案是您不能。有几种选择,取决于你想要什么样的结果,最有可能你想要的是:

  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. 推送结果

  5. 如果你想要某种不同的结果,你还可以做其他更复杂的事情,但我会把它留在这里。

答案 1 :(得分:0)

首先,我创建了一个测试项目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) Manual Page文档。

阅读GIT URLS

  

通常,URL包含有关传输协议,远程服务器地址和存储库路径的信息。根据传输协议,可能缺少某些信息。

     

Git支持ssh,git,http和https协议(此外,ftp和ftps可用于获取,rsync可用于获取和推送,但这些效率低且不推荐使用;请勿使用它们)。

     

以下语法可以与它们一起使用:

     
      
  • SSH:// [用户@] host.xz [:端口] /path/to/repo.git/

  •   
  • GIT中://host.xz [:端口] /path/to/repo.git/

  •   
  • HTTP [S]://host.xz [:端口] /path/to/repo.git/

  •   
  • FTP [S]://host.xz [:端口] /path/to/repo.git/

  •   
     

等...

我做了git push -f git@github.com:aruprakshit/test-app_2.git,我从一个repo的发送到另一个分支。但您可以创建一个单独的分支,然后将更改推送到第二个repo的分支。

答案 2 :(得分:0)

有几种方法可以解决这个问题。这取决于你想要的情况和结果。

  • 这两个存储库是否有共享历史记录?
  • 您是否希望合并或覆盖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。像往常一样,有几种方法可以实现这一目标。我和this technique一起去,因为它最简单。

下一步将在两个存储库的历史记录中创建一个连接点。这将保留文件的文件历史记录。没有这种联系,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的历史记录。幸运的是,您可以使用此技术并在以后进行更复杂的修剪。我会让其他人报道。