将现有的、未提交的工作移动到 Git 中的新分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1394797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:59:46  来源:igfitidea点击:

Move existing, uncommitted work to a new branch in Git

gitgit-branchgit-stashgit-reset

提问by Dane O'Connor

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.

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

回答by knittl

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 branchwith:

并提交到您的新分支

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

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

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

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

您不会重置原始分支,它会保持原样。最后一次提交<old-branch>仍然是相同的。因此你checkout -b然后提交。



Update 2020 / Git 2.23

2020 年更新 / Git 2.23

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

Git 2.23 增加了新的switch子命令,以试图清除由于checkout(切换分支、恢复文件、分离 HEAD 等)的重载使用而带来的一些混乱。

Starting with this version of Git, replace above's command with:

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

git switch -c <new-branch>

The behavior is identical and remains unchanged.

行为是相同的并且保持不变。

回答by Robin Qiu

Alternatively:

或者:

  1. Save current changes to a temp stash:

    $ git stash

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

    $ git stash branch <new-branch> stash@{0}

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

    $ git stash

  2. 基于这个 stash 创建一个新分支,并切换到新分支:

    $ git stash branch <new-branch> stash@{0}

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

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

回答by joeytwiddle

If you have been making commitson your main branch while you coded, but you now want to move those commits to a different branch, this is a quick way:

如果您在编码时一直在主分支上进行提交,但现在想将这些提交移动到不同的分支,这是一种快速的方法:

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

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

    git branch -f <previous-branch> <earlier-commit-id>
    

    For example:

    git branch -f master origin/master
    

    or if you had made 4 commits:

    git branch -f master HEAD~4
    
  1. 将您当前的历史记录复制到一个新分支上,同时也带来任何未提交的更改:

    git checkout -b <new-feature-branch>
    
  2. 现在强制原来的“凌乱”分支回滚:(不切换到它)

    git branch -f <previous-branch> <earlier-commit-id>
    

    例如:

    git branch -f master origin/master
    

    或者,如果您已经进行了 4 次提交:

    git branch -f master HEAD~4
    

Warning:git branch -f master origin/masterwill reset the tracking informationfor that branch. So if you have configured your masterbranch to push to somewhere other than origin/masterthen that configuration will be lost.

警告:git branch -f master origin/master重置该分支的跟踪信息。因此,如果您已将master分支配置为推送到其他位置,origin/master则该配置将丢失。

Warning:There is also a danger if you rebase after branching, which is described here. The only way to avoid that is to create a new history using cherry-pick. That link describes the safest fool-proof method. If you have uncommitted changes, you may want to git stashat the start and git stash popat the end.

警告:如果您在分支后变基也会存在危险,这在此处进行了描述。避免这种情况的唯一方法是使用cherry-pick 创建一个新的历史记录。该链接描述了最安全的万无一失的方法。如果您有未提交的更改,您可能希望git stash在开始和git stash pop结束时进行。

回答by Alex Burov

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 分支,并且我希望我的新分支从“master”增长。我没有对我的新工作做出任何承诺。这是分支结构:“master”->“Old_feature”

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

回答by password

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 中工作时,我经常这样做,然后想在推送到我的 origin/ 之前创建一个本地分支。

git cherry-pick <commitID>

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

有很多你可以摘樱桃做的,如所描述这里,但是这可能是一个用例为您服务。

回答by Przemek Struciński

This may be helpful for all using tools for GIT

这可能对所有使用 GIT 工具的人都有帮助

Command

命令

Switch branch - it will move your changes to new-branch. Then you can commit changes.

切换分支 - 它会将您的更改移动到新分支。然后您可以提交更改。

 $ git checkout -b <new-branch>

TortoiseGIT

TortoiseGIT

Right-click on your repository and then use TortoiseGit->Switch/Checkout

右键单击您的存储库,然后使用 TortoiseGit->Switch/Checkout

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

SourceTree

源树

Use the "Checkout" button to switch branch. You will see the "checkout" button at the top after clicking on a branch. Changes from the current branch will be applied automatically. Then you can commit them.

使用“结帐”按钮切换分支。单击分支后,您将在顶部看到“结帐”按钮。当前分支的更改将自动应用。然后你可以提交它们。

enter image description here

在此处输入图片说明

回答by NBhat

I used @Robinanswer & listing all that I did,

我使用@Robin回答并列出我所做的一切,

git status                               <-- review/list uncommitted changes
git stash                                <-- stash uncommitted changes
git stash branch <new-branch> stash@{1}  <-- create a branch from stash
git add .                                <-- add local changes
git status                               <-- review the status; ready to commit
git commit -m "local changes ..."        <-- commit the changes
git branch --list                        <-- see list of branches incl the one created above
git status                               <-- nothing to commit, working tree (new-branch) is clean
git checkout <old-branch>                <-- switch back

!If the repo has more than one stash, see which one to apply to the new-branch:

如果 repo 有多个 stash,请查看将哪个应用到新分支:

git stash list  
  stash@{0}: WIP on ...  
  stash@{1}: WIP on ...

and inspect the individual stash by,

并通过以下方式检查个人藏匿处,

git stash show stash@{1}

Or inspect all stashes at once:

或者一次检查所有藏品:

git stash list -p

回答by Kristofer Doman

There is actually a really easy way to do this with GitHub Desktop now that I don't believe was a feature before.

实际上,使用 GitHub Desktop 有一种非常简单的方法可以做到这一点,因为我认为以前这不是一个功能。

All you need to do is switch to the new branch in GitHub Desktop, and it will prompt you to leave your changes on the current branch (which will be stashed), or to bring your changes with you to the new branch. Just choose the second option, to bring the changes to the new branch. You can then commit as usual.

您需要做的就是切换到 GitHub Desktop 中的新分支,它会提示您将更改保留在当前分支上(将被隐藏),或者将您的更改带到新分支。只需选择第二个选项,将更改带到新分支。然后你可以像往常一样提交。

GitHub Desktop

GitHub 桌面