Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

git - Git:从master上的未分级/未提交的更改创建分支(Git: Create a branch from unstaged/uncommitted changes on master)

Context: I'm working on master adding a simple feature.

(上下文:我正在为master添加一个简单的功能。)

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:

(我认为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?

(你知道有没有办法实现这个目标?)

  ask by knoopx translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(它只是从当前HEAD创建分支并在那里设置HEAD。)

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?

(您是否感到困惑,因为checkout master不会丢弃您的更改?)

Since the changes are only local, git does not want you to lose them too easily.

(由于更改只是本地的,因此git不希望您太容易丢失它们。)

Upon changing branch, git does not overwrite your local changes.

(更改分支后,git不会覆盖您的本地更改。)

The result of your checkout master is:

(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.

(git确实改变了HEAD,但没有覆盖你的本地文件。)

That is why your last status still show your local changes, although you are on master .

(这就是为什么你的上一个状态仍然显示你的本地更改,虽然你是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.

(尝试返回到您的分支,提交更改,然后再次检出主服务器。)

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.

(您应该在第一次结账后收到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.

(Git不会自动保存它们,您必须告诉它以便以后保存它们。)

If you make changes and do not explicitly commit or stash them, git will not version them.

(如果您进行更改并且未显式提交或存储它们,则git不会对它们进行版本控制。)

If you change HEAD ( checkout master ), the local changes are not overwritten since unsaved.

(如果更改HEAD( checkout master ),则自未保存后不会覆盖本地更改。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...