Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将本地分支推送到github的一个分支?(本地分支不是克隆的,而是创建的)_Git_Github - Fatal编程技术网

如何将本地分支推送到github的一个分支?(本地分支不是克隆的,而是创建的)

如何将本地分支推送到github的一个分支?(本地分支不是克隆的,而是创建的),git,github,Git,Github,我在本地机器中有两个分支,即master和firstbranch。我在github存储库中有两个分支,即master和firstbranch_1。现在我想从我的localbranch(firstbranch)推送到branch firstbranch_1的远程存储库。我应该怎么做?注意:我没有从github克隆任何东西。我首先在本地机器上创建了它,并将其推送到repo中,然后在github中创建了一个分支。只需推即可。如果分支不相关,那么您可能必须强制执行它 git push origin fi

我在本地机器中有两个分支,即master和firstbranch。我在github存储库中有两个分支,即master和firstbranch_1。现在我想从我的localbranch(firstbranch)推送到branch firstbranch_1的远程存储库。我应该怎么做?注意:我没有从github克隆任何东西。我首先在本地机器上创建了它,并将其推送到repo中,然后在github中创建了一个分支。

只需推即可。如果分支不相关,那么您可能必须强制执行它

git push origin firstbranch:firstbranch_1
(假设远程调用origin)

将本地分支推送到GitHub 需要使用
--set upstream
标志“连接”本地和远程分支,因此
origin/firstbranch
将显示在您的Git日志中,
Git status
将显示

Your branch is ahead/behind 'origin/firstbranch' by XY commits
如果本地分支和远程分支不同,则显示消息(因此需要
推送
/
拉送

从GitHub“克隆”(获取)分支 这将“克隆”分支,并创建一个本地
firstbranch\u 1
,连接到远程分支


刚刚意识到,我误解了你的问题。要连接本地
firstbranch
和远程
firstbranch\u 1
,请使用

$ git push origin firstbranch:firstbranch_1
但如果两个分支不同,此命令将失败:

To https://github.com/whatever/repo.git
! [rejected]        firstbranch -> firstbranch_1 (non-fast-forward)
error: failed to push some refs to 'https://github.com/whatever/repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
如果你想

将远程分支替换为本地分支 这将强制推送,远程
firstbranch_1
将替换为本地
firstbranch
的提交

将本地分支替换为远程分支 这将删除本地
firstbranch
的更改,并将其尖端移动到远程
firstbranch\u 1

在远程分支上重新设置本地分支的基础 这将保留提交的
firstbranch\u 1
,并在其上应用
firstbranch

可能重复的
$ git push origin firstbranch:firstbranch_1
To https://github.com/whatever/repo.git
! [rejected]        firstbranch -> firstbranch_1 (non-fast-forward)
error: failed to push some refs to 'https://github.com/whatever/repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git push --force-with-lease origin firstbranch:firstbranch_1
$ git checkout firstbranch
$ git reset --hard origin/firstbranch_1
$ git rebase origin/firstbranch_1 firstbranch