将本地分支推送到远程的新项目

时间:2014-05-18 16:52:10

标签: git github git-branch

这可能有点不典型。我正在现有的个人项目的本地分支上工作,但我还没有把它推到远程(GitHub)。

随着我的本地分支的发展,我现在决定基于这个分支创建一个 new 项目,我想将这个分支中的所有代码推送到新项目的主人。

最后,我不想修改现有/旧项目的遥控器上的任何内容。我应该如何使用git?

2 个答案:

答案 0 :(得分:3)

如果您在github上创建了一个新项目,请获取repo的url并执行:

git push <new_project_url> <local_branch>:master

答案 1 :(得分:2)

添加新遥控器并按下

假设存在新项目,则从现有工作副本为新项目添加新远程:

-> git remote add new-remote git@github.com/me/new-project.git

然后推动它:

-> git checkout new-branch # If not already on "the new" branch
-> git push
fatal: The current branch new has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin new-branch
-> git push --set-upstream new-remote master

请注意,当您尝试按下不跟踪任何内容的分支时给出的错误消息会提供设置跟踪分支所需的命令。

这将修改.git/config文件,如下所示:

[remote "new-remote"]
    url = git@github.com/me/new-project.git
    fetch = +refs/heads/*:refs/remotes/new-remote/*
[branch "new-branch"]
    remote = new-remote
    merge = refs/heads/master

将来可以选择从该分支推送/拉出。