git 如何创建一个新分支并将现有代码移动到那里

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

How to create a new branch and move existing code there

gitgit-branch

提问by

Let say that I start to work on clear masterbranch - no changes to commit. Do some local changes but realize that this modifications should be in a separate branchinstead of master.

假设我开始在 clear masterbranch -上工作no changes to commit。做一些本地更改,但意识到这种修改应该在一个单独的branch而不是master.

Is there a way how to movethis changes to separate new branchand restate masterbranch into status no changes to commit?

有没有办法如何移动此更改以将 newbranch和 restatemaster分支分离为 status no changes to commit

EDIT

编辑

Following the accepted answer for git branching - how to make current master a branch and then revert master back to previous version? ...When following the steps, my master will still have modified files.See the last comment 7.

遵循git 分支的公认答案- 如何使当前 master 成为一个分支,然后将 master 恢复到以前的版本?...按照步骤操作,我的master还是会有修改过的文件。见最后评论7

Do I am missing something ?

我错过了什么吗?

$ git branch                                   # 1. starting on master                                     
# On branch master
  nothing to commit, working directory clean


# On branch master                             # 2.modifying a file 
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")



$ git stash                                    # 3. stashing changes
Saved working directory and index state WIP on master: 393bfad initial commit
HEAD is now at 393bfad initial commit
$ git status
# On branch master
nothing to commit, working directory clean


$ git checkout -b experiment                   # 4. creating new branch experiment 
Switched to a new branch 'experiment'


$ git stash pop                                # 5. pop staged changes in exper.
# On branch experiment
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (16b6871d43f367edd03f59558eca4163cd1a2b2f)


$ git checkout master                           #6. going back to master
M   test.txt
Switched to branch 'master'
git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt                #7. in master test.txt is still modified !!!

采纳答案by William Pursell

After you git stash pop, you need to commit to the new branch. Just commit before checking out master.

在你之后git stash pop,你需要提交到新的分支。签出前提交即可master

$ git stash pop
$ git commit -m "Committing changes to branch experiment"
$ git checkout master

Consider the following sequence. From the beginning (ie, you are in branch master with local unstaged changes in the working directory), you could just do:

考虑以下顺序。从一开始(即,您在工作目录中具有本地未暂存更改的分支 master 中),您可以执行以下操作:

$ git checkout -b experiment
$ git add test.txt
$ git commit -m 'Commit message'
$ git checkout master

The advantage of the stash/pop is that it does the 'add' for you and you don't need to specify the files that are changed. But you still need to commit on the new branch.

stash/pop 的优点是它会为您“添加”,您无需指定已更改的文件。但是您仍然需要在新分支上提交。

回答by Oriely

use git stash and git stash pop at the new branch

在新分支上使用 git stash 和 git stash pop

回答by Alex Lauerman

I haven't tested this (and I'm not a git guru that should be blindly trusted), but after reading about stashing (http://git-scm.com/book/en/Git-Tools-Stashing) it seems like the following commands may be best.

我还没有测试过这个(我不是应该盲目信任的 git 大师),但是在阅读了关于 stashing(http://git-scm.com/book/en/Git-Tools-Stashing)之后,似乎像下面的命令可能是最好的。

git stash
git stash branch testchanges

回答by michas

If you did not commit anything, it is no problem to go with git stash. If you already did some commits (and you really should) git stashwon't help. In this case the easiest way is:

如果您没有提交任何内容,那么使用git stash. 如果你已经做了一些提交(你真的应该)git stash将无济于事。在这种情况下,最简单的方法是:

  • You checkout master.
  • You work on the code and do your commits. (Neverhave uncommited changes!)
  • You realize, that you want a new branch based on master.
  • You simply rename your branch git branch -m feature-xy.
  • As this branch still tracks the remote master you can do things like git pull --rebaseto integrate potential upstream changes.
  • Once you are finished push it: git push -u feature-xy:feature-xy(Note the -uto update you upstream branch.)
  • To get a local clean master simply check it out: git checkout master(As there is no local master anymore, git will create a new one from upstream.)
  • 你结账大师。
  • 您处理代码并提交。(永远不要有未提交的更改!)
  • 你意识到,你想要一个基于 master 的新分支。
  • 您只需重命名您的分支git branch -m feature-xy
  • 由于此分支仍跟踪远程主服务器,因此您可以执行诸如git pull --rebase集成潜在上游更改之类的操作。
  • 完成推送后:(git push -u feature-xy:feature-xy注意-u更新上游分支。)
  • 要获得一个本地干净的 master 只需检查一下:(git checkout master因为不再有本地 master,git 将从上游创建一个新的。)