git 如何退出我当前的分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42594254/
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
How do I exit my current branch?
提问by A Nice Guy
I have cloned a repository and created a branch (branch1
) off it.
I have a few changes in that branch.
Now I want to exit this branch, and create a new branch off of master
.
我克隆了一个存储库并创建了一个分支 ( branch1
)。我在那个分支有一些变化。现在我想退出这个分支,并从master
.
From what I understand, I can create a new branch using git branch myBranch
,
then switch to the branch by git checkout myBranch
.
But won't doing this create a new branch off the existing branch branch1
?
据我了解,我可以使用 创建一个新分支git branch myBranch
,然后通过 切换到分支git checkout myBranch
。但是这样做不会在现有分支上创建一个新分支branch1
吗?
I want to exit my current branch (branch1
) and then create a new branch and enter it (myBranch
)
How can I do this?
我想退出当前分支 ( branch1
) 然后创建一个新分支并输入它 ( myBranch
) 我该怎么做?
回答by buettner123
You can do a
git checkout master
.
Then a
git checkout -b new_branch
你可以做一个
git checkout master
. 然后一个
git checkout -b new_branch
git checkout -b foo
is the short form for git branch foo
and git checkout foo
afterwards.
git checkout -b foo
是git branch foo
和git checkout foo
之后的缩写。
Note that uncommitted changes will be still there on the new branch. If you have any conflicts because the files on master changed in the meantime, you can use git stash
to stash your current changes, checkout your new branch and do a git stash apply
afterwards. You need to resolve occuring conflicts on the new branch then.
请注意,未提交的更改仍将保留在新分支上。如果您因为 master 上的文件在此期间更改而发生任何冲突,您可以使用git stash
stash 您当前的更改,检出您的新分支并在git stash apply
之后执行。然后您需要解决新分支上发生的冲突。
回答by SebNag
From the git branch manual
documentation:
从git branch manual
文档:
SYNOPSIS
概要
git branch [--set-upstream | --track | --no-track] [-l] [-f] < branchname > [< start-point >]
git 分支 [--set-upstream | --track | --no-track] [-l] [-f] <分支名称> [<起点>]
DESCRIPTION
描述
The command [...] creates a new branch head named < branchname > which points to the current HEAD, or < start-point > if given.
Note that this will create the new branch, but it will not switch the working tree to it; use "git checkout " to switch to the new branch.
命令 [...] 创建一个名为 < branchname > 的新分支头,它指向当前的 HEAD,或者 < start-point > 如果给定。
请注意,这将创建新分支,但不会将工作树切换到它;使用“git checkout”切换到新分支。
Thus, git branch myBranch master
will create a new branch starting from the commit to which the current master
branch head refers to.
In order to work on the freshly created branch, run git checkout myBranch
.
This is kinda the long form for git checkout -b myBranch master
.
因此,git branch myBranch master
将从当前master
分支头所引用的提交开始创建一个新分支。
为了在新创建的分支上工作,运行git checkout myBranch
. 这是git checkout -b myBranch master
.
For a more detailed description, read through doc git-branchand doc git-checkout
有关更详细的描述,请阅读doc git-branch和doc git-checkout
回答by smarber
Basically, to switch from a branch to a newbranch with zero changes, you can do the following
基本上,要从一个分支切换到一个零更改的新分支,您可以执行以下操作
# to discard not committed changes for ever
git reset --hard HEAD
# In case you don't want to discard change but put them aside for now
git stash --all
# to switch to the new branch which is created and placed on the <commit_reference>
git checkout -b <new_branch_name> <commit_reference>
However you should explain further what you mean when you say exita branch, remove it? or just switch to another branch. If you want to remove it, you need to run this after the two previous commands
但是,当您说退出分支,删除它时,您应该进一步解释您的意思吗?或者只是切换到另一个分支。如果要删除它,则需要在前两个命令之后运行它
git branch -d <branch_you_exited>
回答by Marina Liu
You can use only one command to meet your requirement, that is
您只能使用一个命令来满足您的要求,即
git checkout -b myBranch commit
git checkout -b myBranch commit
The functions of the command are:
该命令的功能是:
1.Exit current branch.
1.退出当前分支。
2.Create a new branch myBranch (git branch myBranch
) from the commit you specified.
2.git branch myBranch
从您指定的提交创建一个新分支 myBranch ( )。
3.Switch myBranch as current branch (git checkout myBranch
).
3.将 myBranch 切换为当前分支 ( git checkout myBranch
)。
e.g.
例如
git checkout -b myBranch be42c57
, git create a new branch myBranch from be42c57
and point HEAD to it.
git checkout -b myBranch be42c57
, git 创建一个新分支 myBranchbe42c57
并将 HEAD 指向它。
git checkout -b myBranch master
, git will create a new branch myBranch from master (the latest commit id on master) and point HEAD to it.
git checkout -b myBranch master
, git 将从 master (master 上的最新提交 id) 创建一个新分支 myBranch 并将 HEAD 指向它。