将现有的、未提交的工作移动到 Git 中的新分支 [英] Move existing, uncommitted work to a new branch in Git

查看:31
本文介绍了将现有的、未提交的工作移动到 Git 中的新分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一些新功能的工作,在编码了一段时间后,我决定这个功能应该放在自己的分支上.

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

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

解决方案

Update 2020/Git 2.23

Git 2.23 添加了新的 switch 子命令尝试清除因 checkout<的过度使用而引起的一些混乱/a>(切换分支、恢复文件、分离HEAD等)

从此版本的 Git 开始,将 checkout 命令替换为:

git switch -c <新分支>

行为相同且保持不变.


2020 年更新前/Git 2.23

使用以下内容:

git checkout -b <新分支>

这将使您当前的分支保持原样,创建并签出一个新分支并保留您的所有更改.然后,您可以暂存文件中的更改以提交:

git add <文件>

并提交到您的新分支:

git commit -m "<此提交的简要描述>";

工作目录中的更改和索引中暂存的更改不属于任何分支.这会更改这些修改结束的分支.

您不必重置原始分支,它会保持原样.

上的最后一次提交仍然是相同的.因此你 checkout -b 然后提交.

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.

解决方案

Update 2020 / Git 2.23

Git 2.23 adds the new switch subcommand in an attempt to clear some of the confusion that comes from the overloaded usage of checkout (switching branches, restoring files, detaching HEAD, etc.)

Starting with this version of Git, replace the checkout command with:

git switch -c <new-branch>

The behavior is identical and remains unchanged.


Before Update 2020 / Git 2.23

Use the following:

git checkout -b <new-branch>

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

git add <files>

and commit to your new branch with:

git commit -m "<Brief description of this commit>"

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

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

这篇关于将现有的、未提交的工作移动到 Git 中的新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