Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.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

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
Git 将本地主分支合并到远程分支_Git_Github_Gitlab - Fatal编程技术网

Git 将本地主分支合并到远程分支

Git 将本地主分支合并到远程分支,git,github,gitlab,Git,Github,Gitlab,我从master branch克隆了一个存储库,并做了大量修改。突然,我想起我正在使用master brach,我想将它提交给新创建的远程分支,而不是master分支。实际上,我对git非常陌生。请帮助我,谢谢这是一个常见的工作流错误。一个简单的选择是从master中的当前点创建一个新分支,然后将本地master分支恢复到开始工作之前的点: # from master # git commit any outstanding changes git branch feature git rese

我从master branch克隆了一个存储库,并做了大量修改。突然,我想起我正在使用master brach,我想将它提交给新创建的远程分支,而不是master分支。实际上,我对git非常陌生。请帮助我,谢谢

这是一个常见的工作流错误。一个简单的选择是从
master
中的当前点创建一个新分支,然后将本地
master
分支恢复到开始工作之前的点:

# from master
# git commit any outstanding changes
git branch feature
git reset --hard HEAD~2   # replace 2 with the actual number of commits you did make

这假设您在意识到自己在错误的分支上之前向本地
主分支提交了两次。硬重置命令只是删除这些提交,但现在仍然是
功能的一部分。如果您想这样做,现在可以将
功能
推到远程。

如果您对错误的分支进行了更改,请执行
git checkout-b newBranchName
。 这将把所有更改移动到名为
newBranchName

然后执行
git branch
,查看您当前正在处理哪个分支

如果这是
newBranchName
,则执行以下操作

git add . //stages all changed files
git commit -m "any message here" //commit with a message
git push -u origin newBranchName //push local branch to remote with name newBranchName 
这样做会将本地分支推送到名为
newBranchName

现在执行
git checkout master
将再次将您带回本地master分支

通过
git branch

如果显示绿色母版或
*母版
,则执行此操作
git reset——硬头
恢复您在本地计算机中所做的更改


重置后,本地主分支将与远程主分支完全相同

更快:
git签出-b功能;git分支-f主原点/主
。没有工作树搅动。@jthill是一个很好的技巧,但这可能假设OP没有在两者之间进行提取,或者,如果已经进行了提取,则可以将
master
还原到其他起点。@R.Wedisa欢迎使用堆栈溢出。如果这个答案解决了你的问题,那么请考虑接受它,点击左边的绿色复选标记。谢谢你的时间。我会核实的。有人否决了我的答案。如果你觉得这有帮助,请投票表决,它解决了你的问题,然后接受这个解决方案。实际上不是。不管怎样,我都用艰难的方式解决了。我创建了一个新分支,并对我所做的所有更改进行了硬编码。:)