建立新分支 [英] Create new branch

查看:96
本文介绍了建立新分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建新的分支'B'.目前,我有一个master分支(本地和远程)和功能分支-'A'(本地).功能分支(A)已在远程中删除.另外,我的本地功能分支中有一些已提交的文件和未暂存的文件.我想在不损失任何更改的情况下上手,并创建另一个分支,先提交该分支,然后提交我的新更改.我能怎么做?请提出建议.

I want to create new branch 'B'. Currently, I have a master branch(local and remote) and feature branch- 'A'(local). Feature branch(A) is deleted in remote. Also, I have some files committed and unstaged files in my local feature branch. I want to go to master without losing any of changes and create another branch , commit the branch first and then commit my new changes. How can I do? Please suggest.

推荐答案

现在,我看到在某些地方您写了未跟踪的变更,在某些地方还进行了未暂存的变更.这是两个不同的事物,应该以不同的方式处理.如果您已跟踪但未暂存更改,则应在签出新分支之前git stash进行更改,并在将A合并到新分支中后git stash pop进行更改.

edit: I now see that in some places you wrote that you had untracked changes, and in some that you had unstaged changes. These are two different things and should be handled differently. If you have tracked but unstaged changes, you should git stash your changes before checking out the new branch, and git stash pop them after merging A into the new branch.

edit 2:对于未跟踪的更改,另一种确保未隐藏的更改也可以仅将它们应用于新分支的方法是将git stash与标志-u一起使用,这也会存储未跟踪的更改.在这种情况下的流量将是

edit 2: For untracked changes, another way to ensure that they are also stashed and then just apply them to the new branch is to use git stash with the flag -u, which also stashes untracked changes. The flow in that case would be

git branch newBranch master
git stash -u
git checkout newBranch
git merge A
git stash pop
git add && git commit

根据您在评论中提供的其他信息,我认为您要达到的状态如下:

Based on the additional information you provided in the comments, I believe the state you are trying to arrive at is the following:

  1. master分支保持不变
  2. 功能分支A保持不变
  3. 有一个新分支B从master分支出来,其中包含分支A
  4. 中已提交和未跟踪的更改.
  1. master branch remains unchanged
  2. feature branch A remains unchanged
  3. There is a new branch B branching off of master, which contains both the committed and untracked changes from branch A

首先,我想根据您后续的问题来澄清一些您可能不知道的事情:使用git checkout签出其他分支或提交时,或执行操作命令时其他分支,您先前签出的分支/提交上的所有提交都不会丢失,您可以随时使用git checkout重新回到它.此外,除非在您检出的commit/branch中对未跟踪的文件进行跟踪,否则将不会对其进行修改.

First, I'd like to clarify something which I think based on your follow-up questions you may not be aware of: When you use git checkout to checkout a different branch or commit, or when you execute commands that manipulate other branches, whatever was committed on the branch / commit you previously had checked out is not lost, and you can go back to it at any time using git checkout again. Additionally, untracked files will not be modified unless they are tracked in the commit / branch you are checking out.

现在,要解决您的问题,我将使用以下命令: 1. git branch newBranch mastermaster分支创建新分支.
2. git checkout newBranch签出新创建的分支.请记住,除非在新分支上对未跟踪的文件进行跟踪,否则将不会对其进行修改,根据您的描述,我知道它们不会被修改.还请记住,在分支A上提交的任何内容都将保留在那里,并且不会因切换到另一个分支而丢失.
3. git merge A合并从分支A到新分支的已提交更改.
4. git addgit commit添加未跟踪的更改并将它们提交到新分支.

Now, to solve your question, I would use the following commands: 1. git branch newBranch master to create a new branch off of master branch.
2. git checkout newBranch to checkout the newly created branch. Remember that untracked files wont be modified unless they are tracked on the new branch, which from your description I understand they are not. Also remember that whatever was committed on branch A will stay there and will not be lost by switching to another branch.
3. git merge A to merge the committed changes from branch A to the new branch.
4. git add and git commit to add the untracked changes and commit them to the new branch.

最后,您可以继续在新分支上工作,或者如果您想返回到分支A,则可以执行git checkout A返回.请注意,由于现在在分支B上跟踪了在分支A上未跟踪的更改,因此,如果再次更改它们并检出B,您将在从A进行新更改之前获得版本.

Finally, you can either keep working on the new branch or if you want to go back to branch A you can execute git checkout A to go back. Note that since the changes which were untracked on branch A are now tracked on branch B, if you change them again and checkout B you will get the version before the new changes from A.

这篇关于建立新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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