热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将现有的,未提交的工作移动到Git中的新分支-Moveexisting,uncommittedworktoanewbranchinGit

Istartedsomeworkonanewfeatureandaftercodingforabit,Idecidedthisfeatureshouldbeon

I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch.

我开始研究一个新功能,经过一段时间的编码,我决定这个功能应该在它自己的分支上。

How do I move the existing uncommitted changes to a new branch and reset my current one?

如何将现有的未提交更改移动到新分支并重置当前更改?

I want to reset my current branch while preserving existing work on the new feature.

我想重置当前分支,同时保留新功能的现有工作。

5 个解决方案

#1


2990  

Use the following:

使用以下内容:

git checkout -b 

This will leave your current branch as is, create and checkout a new branch and keep all your changes. You can then make a commit with:

这将使您的当前分支保持原样,创建并签出新分支并保留所有更改。然后您可以使用以下命令进行提交:

git add 

and commit to your new branch with:

并提交到您的新分支:

git commit -m ""

The changes in the working directory and changes staged in index do not belong to any branch yet. This changes where those changes would end in.

工作目录中的更改和索引中的更改不属于任何分支。这会改变这些变化的结果。

You don't reset your original branch, it stays as it is. The last commit on will still be the same. Therefore you checkout -b and then commit.

您没有重置原始分支,它保持不变。 上的最后一次提交仍然是相同的。因此,您签出-b然后提交。

#2


256  

Alternatively:

或者:

  1. Save current changes to a temp stash:

    将当前更改保存到临时存储:

    $ git stash

    $ git stash

  2. Create a new branch based on this stash, and switch to the new branch:

    基于此存储创建新分支,并切换到新分支:

    $ git stash branch stash@{0}

    $ git stash branch stash @ {0}

Tip: use tab key to reduce typing the stash name.

提示:使用Tab键减少键入存储名称。

#3


37  

If you have been making commits on your main branch while you coded, but you now want to move those commits to a different branch:

如果您在编码时已在主分支上进行提交,但现在要将这些提交移动到其他分支:

  1. Copy your current history onto a new branch, bringing along any uncommitted changes too:

    将当前历史记录复制到新分支,同时带来任何未提交的更改:

    git checkout -b 
    
  2. Now force the original "messy" branch to roll back: (without switching to it)

    现在强制原来的“凌乱”分支回滚:(没有切换到它)

    git branch -f  
    

    For example:

    例如:

    git branch -f master origin/master
    

    or if you had made 4 commits:

    或者如果你做了4次提交:

    git branch -f master HEAD~4
    

Warning: It appears that git branch -f master origin/master will reset the tracking information for that branch. So if you have configured your master branch to push to somewhere other than origin/master then that configuration will be lost.

警告:看来git branch -f master origin / master将重置该分支的跟踪信息。因此,如果您已将主分支配置为推送到origin / master之外的某个位置,那么该配置将丢失。

An alternative is to use this reset technique. But those instructions will discard any uncommitted changes you have. If you want to keep those, stash them first and unstash them at the end.

另一种方法是使用这种重置技术。但是这些说明将丢弃您拥有的任何未提交的更改。如果你想保留它们,先将它们藏起来并在最后取出它们。

#4


16  

If you commit it, you could also cherry-pick the single commit ID. I do this often when I start work in master, and then want to create a local branch before I push up to my origin/.

如果你提交它,你也可以挑选单个提交ID。当我开始在master中工作时,我经常这样做,然后想要在我推送到我的原点之前创建一个本地分支。

git cherry-pick 

There is alot you can do with cherry-pick, as described here, but this could be a use-case for you.

如此处所述,你可以使用樱桃挑选,但这可能是一个用例。

#5


0  

The common scenario is the following: I forgot to create the new branch for the new feature, and was doing all the work in the old feature branch. I have commited all the "old" work to the master branch, and I want my new branch to grow from the "master". I have not made a single commit of my new work. Here is the branch structure: "master"->"Old_feature"

常见的情况如下:我忘了为新功能创建新分支,并且正在旧功能分支中完成所有工作。我已将所有“旧”工作提交给主分支,我希望我的新分支从“主”增长。我没有单独提交我的新作品。这是分支结构:“master” - >“Old_feature”

git stash 
git checkout master
git checkout -b "New_branch"
git stash apply

推荐阅读
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • vue使用
    关键词: ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 006_Redis的List数据类型
    1.List类型是一个链表结构的集合,主要功能有push,pop,获取元素等。List类型是一个双端链表的结构,我们可以通过相关操作进行集合的头部或者尾部添加删除元素,List的设 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
author-avatar
为徐嘉伟丷默哀
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有