Git:从 master 上未暂存/未提交的更改创建一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2569459/
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 08:11:17  来源:igfitidea点击:

Git: Create a branch from unstaged/uncommitted changes on master

gitgit-stash

提问by knoopx

Context: I'm working on master adding a simple feature. After a few minutes I realize it was not so simple and it should have been better to work into a new branch.

上下文:我正在为 master 添加一个简单的功能。几分钟后,我意识到事情并没有那么简单,进入一个新分支应该会更好。

This always happens to me and I have no idea how to switch to another branch and take all these uncommited changes with me leaving the master branch clean. I supposed git stash && git stash branch new_branchwould simply accomplish that but this is what I get:

这总是发生在我身上,我不知道如何切换到另一个分支并采取所有这些未提交的更改,让主分支保持干净。我以为git stash && git stash branch new_branch会简单地完成,但这就是我得到的:

~/test $ git status
# On branch master
nothing to commit (working directory clean)

~/test $ echo "hello!" > testing 

~/test $ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testing
#
no changes added to commit (use "git add" and/or "git commit -a")

~/test $ git stash
Saved working directory and index state WIP on master: 4402b8c testing
HEAD is now at 4402b8c testing

~/test $ git status
# On branch master
nothing to commit (working directory clean)

~/test $ git stash branch new_branch
Switched to a new branch 'new_branch'
# On branch new_branch
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testing
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (db1b9a3391a82d86c9fdd26dab095ba9b820e35b)

~/test $ git s
# On branch new_branch
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testing
#
no changes added to commit (use "git add" and/or "git commit -a")

~/test $ git checkout master
M   testing
Switched to branch 'master'

~/test $ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   testing
#
no changes added to commit (use "git add" and/or "git commit -a")

Do you know if there is any way of accomplishing this?

你知道是否有任何方法可以实现这一点?

回答by Gauthier

No need to stash.

没必要藏起来。

git checkout -b new_branch_name

does not touch your local changes. It just creates the branch from the current HEAD and sets the HEAD there. So I guess that's what you want.

不涉及您的本地更改。它只是从当前 HEAD 创建分支并将 HEAD 设置在那里。所以我想这就是你想要的。

--- Edit to explain the result of checkout master ---

--- 编辑解释结帐大师的结果---

Are you confused because checkout masterdoes not discard your changes?

您是否因为checkout master没有丢弃您的更改而感到困惑?

Since the changes are only local, git does not want you to lose them too easily. Upon changing branch, git does not overwrite your local changes. The result of your checkout masteris:

由于更改只是本地的,git 不希望您太容易丢失它们。更改分支后,git 不会覆盖您的本地更改。你的结果checkout master是:

M   testing

, which means that your working files are not clean. git did change the HEAD, but did not overwrite your local files. That is why your last status still show your local changes, although you are on master.

,这意味着您的工作文件不干净。git 确实更改了 HEAD,但没有覆盖您的本地文件。这就是为什么您的最后状态仍然显示您的本地更改的原因,尽管您处于master.

If you really want to discard the local changes, you have to force the checkout with -f.

如果您真的想放弃本地更改,则必须使用-f.

git checkout master -f

Since your changes were never committed, you'd lose them.

由于您的更改从未提交,因此您会丢失它们。

Try to get back to your branch, commit your changes, then checkout the master again.

尝试返回您的分支,提交您的更改,然后再次检出 master。

git checkout new_branch
git commit -a -m"edited"
git checkout master
git status

You should get a Mmessage after the first checkout, but then not anymore after the checkout master, and git statusshould show no modified files.

您应该M在第一次结帐后收到一条消息,但在checkout master,之后不会再收到一条消息,并且git status不应显示任何修改过的文件。

--- Edit to clear up confusion about working directory (local files)---

--- 编辑以消除有关工作目录(本地文件)的混淆---

In answer to your first comment, local changes are just... well, local. Git does not save them automatically, you must tell it to save them for later. If you make changes and do not explicitly commit or stash them, git will not version them. If you change HEAD (checkout master), the local changes are not overwritten since unsaved.

为了回答您的第一条评论,本地更改只是......好吧,本地更改。Git 不会自动保存它们,您必须告诉它保存它们以备后用。如果您进行更改并且没有明确提交或存储它们,git 将不会对它们进行版本控制。如果您更改 HEAD ( checkout master),则本地更改不会因为未保存而被覆盖。

回答by Grant Limberg

Try:

尝试:

git stash
git checkout -b new-branch
git stash apply

回答by Pylinux

Two things you can do:

你可以做两件事:

git checkout -b sillyname
git commit -am "silly message"
git checkout - 

or

或者

git stash -u
git branch sillyname stash@{0}

(git checkout -<-- the dash is a shortcut for the previous branch you were on )

git checkout -<-- 破折号是您所在的上一个分支的快捷方式)

(git stash -u<-- the -umeans that it also takes unstaged changes )

( git stash -u<---u意味着它也需要非阶段性的变化)

回答by Tod Birdsall

If you are using the GitHub Windows client (as I am) and you are in the situation of having made uncommitted changes that you wish to move to a new branch, you can simply "Crate a new branch" via the GitHub client. It will switch to the newly created branch and preserve your changes.

如果您正在使用 GitHub Windows 客户端(就像我一样)并且您希望移动到新分支进行未提交的更改,您可以通过 GitHub 客户端简单地“创建一个新分支”。它将切换到新创建的分支并保留您的更改。

enter image description here

在此处输入图片说明

回答by Maverick

If you want your current uncommited changes on the current branch to move to a new branch, use the following command to create a new branch and copy the uncommitted changes automatically.

如果您希望当前分支上当前未提交的更改移动到新分支,请使用以下命令创建新分支并自动复制未提交的更改。

git checkout -b branch_name

This will create a new branch from your current branch (assuming it to be master), copy the uncommited changes and switch to the new branch.

这将从您当前的分支(假设它是主分支)创建一个新分支,复制未提交的更改并切换到新分支。

Commit your changes to the new branch.

将您的更改提交到新分支。

git commit -m "First commit"

Since, a new branch is created, before pushing it to remote, you need to set the upstream. Use the below command to set the upstream and push it to remote.

由于创建了一个新分支,因此在将其推送到远程之前,您需要设置上游。使用以下命令设置上游并将其推送到远程。

git push --set-upstream origin feature/feature/NEWBRANCH

Once you hit this command, a new branch will be created at the remote and your new local branch will be pushed to remote.

一旦你点击这个命令,一个新的分支将在远程创建,你的新本地分支将被推送到远程。

Now, if you want to throw away your uncommited changes from master branch, use:

现在,如果您想从 master 分支中丢弃未提交的更改,请使用:

git checkout master -f

This will throw away any uncommitted local changes on checkout.

这将在结帐时丢弃任何未提交的本地更改。

回答by Jerry Dodge

In the latest GitHubclient for Windows, if you have uncommitted changes, and choose to create a new branch.
It prompts you how to handle this exact scenario:

在最新的GitHub客户端 for Windows 中,如果您有未提交的更改,并选择创建一个新分支。
它会提示您如何处理这个确切的场景:

enter image description here

在此处输入图片说明

The same applies if you simply switch the branch too.

如果您也只是切换分支,这同样适用。