本文翻译自:Push commits to another branch

Is it possible to commit and push changes from one branch to another. 是否可以将更改从一个分支提交到另一个分支。

Assume I commited changes in BRANCH1 and want to push them to BRANCH2 . 假设我在BRANCH1中提交了更改,并希望将其推送到BRANCH2中

From BRANCH1 , is it valid to do: BRANCH1执行以下操作是否有效:

git push origin **BRANCH2**

And then reset BRANCH1? 然后重置BRANCH1?


#1楼

参考:https://stackoom.com/question/wJQj/推送提交到另一个分支


#2楼

Certainly, though it will only work if it's a fast forward of BRANCH2 or if you force it. 当然,尽管仅当它是BRANCH2的快进或您强制使用时才起作用。 The correct syntax to do such a thing is 做这种事情的正确语法是

git push <remote> <source branch>:<dest branch>

See the description of a "refspec" on the git push man page for more detail on how it works. 有关其工作原理的更多详细信息,请参见git push手册页上“ refspec”的描述。 Also note that both a force push and a reset are operations that "rewrite history", and shouldn't be attempted by the faint of heart unless you're absolutely sure you know what you're doing with respect to any remote repositories and other people who have forks/clones of the same project. 还要注意,强制推送和重置都是“重写历史记录”的操作,不应轻率地尝试,除非您完全确定知道自己对任何远程存储库和其他存储库正在做什么具有同一项目的分叉/克隆的人。


#3楼

That will almost work. 那几乎可以工作。

When pushing to a non-default branch, you need to specify the source ref and the target ref: 当推送到非默认分支时,您需要指定源引用和目标引用:

git push origin branch1:branch2

Or 要么

git push <remote> <branch with new changes>:<branch you are pushing to>

#4楼

In my case I had one local commit, which wasn't pushed to origin\\master , but commited to my local master branch. 在我的情况下,我有一个本地提交,该提交未推送到origin\\master ,而是提交到了本地master分支。 This local commit should be now pushed to another branch. 现在应将此本地提交推送到另一个分支。

With Git Extensions you can do something like this: 使用Git Extensions,您可以执行以下操作:

  • (Create if not existing and) checkout new branch, where you want to push your commit. (如果不存在,则创建,然后)在要推送提交的地方签出新分支。
  • Select the commit from the history, which should get commited & pushed to this branch. 从历史记录中选择提交,它将被提交并推送到该分支。
  • Right click and select Cherry pick commit . 右键单击并选择Cherry pick commit
  • Press Cherry pick button afterwards. 然后按Cherry选择按钮。
  • The selected commit get's applied to your checked out branch. 所选的提交获取将应用于您的已签出分支。 Now commit and push it. 现在提交并推送它。
  • Check out your old branch, with the faulty commit. 检查您的旧分支,并提交错误的内容。
  • Hard reset this branch to the second last commit, where everything was ok (be aware what are you doing here!). 将该分支硬重置为最后一个提交,一切正常(请注意您在这里做什么!)。 You can do that via right click on the second last commit and select Reset current branch to here . 您可以通过右键单击倒数第二个提交,然后选择“将当前分支重置为此处”来执行此操作 Confirm the opperation, if you know what you are doing. 如果您知道自己在做什么,请确认操作。

You could also do that on the GIT command line . 您也可以在GIT命令行上执行此操作。 Example copied from David Christensen : David Christensen复制的示例:

I think you'll find git cherry-pick + git reset to be a much quicker workflow: 我认为您会发现git cherry-pick + git reset是一个更快的工作流程:

Using your same scenario, with "feature" being the branch with the top-most commit being incorrect, it'd be much easier to do this: 使用相同的场景,将“功能”作为最不正确提交的分支,这样做会容易得多:

git checkout master
git cherry-pick feature
git checkout feature
git reset --hard HEAD^

Saves quite a bit of work, and is the scenario that git cherry-pick was designed to handle. 节省了大量工作,这是git cherry-pick旨在处理的方案。

I'll also note that this will work as well if it's not the topmost commit; 我还要注意,如果它不是最高的提交,那么它也将起作用。 you just need a commitish for the argument to cherry-pick, via: 您只需要通过以下方式为Cherry-pick的参数添加一个命令即可:

git checkout master
git cherry-pick $sha1
git checkout feature
git rebase -i ... # whack the specific commit from the history


#5楼

I get a bad result with git push origin branch1:branch2 command: 我用git push origin branch1:branch2命令得到了不好的结果:

In my case, branch2 was deleted and branch1 was updated with the new changes. 在我的情况下, branch2被删除,而branch1用新更改进行了更新。

Thus, if you want the changes only push on the branch2 from the branch1 , try these procedures: 因此,如果你想改变只推就branch2branch1 ,请尝试以下方法:

  • On branch1 : git add . branch1git add .
  • On branch1 : git commit -m 'comments' branch1git commit -m 'comments' branch1 git commit -m 'comments'
  • On branch1 : git push origin branch1 branch1git push origin branch1

  • On branch2 : git pull origin branch1 branch2git pull origin branch1

  • On branch1 : revert to the previous commit. branch1 :恢复为上一个提交。


#6楼

You have committed to BRANCH1 and want to get rid of this commit without losing the changes? 您已承诺使用BRANCH1,并希望在不丢失更改的情况下摆脱此承诺? git reset is what you need. git reset是您需要的。 Do: 做:

