git:在不分离头的情况下切换分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/471300/
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
git: switch branch without detaching head
提问by Dana Robinson
I have a repository on github with a main branch (master) and a branch for some experimental work. I made some commits and pushed to the experimental branch and everything was fine.
我在 github 上有一个存储库,其中有一个主分支(master)和一个用于一些实验工作的分支。我做了一些提交并推送到实验分支,一切都很好。
Now, on a different machine, I try to clone my repository (git clone repository) and then switch to the experimental branch (git checkout branchname) but every time I do this my head gets detached and I can't push my changes. What am I doing wrong? I get the feeling I'm missing a fundamental git concept someplace but reading random git man pages isn't giving me any clues.
现在,在另一台机器上,我尝试克隆我的存储库(git clone repository),然后切换到实验分支(git checkout branchname),但每次我这样做时,我的头都会被分离,我无法推送我的更改。我究竟做错了什么?我觉得我在某个地方缺少一个基本的 git 概念,但是阅读随机的 git 手册页并没有给我任何线索。
I'm new to git so I'm sorry if I'm being an idiot but I can't find anything in the docs that will help me reattach my head.
我是 git 新手,所以如果我是个白痴,我很抱歉,但我在文档中找不到任何可以帮助我重新思考的内容。
EDIT
编辑
The concept of a tracking branch is what I was missing. Now that I grok that concept everything is clear. Personally, I find the git branch --track
syntax to be much more intuitive than git checkout -b branch-name origin/branch-name
.
跟踪分支的概念是我所缺少的。现在我理解了这个概念,一切都清楚了。就个人而言,我发现git branch --track
语法比git checkout -b branch-name origin/branch-name
.
Thanks for the help!
谢谢您的帮助!
回答by Kent Fredric
# first time: make origin/branchname locally available as localname
git checkout -b localname origin/branchname
# othertimes
git checkout localname
git push origin
For convenience, you may use the same string for localname & branchname
When you checked out origin/branchname
you weren't really checking out a branch.
origin/branchname
is a "remote" name, and you can get a list of them with
为了方便起见,你可以使用相同字符串的localName和BRANCHNAME
当你签出origin/branchname
你是不是真的检查出的一个分支。
origin/branchname
是一个“远程”名称,您可以使用以下命令获取它们的列表
branch -a
If you have colours enabled, local branches will be one colour, and remote another.
如果您启用了颜色,则本地分支将是一种颜色,而远程分支将是另一种颜色。
You have to first make a remote branch tracked locally in order to be able to switch-to and work on it.
您必须首先在本地跟踪一个远程分支,以便能够切换到并对其进行处理。
回答by Christoph Rüegg
git clone [email protected]:abc/def.git
cd def
Now create a tracking branch:
现在创建一个跟踪分支:
git branch --track experimental origin/experimental
git checkout experimental
Then, after working there, simply push to github by
然后,在那里工作后,只需通过以下方式推送到 github
git push
回答by Pat Notz
To expand on Kent's reply, after you do your clone the only branch you'll have (remotes don't count) is the one that was active in the repository you cloned from -- master in your case.
为了扩展 Kent 的回复,在您进行克隆之后,您将拥有的唯一分支(遥控器不算在内)是您从中克隆的存储库中处于活动状态的分支——在您的情况下是 master。
So, first you'll want to create a new branch to track the remote experimental branch:
因此,首先您需要创建一个新分支来跟踪远程实验分支:
$ git branch experimental origin/experimental
and then check it out:
然后检查一下:
$ git checkout experimental
However, Kent is correct -- these two commands can be combined
但是,Kent 是正确的——这两个命令可以组合使用
$ git checkout -b experimental origin/experimental
回答by VonC
With Git 2.23 (August 2019), you would use the git switch
command
使用 Git 2.23(2019 年 8 月),您将使用git switch
命令
If you have a remote branch of the same name, it will be automatically tracked:
如果你有一个同名的远程分支,它会被自动跟踪:
$ git switch new-topic
Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
Switched to a new branch 'new-topic'