Git:从 master 上未暂存/未提交的更改创建分支
上下文:我正在研究 master 添加一个简单的功能。几分钟后,我意识到事情没那么简单,进入一个新分支应该会更好。
这种情况总是发生在我身上,我不知道如何切换到另一个分支并保留所有这些未提交的更改,让主分支保持干净。我认为 git stash && git stashbranch 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")
你知道是否有任何方法可以实现这一点?
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.
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_branch
would simply accomplish that but this is what I get:
~/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没必要藏起来。
2020 更新 / Git 2.23
Git 2.23 添加了新的
switch
子命令,试图消除由于checkout
的超载使用而造成的一些混乱(切换分支、恢复文件、分离 HEAD 等)从此版本的 Git 开始,将下面的 git checkout 命令替换为:
行为保持不变。
在 Update 2020 之前 / Git 2.23
不会触及您的本地更改。它只是从当前 HEAD 创建分支并在那里设置 HEAD。
所以我想这就是你想要的。
--- 编辑以解释 checkout master 的结果 ---
您是否因为
checkout master
不放弃您的更改而感到困惑?由于更改只是本地的,git 不希望您太容易丢失它们。更改分支后,git 不会覆盖您的本地更改。您的
checkout master
的结果是:,这意味着您的工作文件不干净。 git 确实更改了 HEAD,但没有覆盖您的本地文件。这就是为什么尽管您处于
master
状态,但您的最后状态仍然显示您的本地更改。如果您确实想放弃本地更改,则必须使用
-f
强制签出。由于您的更改从未提交,因此您会丢失它们。
尝试返回您的分支,提交更改,然后再次签出主分支。
您应该在第一次结账后收到一条
M
消息,但在checkout master
之后就不再收到一条M
消息,并且git status
应该显示没有修改的文件。--- 编辑以消除有关工作目录(本地文件)的混乱---
在回答您的第一条评论时,本地更改只是......好吧,本地的。 Git 不会自动保存它们,您必须告诉它保存它们以供以后使用。
如果您进行更改并且没有显式提交或存储它们,git 将不会对它们进行版本控制。如果您更改 HEAD (
checkout master
),则本地更改不会被覆盖,因为未保存。No need to stash.
Update 2020 / Git 2.23
Git 2.23 adds the new
switch
subcommand, in an attempt to clear some of the confusion caused by the overloaded usage ofcheckout
(switching branches, restoring files, detaching HEAD, etc.)Starting with this version of Git, replace the
git checkout
command below with:The behavior remains unchanged.
Before Update 2020 / Git 2.23
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.
--- Edit to explain the result of checkout master ---
Are you confused because
checkout master
does not discard your changes?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 master
is:, 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
.If you really want to discard the local changes, you have to force the checkout with
-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.
You should get a
M
message after the first checkout, but then not anymore after thecheckout master
, andgit status
should show no modified files.--- 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.尝试:
Try:
如果您希望当前分支上当前未提交的更改移动到新分支,请使用以下命令创建新分支并自动复制未提交的更改。
这将从当前分支(假设它是主分支)创建一个新分支,复制未提交的更改并切换到新分支。
将文件添加到舞台和将您的更改提交到新分支。
由于创建了一个新分支,因此在将其推送到远程之前,您需要设置上游。使用以下命令设置上游并将其推送到远程。
一旦您点击此命令,将在远程创建一个新分支,并将新的本地分支推送到远程。
现在,如果您想丢弃主分支中未提交的更改,请使用:
这将在签出时丢弃所有未提交的本地更改。
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.
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.
Add files to stage & commit your changes to the new branch.
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.
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:
This will throw away any uncommitted local changes on checkout.
您可以做两件事:
或
(
git checkout -
<-- 破折号是您所在的上一个分支的快捷方式)(
git stash -u
<- --u
表示它还进行未暂存的更改)Two things you can do:
or
(
git checkout -
<-- the dash is a shortcut for the previous branch you were on )(
git stash -u
<-- the-u
means that it also takes unstaged changes )如果您正在使用 GitHub Windows 客户端(就像我一样),并且您处于未提交更改并希望将其移至新分支的情况,则只需通过 GitHub 客户端“创建新分支”即可。它将切换到新创建的分支并保留您的更改。
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.
在最新的 Windows 版 GitHub 客户端中,如果您有未提交的更改,请选择创建新分支。
它会提示您如何处理这种情况:
如果您也只是切换分支,则同样适用。
In the latest GitHub client for Windows, if you have uncommitted changes, and choose to create a new branch.
It prompts you how to handle this exact scenario:
The same applies if you simply switch the branch too.