git branch BRANCH2

if you want BRANCH2 to be a new branch. 如果您希望BRANCH2成为新分支。 You can also merge this at the end with another branch if you want. 如果需要,还可以在最后将其与另一个分支合并。 If BRANCH2 already exists, then leave this step out. 如果BRANCH2已经存在,则省略此步骤。

Then do: 然后做:

git reset --hard HEAD~3

if you want to reset the commit on the branch you have committed. 如果要在已提交的分支上重置提交。 This takes the changes of the last three commits. 这将更改最后三个提交。

Then do the following to bring the resetted commits to BRANCH2 然后执行以下操作将重置的提交提交到BRANCH2

git checkout BRANCH2

This source was helpful: https://git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch 该来源很有帮助: https : //git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch

推送提交到另一个分支相关推荐

  1. idea提交git差件_多人合作使用git,推送代码、和并分支

    原文地址: 多人合作使用git,推送代码.和并分支 ​www.limuke.top 下面将会演示多人合作写项目使用git的教程:创建分支.推送代码.将自己的分支的代码和主分支合并. 下面是模仿两个人合 ...

  2. idea推送代码时切换git分支

    1.在idea右下角点击git:xxx 2.Remote Branches就是远端分支,也就是要上传的分支,我这里有master,test.比如要切换到test分支,就点击test分支后的三角,在弹出 ...

  3. git创建本地分支以及推送本地分之至远程分支

    Git分支策略 实际开发中,应当按照以下几个基本原则进行管理: 首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能再上边干活. 那在哪干活呢?干活都在dev分支上,也就是说,de ...

  4. Visual Studio无法推送提交到Github的解决方法

    Visual Studio无法推送提交到Github的解决方法 参考文章: (1)Visual Studio无法推送提交到Github的解决方法 (2)https://www.cnblogs.com/ ...

  5. php推送示例mip,WordPress文章页如何自动推送提交MIP/AMP页面 | 小灰灰博客

    如果是安装 AMP 插件实现的 AMP 页面或者 MIP 页面,那么文章的 AMP 地址是在文章原地址后面加后缀 /amp,页面的 AMP 地址是在页面原地址后面加后缀 ?amp ,MIP 的则加相应 ...

  6. 百度网址批量提交 百度网站快速收录批量推送提交工具【批量版】

    百度网址批量提交 百度网站快速收录批量推送提交工具[批量版] 本seo站长工具软件使用的百度站长平台官方提供的API推送接口 百度seo软件 主动推送工具插件的作用 是批量提交网站链接,主动推送给百度 ...

  7. 【全网最全面】SourceTree使用教程详解(连接远程仓库,克隆,拉取,提交,推送,新建/切换/合并分支,冲突解决,提交PR)...

    前言: 俗话说的好工欲善其事必先利其器,Git分布式版本控制系统是我们日常开发中不可或缺的.目前市面上比较流行的Git可视化管理工具有SourceTree.Github Desktop.Tortois ...

  8. 给女盆友微信定时消息推送,给她一个大大的情惊喜!!!

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 目录 前言 一.微信推送 二.使用步骤 1. 注册微信微信公众平台 2.配置脚本参数 3.第三步:fork 仓库, 填入相应配置 1. ...

  9. git本地仓库推送到远程仓库指定分支步骤

    1.初始化项目 git init 2.建立本地仓库和远程仓库的连接 git remote add origin 远程仓库URL 3.将码云上的仓库pull到本地 git pull origin 分支名 ...

最新文章

  1. Java学习路线图,如何学习Java事半功倍?
  2. MySQL导入导出数据和结构
  3. 基于jsp+javabean+servlet的二手物品交易系统_基于Jsp+Servlet的商城系统
  4. 动态规划算法-06Longest Valid Parentheses问题
  5. HDU 4618 - Palindrome Sub-Array(2013MUTC2-1008)(DP)
  6. python中return和and连用
  7. 了解java虚拟机---JVM的基本结构(1)
  8. 超声乳化设备行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  9. CCF NOI1098 森林
  10. 如何在代码里打开Android手机通知状态栏
  11. python语言程序设计 陈东_清华大学出版社-图书详情-《Python语言程序设计》
  12. EnableQ 安装
  13. 【如何在12306网站上购买上中下卧铺火车票呢?】
  14. 和平精英官方网站静态页面制作与学习html+css保姆级教程
  15. 如何系统性地学习分布式系统
  16. win7系统文件夹共享后有锁图标怎么去掉?
  17. 敬业福!2023官方集福攻略
  18. fio 全称是flexible I/O tester(灵活的I/O测试工具)。可以根据用户指定I/O类型进行多线程/进程的I/O负载模拟。
  19. android adb 抓取log
  20. 真爱如血第七季/全集True Blood迅雷下载

热门文章

  1. Struts2 异常处理
  2. 什么叫做坐标系的平移和旋转.
  3. 对quake3源代码的学习与研究初步的计划
  4. OkHttp 源码解析---拦截器
  5. FastJson 打Release 包解析失败
  6. ListView 常用属性
  7. JavaScript语言基础12
  8. RocketMQ NameServer网络通信架构(基于Netty)
  9. Flutter开发之ListView使用第三方flutter_refresh加载更多(37)
  10. 微信小程序eventChannel页面间事件通信通道