git:在不分离头的情况下切换分支 [英] git: switch branch without detaching head

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

问题描述

我在 github 上有一个存储库,其中有一个主分支(master)和一个用于一些实验工作的分支.我做了一些提交并推送到实验分支,一切都很好.

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.

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

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 新手,所以如果我是个白痴,我很抱歉,但我在文档中找不到任何可以帮助我重新思考的内容.

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 branch --track 语法比 git checkout -b branch-name origin/branch-name 更直观.

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.

感谢您的帮助!

推荐答案

# first time: make origin/branchname locally available as localname
git checkout -b localname origin/branchname 

# othertimes 
git checkout localname 

git push origin

为方便起见,您可以对 localname & 使用相同的字符串.分支名称
当您检出 origin/branchname 时,您并没有真正检出分支.origin/branchname 是一个远程"名称,您可以使用

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天全站免登陆