Git:从master上未分配/未提交的更改中创建一个分支 [英] Git: Create a branch from unstaged/uncommitted changes on master

查看:208
本文介绍了Git:从master上未分配/未提交的更改中创建一个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我正在为主人添加一个简单的功能。几分钟后,我意识到这并不是那么简单,而应该更好地工作到一个新的分支。



这总是发生在我身上,我不知道如何切换到另一个分支并采取所有这些无法改变的变化,使主分支保持干净。我认为 git stash&&

 〜/ test $ git stash branch new_branch 完全可以实现,但这是我得到的结果。 git status 
#在分支master上
没有提交(工作目录干净)

〜/ test $ echohello! >测试

〜/ test $ git status
#分支大师
#已更改但未更新:
#(使用git add< file> ... 以更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#修改:测试

没有更改添加到提交(使用git add和/或git commit -a)

〜/ test $ git存储
保存的工作目录和索引状态WIP在master上:4402b8c测试
HEAD现在在4402b8c测试

〜/ test $ git status
#在分支master上
没有提交(工作目录干净)

〜/ test $ git存储分支new_branch
转换为新分支'new_branch'
#在分支上new_branch
#已更改但未更新:
#(使用git add< file> ...来更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#修改:测试

没有改变adde d提交(使用git add和/或git commit -a)
丢弃的refs / stash @ {0}(db1b9a3391a82d86c9fdd26dab095ba9b820e35b)

〜/ test $ git s
#在分支上new_branch
#已更改但未更新:
#(使用git add< file> ...来更新将提交的内容)
#(使用git结账 - < file> ...放弃工作目录中的更改)

#修改:测试

没有更改添加到提交中(使用git add 和/或git commit -a)

〜/ test $ git checkout master
M测试
转换到分支'master'

〜/ test $ git status
#分支大师
#已更改但未更新:
#(使用git add< file> ...更新将要提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#修改:测试

无变化添加到提交(使用git add和/或git commit -a)

你知道是否有任何方式实现这一点?

解决方案

不需要存储。

  git checkout -b new_branch_name 

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



---编辑说明结帐大师的结果---



您是否因为 checkout master 不会放弃您的更改而感到困惑?



因为更改只是本地的,所以git不希望你太容易丢失它们。更改分支后,git不会覆盖您的本地更改。您的结帐大师的结果是:

  M测试

,这意味着您的工作文件不干净。 git确实改变了HEAD,但没有覆盖你的本地文件。这就是为什么你的最后一个状态仍然显示你的本地变化,尽管你在 master



如果你真的想要要放弃本地更改,您必须强制使用 -f 结帐。

  git checkout master -f 

由于您的更改从未提交过,因此您将失去它们。



试着回到你的分支,提交你的修改,然后再重新签出master。

  git checkout new_branch 
git commit -a -medited
git checkout master
git status

您应该在第一次结帐后得到 M 消息,但是在 checkout master 和 git status 应该不会显示修改过的文件。

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



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


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?

解决方案

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.

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

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.

If you really want to discard the local changes, you have to force the checkout with -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.

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

You should get a M message after the first checkout, but then not anymore after the checkout master, and git 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.

这篇关于Git:从master上未分配/未提交的更改中创建一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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