![](/img/trans.png)
[英]How to Push branch of one repository to a new branch in another repository?
[英]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分支。 关于如何执行此操作的任何想法?
非常感谢。
您不推送目录 。 通常,您会推送提交 (此处使用“大部分”一词是因为您通常也可能会推送标签,包括带注释的标签)。
这意味着对“如何推送此目录”的字面回答是您不能这样做。 但是,有几种选择,具体取决于所需的结果类型,最可能的是:
克隆另一个存储库,并检出其master
分支:
cd $HOME/dir-where-you-work git clone git://host/other-repo
将目录及其所有文件从此存储库复制到另一个克隆中:
cp -r project/MyWorkingDir/Dir1 other-repo
将结果提交到另一个克隆中:
cd other-repo git add Dir1 git commit [write up a good commit message]
推动结果
如果您想要某种不同的结果,还可以执行其他更复杂的操作,但是我将在此保留。
首先,我创建了一个测试项目作为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复制到目标存储库中,然后像往常一样提交即可。 你完成了!
从这里开始,我将假定它们没有共享的历史记录,您希望合并,并且希望保留历史记录。
然后,使源存储库成为目标的远程目录。 进入目标存储库并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.