本文翻译自:How to commit my current changes to a different branch in git [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • How do I merge my local uncommitted changes into another Git branch? 如何将本地未提交的更改合并到另一个Git分支? 6 answers 6个答案

Sometimes it happens that I make some changes in my working directory and I realize that these changes should be committed in a branch different to the current one. 有时我会在工作目录中进行一些更改,并且我意识到这些更改应该在与当前更改的分支中提交。 This usually happens when I want to try out new things or do some testing and I forget to create a new branch beforehand, but I don't want to commit dirty code to the master branch. 这通常发生在我想尝试新事物或进行一些测试时,我忘记事先创建一个新分支,但我不想将脏代码提交给主分支。

So, how can I make that uncommitted changes (or changes stored in the index) be committed to a different branch than the current one? 那么, 如何将未提交的更改(或存储在索引中的更改)提交到与当前更改不同的分支?


#1楼

参考:https://stackoom.com/question/CLzR/如何将当前更改提交到git中的不同分支-重复


#2楼

  1. git checkout my_other_branch
  2. git add my_file my_other_file
  3. git commit -m

并提供您的提交消息。


#3楼

You can just create a new branch and switch onto it. 您可以创建一个新分支并切换到它。 Commit your changes then: 然后提交您的更改:

git branch dirty
git checkout dirty
// And your commit follows ...

Alternatively, you can also checkout an existing branch (just git checkout <name> ). 或者,您也可以签出现有分支(只需git checkout <name> )。 But only, if there are no collisions (the base of all edited files is the same as in your current branch). 但是,只有在没有冲突的情况下(所有已编辑文件的基础与当前分支中的相同)。 Otherwise you will get a message. 否则你会收到一条消息。


#4楼

The other answers suggesting checking out the other branch, then committing to it, only work if the checkout is possible given the local modifications. 其他答案建议检查另一个分支,然后承诺,只有在可以通过本地修改结账时才能工作。 If not, you're in the most common use case for git stash : 如果没有,你就是git stash最常见的用例:

git stash
git checkout other-branch
git stash pop

The first stash hides away your changes (basically making a temporary commit), and the subsequent stash pop re-applies them. 第一个stash隐藏了您的更改(基本上是进行临时提交),随后的stash pop重新应用它们。 This lets git use its merge capabilities. 这让git使用它的合并功能。

If when you try to pop the stash, you run into merge conflicts... the next steps depend on what those conflicts are. 如果当您尝试弹出存储时,会遇到合并冲突......接下来的步骤取决于这些冲突是什么。 If all the stashed changes indeed belong on that other branch, you're simply going to have to sort through them - it's a consequence of having made your changes on the wrong branch. 如果所有隐藏的更改确实属于另一个分支,那么您只需要对它们进行排序 - 这是因为您在错误的分支上进行了更改。

On the other hand, if you've really messed up, and your work tree has a mix of changes for the two branches, and the conflicts are just in the ones you want to commit back on the original branch, you can save some work. 另一方面,如果你真的搞砸了,你的工作树有两个分支的混合变化,并且冲突只是你想要在原始分支上提交的那些,你可以节省一些工作。 As usual, there are a lot of ways to do this. 像往常一样,有很多方法可以做到这一点。 Here's one, starting from after you pop and see the conflicts: 这是一个,从弹出并看到冲突后开始:

# Unstage everything (warning: this leaves files with conflicts in your tree)
git reset
# Add the things you *do* want to commit here
git add -p     # or maybe git add -i
git commit
# The stash still exists; pop only throws it away if it applied cleanly
git checkout original-branch
git stash pop
# Add the changes meant for this branch
git add -p
git commit
# And throw away the rest
git reset --hard

Alternatively, if you realize ahead of the time that this is going to happen, simply commit the things that belong on the current branch. 或者,如果您事先意识到这将会发生,只需提交属于当前分支的东西。 You can always come back and amend that commit: 您可以随时返回并修改该提交:

git add -p
git commit
git stash
git checkout other-branch
git stash pop

And of course, remember that this all took a bit of work, and avoid it next time, perhaps by putting your current branch name in your prompt by adding $(__git_ps1) to your PS1 in your bashrc. 当然,请记住,这一切都需要一些工作,并且下次可以避免它,可能是通过在你的bashrc中的PS1中添加$(__git_ps1)来将你当前的分支名称添加到你的提示中。 (See for example the Git in Bash docs.) (例如参见Bash文档中的Git 。)

如何将当前更改提交到git中的不同分支[重复]相关推荐

  1. Git之(三)Git中常用命令——分支管理

    三.Git中常用命令--分支管理 为什么要使用分支管理? 分支就是科幻电影里面的平行宇宙,也就是当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干 ...

  2. 在微信开发者工具中将代码提交到git中

    一:创建git仓库 在git中新建仓库,可以不用初始化仓库. 二:复制地址 创建成功之后会有一个仓库地址,将这个仓库地址复制下来. 三:在微信开发者工具中初始化仓库 git仓库创建完之后,新建一个微信 ...

  3. 从另一个分支在Git中创建一个分支

    我有两个分支: master和dev 我想从dev分支创建一个"功能分支". 目前在分支机构dev上,我执行以下操作: $ git checkout -b myfeature de ...

  4. 乌龟git 分支_关于git中自己的分支和主分支有冲突的解决方案(git和乌龟git)...

    阐述一个案例,最近在开发中遇到一个问题.自己在代码的主分支拉了一个分支,开始快乐的开发修改了.同事小明也在主分支拉了一个分支,也在快乐的修改.小明的开发速度很快,一个问题很快就解决了,并且把自己的代码 ...

  5. Git 中删除本地分支和删除远程分支是两回事儿

    目录 本地操作 1. 创建本地分支 2. 删除本地分支 远端操作 1. 创建远程分支 2. 删除远程分支 本地操作 1. 创建本地分支 在本地仓库中创建本地分支,Git 命令如下: git branc ...

  6. 如何在Git中克隆单个分支?

    我有一个本地的名为" skeleton"的Git存储库,用于存储项目框架. 它有几个分支,分别用于不同类型的项目: casey@agave [~/Projects/skeleton ...

  7. git中查看全部分支的命令

    现在介绍一个超级实用.使用频率极高但几乎所有 Git 教程都不重视的命令 git branch -avv,它用来查看全部分支信息: 上图有三行信息,依次说明: 第一行,开头的星号表示当前所在分支,绿色 ...

  8. 如何在Git中更改多次提交的作者和提交者名称以及电子邮件?

    我当时正在学校计算机上编写一个简单的脚本,然后将更改提交到Git(在笔驱动器中的一个回购中,该回购是从家里的计算机中克隆的). 经过几次提交后,我意识到我正在以root用户身份提交东西. 有什么办法可 ...

  9. 【Git、GitHub、GitLab】七 git中分支的删除以及出现分离头指针的情况

    上一篇文章学习了GIT中commit.tree和blob三个对象之间的关系,点击链接查看:[Git.GitHub.GitLab]六 GIT中commit.tree和blob三个对象之间的关系 文章目录 ...

最新文章

  1. 字符串相关的类String
  2. anaconda 设置python3为主_关于在Windows、Linux和Mac上安装设置Python的问题
  3. @RequestParam和@RequestBody的区别 (结合 Get/Post )
  4. java 本地发送邮件_java在本地发送邮件可以,在服务器发送报错
  5. activeMq初识 - 2
  6. redis——数据结构(字典、链表、字符串)
  7. 我的一个树莓派小车项目
  8. mysql replication
  9. ASP.NET连接ACCESS数据库web.config内路径最优写法
  10. HDU 1870 愚人节的礼物 栈的应用
  11. oracle初始化化表空间用户权限
  12. 合成谬误与公地悲剧(为何设置产品总监职位及核算名义成本)
  13. Vue3 JSON编辑器
  14. exe msdt 无法上网_msdt(缺失msdtexe连不上网)
  15. RFID射频识别系统简述
  16. 黑色星期五 问题描述   有些西方人比较迷信,如果某个月的13号正好是星期五,他们就会觉得不太吉利,用古人的说法,就是“诸事不宜”。请你编写一个程序,统计出在某个特定的年份中,出现了多少次既是13号又
  17. Python 将tif文件分割成多个小tif
  18. java毕业设计电影推荐网站mybatis+源码+调试部署+系统+数据库+lw
  19. 网页贴便签, Chrome特色网页便签纸 OurStickys
  20. 一文总结Keras的loss函数和metrics函数

热门文章

  1. 为什么很多程序员面试造火箭,入职拧螺丝?
  2. 我自己写的3D图形数学库。。。有点乱!
  3. 算法--------------整数反转
  4. 实用ListView加载提示工具类
  5. 一文看懂鸿蒙手机操作系统。和安卓、iOS 有何区别?
  6. Java、Android静态代理与动态代理
  7. Java学习笔记30
  8. Activity应用场景解析
  9. 【Java基础】异常
  10. etcd 访问 锁_在系统中用etcd实现服务注册和发现