git:切换分支而不分离头部 [英] git: switch branch without detaching head

查看:315
本文介绍了git:切换分支而不分离头部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在github上有一个主分支(主)和一个分支用于一些实验性工作。我做了一些提交并推送到实验分支,一切正常。

现在,在另一台机器上,我尝试克隆我的存储库(git clone repository )然后切换到实验分支(git checkout branchname ),但每次我这样做时,我的头都会分离,我无法推送我的更改。我究竟做错了什么?我感觉我错过了某个地方的基本git概念,但是阅读随机git手册页并没有给我任何线索。



我是git的新手,所以我对不起,如果我是一个白痴,但我找不到任何文件,这将帮助我重新贴上我的头。

编辑



跟踪分支的概念就是我所缺少的。现在我对这个概念有了一个清晰的认识。就个人而言,我发现 git branch --track 语法比 git checkout -b branch-name origin / branch-name <

感谢您的帮助!

解决方案

 #第一次:make origin / branchname本地作为本地名称提供
git checkout -b本地名称origin / branchname

#其他情况
git checkout localname

git push origin

为了方便,你可以使用相同的字符串作为localname& branchname

当您签出 origin / branchname 时,您并未真正检查分支。
origin / branchname 是一个远程名称,您可以使用
<$如果你启用了颜色,本地分支机构将是一种颜色,而另一种则偏远。



您必须首先在本地进行远程分支追踪,以便切换并处理它。


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.

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.

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.

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.

Thanks for the help!

解决方案

# 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

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.

这篇关于git:切换分支而不分离头部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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