我刚刚对错误的分支做了很好的提交。 如何撤消我的主分支中的最后一次提交,然后进行相同的更改并将它们放入我的升级分支?


#1楼

这个话题迟了4年,但这可能对某人有所帮助。

如果您在提交之前忘记创建新分支并在master上提交全部,则无论您执行了多少次提交,以下方法都会更容易:

git stash                       # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop                   # skip if all changes were committed

现在,您的主分支等于origin/master ,所有新提交都在my_feature 。 请注意, my_feature是本地分支,而不是远程分支。


#2楼

如果您有干净(未修改)的工作副本

要回滚一个提交(确保在下一步中记下提交的哈希):

git reset --hard HEAD^

将该提交拉入另一个分支:

git checkout other-branch
git cherry-pick COMMIT-HASH

如果您已修改或未跟踪更改

另请注意, git reset --hard杀死您可能拥有的任何未经跟踪和修改的更改 ,因此如果您有这些更改 ,您可能更喜欢:

git reset HEAD^
git checkout .

#3楼

如果尚未推送更改,还可以执行软重置:

git reset --soft HEAD^

这将恢复提交,但将提交的更改放回到索引中。 假设分支相对于彼此是最新的,git将允许你进入另一个分支,然后你可以简单地提交:

git checkout branch
git commit

缺点是您需要重新输入提交消息。


#4楼

如果您已经推动了更改,则需要在重置HEAD后强行进行下一次推送。

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

警告:硬重置将撤消工作副本中任何未提交的修改,而强制重置将完全覆盖具有本地分支当前状态的远程分支的状态。

以防万一,在Windows上(使用Windows命令行,而不是Bash)它实际上是四个^^^^而不是一个,所以它是

git reset --hard HEAD^^^^

#5楼

因此,如果您的方案是您已经承诺master但意味着提交another-branch (可能或不存在可能尚未存在),但您尚未推送,这很容易修复。

// if your branch doesn't exist, then add the -b argument
git checkout -b another-branch
git branch --force master origin/master

现在你要master所有提交都将在another-branch

来自爱的来自: http : //haacked.com/archive/2015/06/29/git-migrate/


#6楼

如果您想要应用更改的分支已存在(例如,分支开发 ),请按照下面的fotanus提供的说明进行操作,然后:

git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature

显然,如果你愿意,你可以使用tempbranch或任何其他分支名称而不是my_feature

此外,如果适用,请延迟存储弹出(应用),直到您在目标分支合并为止。


#7楼

如果遇到此问题但您有Visual Studio,则可以执行以下操作:

右键单击您的分支,然后选择View History

右键单击要返回的提交。 并根据需要恢复或重置。


#8楼

我最近做了同样的事情,我不小心改变了主人,当我应该致力于其他分支。 但我没有推动任何东西。

如果您刚刚提交错误的分支,并且之后没有更改任何内容,并且没有推送到repo,那么您可以执行以下操作:

// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1 // temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash// create other-branch (if the other branch doesn't already exist)
git branch other-branch// checkout the other branch you should have committed to.
git checkout other-branch// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop// add the changes you want with git add...// re-commit your changes onto other-branch
git commit -m "some message..."

注意:在上面的例子中,我用git reset HEAD~1重写1次提交。 但是如果你想倒回n次提交,那么你可以做git reset HEAD~n。

此外,如果您最终提交到错误的分支,并且在意识到您已经提交到错误的分支之前最终编写了更多代码,那么您可以使用git stash来保存正在进行的工作:

// save the not-ready-to-commit work you're in the middle of
git stash // rewind n commits
git reset HEAD~n // stash the committed changes as a single temp commit onto the stack.
git stash // create other-branch (if it doesn't already exist)
git branch other-branch// checkout the other branch you should have committed to.
git checkout other-branch// apply all the committed changes to the new branch
git stash pop// add the changes you want with git add...// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."// pop the changes you were in the middle of and continue coding
git stash pop

注意:我使用本网站作为参考https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/


#9楼

要详细说明这个答案,如果你有多个提交要转移,例如developnew_branch

git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started

如何修复提交错误的Git分支?相关推荐

  1. git 换行问题_后端必备的 Git 分支开发规范指南

    作者:稻草叔叔来源:https://juejin.im/post/5b4328bbf265da0fa21a6820 Git 是目前最流行的源代码管理工具.为规范开发,保持代码提交记录以及 git 分支 ...

  2. 后端必备 Git 分支开发:规范指南

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! https://www.cnblogs.com/herol ...

  3. 合并分支到master_我敢打赌!这是全网最全的 Git 分支开发规范手册

    来源:https://juejin.im/post/684490... Git 是目前最流行的源代码管理工具.为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维护,现规范 git 的相 ...

  4. 程序员必备 Git 分支开发规范指南

    Git 是目前最流行的源代码管理工具.为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维护,现规范 git 的相关操作. 分支管理 分支命名 master 分支 master 为主分支 ...

  5. 送你一份后端必备的 Git 分支开发规范指南

    作者:稻草叔叔 juejin.im/post/5b4328bbf265da0fa21a6820 Git 是目前最流行的源代码管理工具.为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维 ...

  6. Git 分支开发规范

    您必须知道的 Git 分支开发规范 Git 是目前最流行的源代码管理工具. 为规范开发,保持代码提交记录以及 git 分支结构清晰,方便后续维护,现规范 git 的相关操作. 分支管理 分支命名 ma ...

  7. git revert 后再次merge_git如何回滚错误合并的分支

    导读: 分类:技术干货 题目:git如何回滚错误合并的分支 合并到线上分支出现问题的修复方式. 场景 线上分支:master 你开发的分支:dev1 同时开发的分支:dev2 dev1分支开发的代码已 ...

  8. idea查看svn前分支提交_SVN与Git的区别,读完之后,大部分程序员都收藏了...

    SVN(Subversion)是集中式管理的版本控制器,而Git是分布式管理的版本控制器!这是两者之间最核心的区别. 1) 最核心的区别Git是分布式的,而Svn不是分布的.能理解这点,上手会很容易, ...

  9. 【代码管理】GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流

    GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details ...

最新文章

  1. 报名 | 赢取20万美金!Call For Code编程马拉松北京站来袭!
  2. 调用settings中的常数
  3. 创新实训团队记录:为BR-MTC问题设计一个近似算法
  4. EduCoder Linux文件/目录高级管理二
  5. 计算机组成原理 -- 概念点整理
  6. 摄影技巧的种类之一             ——街拍技巧
  7. 让步的人,最值得深交
  8. linux shell切割脚本,自动分割日志bash shell脚本
  9. 2020统计局的行政划分表_湖州市有几个区和县?湖州市2020年县级以上区划名单...
  10. 发布与安装Github Packages
  11. 项目管理表格模板/实用表格-项目启动
  12. linux检测扩容卡,怎么检测SD卡是否被扩容过?对SD卡进行扩容检测的方法
  13. [CF1463F]Max Correct Set
  14. 20220315_K近邻
  15. (二)WebService之调用soap服务
  16. HTTP Live Streaming 分析
  17. web项目经理手册-【7】项目经理需要铭记在心的话
  18. 你真的了解AsyncTask吗?AsyncTask源码分析
  19. 神经网络 注意力机制 Q K V 理解
  20. 【Benewake(北醒)】 单点系列标品介绍

热门文章

  1. 如何在电脑上搭建一个私服,实现maven项目的上传和下载?
  2. 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
  3. 中国篮球--路在何方!
  4. DPM2010保护Sharepoint
  5. IE与FireFox的不同点(不断更新中..)
  6. 算法一看就懂之「 数组与链表 」
  7. MFCC/Filter Bank的提取流程
  8. 赢利定位是网站建设前提
  9. 黑客如何让脚本定时执行
  10. Elasticsearch地理位置