$ git checkout new-feature
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream new-feature <remote>/<branch>
您可以通过运行git remote show <remote>查看所有远程分支(即远程机器上的分支):
$ git remote show origin
* remote origin
Fetch URL: git@github.com:Flimm/example.git
Push URL: git@github.com:Flimm/example.git
HEAD branch: master
Remote branches:
io-socket-ip new (next fetch will store in remotes/origin)
master tracked
new-branch tracked
Local ref configured for 'git pull':
master merges with remote master
new-branch merges with remote new-branch
Local ref configured for 'git push':
master pushes to master (up to date)
new-branch pushes to new-branch (fast-forwardable)
git push -u origin myNewBranch # Pushes your newly created local branch "myNewBranch"
# to the remote "origin".
# So now a new branch named "myNewBranch" is
# created on the remote machine named "origin"
git pull origin myNewBranch # Pulls new commits from branch "myNewBranch"
# on remote "origin" into remote tracking
# branch on your machine "origin/myNewBranch".
# Here "origin/myNewBranch" is your copy of
# "myNewBranch" on "origin"
git checkout myNewBranch # Switch to myNewBranch
git pull # Updates remote tracking branch "origin/myNewBranch"
# to be in sync with the remote branch "myNewBranch"
# on "origin".
# Pulls these new commits from "origin/myNewBranch"
# to local branch "myNewBranch which you just switched to.
WHERE ---BRANCH TYPE-------- --REFERENCE TARGETS-------
--------------------------------------------------------------
Remote simple branch -----------> remote head (a commit ID)
--------------------------------------------------------------
Local simple branch -----------> local head (a commit ID)
Local local tracking-branch --> local head (a commit ID1)
--> Remote-name/branch-name
Local remote tracking-branch --> local head (a commit ID2)
--> Remote-name/branch-name
--------------------------------------------------------------
'a snapshot' - A recording of the state of one or more files
and their contents at a given moment in time.
'a commit' - A container holding one snapshot, the date and
time it was recorded, who recorded it, and a
comment to say what it's all about.
'a repository' - A repository of commits, organized so we can
look thru them, going backwards in time.
Much like photos added in sequence to a photo
album book, to record our own history, each commit
contains a snapshot of the exact state of our
project at a given moment in time.
It is used to be able to look backwards in time to
how it was at any recorded previous time.
'Remote' - (Upper case) Short for 'a named remote repository'
(of commits, of snapshots)
'remote' - (Lower case) Located on another git repository
'local' - Located on your local git repository
'a head' - A specific young commit, with no children yet of
it's own (i.e. no other commits yet pointing to it),
but which may link backwards in time to one or more
of its natural parents.
Also called a growing tip.
Initially set to a <start-point>.
'a branch' - A symbolic name (i.e. an identifier) pointing
to one specific head, and possibly, depending on
the branch type, also pointing to a remote branch.
The term 'branch' can also refer to a specific
linked list of multiple commits (plural), starting
from the growing tip (or most recent baby), and
linking offspring to their parent(s) backwards in
time.
'tracks' - As we move forward, tracks are what we leave behind.
'tracked' - To be followed, as in, to come afterwards, or after
the fact, by way of the evidence left behind, of the
a state of being of the thing being tracked, as it
moves forwards in time.
'tracking' - The process of capturing and organizing snapshots of
our project so we can later look backwards in time
to find how it previously was.
'tracking-branch' - This term is somewhat redundant, and confusing,
but does have a specific, important meaning.
I have deliberately added the hyphen, because this
term does NOT mean simply 'tracking branch'. (Grab
your aspirin, and a cold pack for your head, lol.)
Because all branches in git are used for, and only
used for, tracking your project, therefore it could
be said that ALL branches are actually
'tracking-branches', but we don't call them that.
Instead we call them, simply 'branches'.
But then what is a 'tracking-branch'?
TL;DR A 'tracking-branch' is a local name that points to
two branches at the same time.
So when you read 'tracking-branch, it might be
helpful to instead think: 'branch-pair'.
(Normal branches only point to one thing, the
head, which is the commit at a growing tip.
And they do not have any symbolic pointers.)
1) The first branch a 'tracking-branch' points to
is the same as for any other branch: a local head,
(i.e. a young commit in our local repository without
any children.) This is where a tracking-branch
keeps a full local copy of a remote branch.
Note that it doesn't necessiarialy hold a full
duplicate copy of the entire second, remote
repository. If you have cloned the remote
repository then you already have most, if not all
of their commits in your own local repository.
2) The second branch a 'tracking-branch' points to
is a branch on a remote repository.
It does this with a <remote-name>/<branch-name>.
The 'remote-name' is used to find the URL to the
remote repository. See `git remote -v`.
Why point to two branches?
This is to be able to operate on two heads at the
same time, like to copy commits from one head to
the other as `git fetch` and `git push` does.
We have two types of 'tracking-branches' (both on
our local repository):
'local tracking-branches',
with a simple branch name, and
'remote tracking-branches',
with a path-style branch name.
See `git branch -avv`. For example:
So our 'remote tracking-branches' are not remote
branches, on a remote repository, but rather are
local branches, which have a local head of their
own, pointing to a local commit, and also at the
same time symbolically pointing, to a remote
branch.
With `git branch -avv`, notice how two branches can
point to origin/remote:
* the first being the 'local-tracking-branch'
with the name 'master', and with the
'[origin/master]' extra clause, and
* the second being the 'remote-tracking-branch'
with the name 'origin/master'.
NOTE: Though they point to the same remote branch,
the local commit head is not always the same!
Thus they are actually two different branches.
The 'local-tracking-branch' is our working branch,
and the 'remote-tracking-branch' is a copy of the
remote's branch that we cloned from or fetched to
update.
型
调查
遥控器
git remote # List names of known Remotes
git remote -v # List names of known Remotes and
# show the 2 URL's pointing to them
#
# See '[remote "<names>"]' in
# $ cat .git/config
型 远程分支 (位于远程存储库上)
git remote show <remote-name> # Download and view
# a specific Remote's info.
# for example, let's download the information for
# two remotes named origin and upstream:
git branch -avv # Show ALL 'local branches', verbosely; (3 types):
git branch -rv # -- type 1 -------------------------------------
# Show ONLY 'local branches' that point to
# 'remote branches' (-r = remote; -v = verbose)
#
# This lists your 'Remote tracking branches'!
# From: $ tree .git/refs/remotes/*
#
# They allow us to move snapshots between
# repositories, and to keep a copy of
# Remote's branches locally.
git branch -vv # -- types 2 and 3 ------------------------------
# Show ONLY 'local branches', that point to local
# things, but his includes two different types of
# branches mixed together, for example:
* master de430b6 [origin/master] <comment describing this branch>
updates 3c40299 [origin/updates] <comment describing this branch>
foo de430b6 <comment describing this branch>
4条答案
按热度按时间k7fdbhmy1#
这是一个很长的答案。
远程:
如果你在协作使用Git,你可能需要将你的提交与其他机器或位置同步。在Git的术语中,每台机器或位置都被称为remote,每台机器或位置都可能有一个或多个分支。大多数情况下,您只有一个名为
origin
的对象。要列出所有远程,请运行git remote
:字符串
您可以通过运行
git remote -v
来查看这些远程名称是哪些位置的快捷方式:型
每个远程都有一个位于
.git/refs/remotes/
下的目录:型
机器上的分支:
TLDR:在本地机器上,有三种类型的分支:本地非跟踪分支、本地跟踪分支和远程跟踪分支。在远程机器上,只有一种类型的分支。
1.本地分支
您可以通过运行
git branch
查看机器上所有本地分支的列表:型
每个本地分支在
.git/refs/heads/
下有一个文件:型
您的计算机上有两种类型的本地分支:非跟踪本地分支和跟踪本地分支。
1.1非跟踪本地分支
非跟踪本地分支不与任何其他分支相关联。您可以通过运行
git branch <branchname>
创建一个。1.2.跟踪本地分支
跟踪本地分支与另一个分支相关联,通常是远程跟踪分支。您可以通过运行
git branch --track <branchname> [<start-point>]
创建一个。您可以使用
git branch -vv
查看本地分支机构正在跟踪分支机构:型
从这个命令的输出中,您可以看到本地分支
master
正在跟踪远程跟踪分支origin/master
,而本地分支new-feature
没有跟踪任何东西。查看哪些分支正在跟踪分支的另一种方法是查看
.git/config
。跟踪本地分支非常有用。它们允许您运行
git pull
和git push
,而无需指定使用哪个上游分支。如果分支没有被设置为跟踪另一个分支,你会得到一个类似这样的错误:型
2.远程跟踪分支(仍在本机)
您可以通过运行
git branch -r
查看计算机上所有远程跟踪分支的列表:型
每个远程跟踪分支都有一个
.git/refs/remotes/<remote>/
下的文件:型
将远程跟踪分支视为远程机器所包含内容的本地缓存。您可以使用
git fetch
更新远程跟踪分支,git pull
在幕后使用。即使远程跟踪分支的所有数据都存储在您的机器上(像缓存一样),它仍然不被称为本地分支。(至少我不会这么叫它!)它只是被称为远程跟踪分支。
远程机器上的分支:
您可以通过运行
git remote show <remote>
查看所有远程分支(即远程机器上的分支):型
这个
git remote
命令通过网络查询远程机器的分支。它不会更新本地机器上的远程跟踪分支,请使用git fetch
或git pull
。从输出中,您可以通过查看标题“远程分支”(忽略标记为“陈旧”的行)下的内容来查看远程机器上存在的所有分支。
如果您可以登录到远程机器并在文件系统中找到存储库,那么您可以查看
refs/heads/
下的所有分支。小抄:
型
型
<start-point>
,并且是类似于origin/foobar
的远程跟踪分支,则--track
标志将自动包括在内)范例:
您可能已经注意到,在某些命令中,您使用
<remote>/<branch>
,而在其他命令中,使用<remote> <branch>
。示例:git branch origin/hello-kitty
和git push --delete origin hello-kitty
。这可能看起来很随意,但有一个简单的方法可以记住什么时候使用斜杠,什么时候使用空格。当你使用斜杠时,你指的是你自己机器上的一个远程跟踪分支,而当你使用空格时,你实际上是在通过网络处理一个远程机器上的分支。
3wabscal2#
本地分支是指只有您(本地用户)才能看到的分支。它只存在于您的本地计算机上。
字符串
remote branch是位于远程位置的分支(大多数情况下为
origin
)。您可以将新创建的本地分支myNewBranch
推送到origin
。现在其他用户可以跟踪它。型
远程跟踪分支是远程分支的本地副本。当使用上面的命令将
myNewBranch
推送到origin
时,将在您的计算机上创建名为origin/myNewBranch
的远程跟踪分支。此远程跟踪分支跟踪origin
上的远程分支myNewBranch
。您可以使用git fetch
或git pull
更新远程跟踪分支,使其与远程分支同步。型
本地跟踪分支是跟踪另一分支的本地分支。这样你就可以把提交推到/从另一个分支拉取。本地跟踪分支在大多数情况下跟踪远程跟踪分支。当您使用带有
-u
选项的git push
命令将本地分支推送到origin
时(如上所示),您将本地分支myNewBranch
设置为跟踪远程跟踪分支origin/myNewBranch
。这是使用git push
和git pull
而不指定要推送到或拉取的上游所必需的。型
3pvhb19x3#
当地分支:
机器上的一个分支,您可以在其中工作并添加提交。您可以使用
git branch
列出这些分支。本地分支(带跟踪):
一种普通的本地分支机构,配置成与远程分支机构相对应。这样做的好处是,无需指定存储库和分支名称即可访问
git pull
和git push
。跟踪还会使git status
在您的分支位于远程之前或之后时通知您。远程分支:
只是远程存储库上的一个分支-通常在服务器上,如GitHub等。
远程跟踪分支:
远程分支的本地副本。永远不应编辑此分支。其目的是跟踪远程分支的当前状态。可以使用
git branch -r
查看远程跟踪分支,通常类似于origin/master
(仓库名称后跟斜杠,然后是分支名称)。运行git fetch
将更新远程跟踪分支,以反映相应远程分支的状态。git branch -avv
是我个人的最爱,它可以快速显示哪些分支在我的机器上,哪些分支在远程,以及每个分支中的最新提交。-a
部分指定应显示所有分支(远程和本地)。最后的v
代表verbose(它显示了最后一次提交的哈希和消息)。感谢@Flimm指出第二个v
添加了关于哪个本地分支跟踪哪个远程分支的信息。vh0rcniy4#
概述
TL;DR-这是有组织的,以便您可以跳到您需要知道的内容。
以下是我将介绍的内容:
快速概览
一个简单的分支是一个本地名称,它引用了一件事:
1.它直接指向本地头部(即,到特定的提交;生长尖端)
tracking-branch是一个本地名称,它引用了两个东西:
1.它直接指向本地头部(即,到特定的提交;生长尖端)、和
1.它象征性地指向远程存储库上的第二个分支。
跟踪分支有两种:
*local-分支指向本地头。
这些分支称为 local-tracking-branches**。* (下面有更多内容)*
*remote-分支指向远程head的本地副本。
这些分支称为 remote-tracking-branches**。* (下面有更多内容)*
以下是4种类型的分支,我们在哪里看到它们,以及它们如何Map:
字符串
简短词汇表
术语Remote和分支似乎重载。
短语tracking分支特别令人困惑,因为它与tracking-branch不是一回事。
x1c 0d1x的数据
master
告诉我们master
当前是默认分支(即我们的工作区域中检出的内容)。顺便说一句,名称master
是refs/heads/master
的缩写。git remote set-head <remote-name> <remote tracking-branch name>
进行设置。(注意,这也不是git remote show <remote-name>
返回的HEAD,它是远程存储库的HEAD的下载值。请注意,所有分支都引用一个提交ID(十六进制数字)。
remotes/origin/HEAD
不是一个分支,所以它没有这个。还要注意,前两行和最后两行也有一个对远程(在本例中,远程名为
origin
)上分支的符号引用。这里的“师父”是我们当地的工作分支。而
remotes/origin/master
是名为master
的分支的本地副本,该分支是从我们称之为origin
的远程获取的(由git fetch
、git clone
或git pull
)。(BTW,
origin
是我们最初使用git clone
命令克隆的远程存储库的默认名称。)型
调查
遥控器
型
远程分支 (位于远程存储库上)
型
的
origin
。使用git remote -v
查看它们。git branch -avv
的远程进行本地读取远程分支:主跟踪更新跟踪
git remote show所做的就是通过网络电话使用
git ls-remote
调用[the remote],并将它们的引用与您的引用进行比较,根据这些结果猜测git fetch
和git push
会做什么。(如果使用git pull
,这意味着运行git fetch
,然后运行git merge
。git remote show
命令也试图猜测它的作用。)本地分支机构 (位于本地存储库中)
型
请注意,前两个名为
master
和updates
的分支(见上图)都在其提交编号后有一个额外的字段。例如,对于名为“master”的分支,此字段为[origin/master]
。这告诉我们,这两个分支不是普通的本地分支,而是Local tracking-branches。与上面的“远程跟踪分支”类似,它们也象征性地指向远程分支。因此,在本例中,
master
不仅指向本地存储库中的分支head,而且还指向远程存储库中的origin/master
。这些额外的字段由.git/config中的参数设置。
相比之下,这里的
foo
是一个简单的正常分支,即非跟踪。相关文件
配置
使用
git branch,
git checkout -b
创建,或通过使用git clone
克隆远程存储库,或通过直接编辑.git/config
或使用以下命令显式管理:遥控器
通过使用
git clone
克隆git仓库,隐式地使用git remote创建。git remote add
-显式添加新的远程名称(到.git/config)git remote rename
git remote remove
-删除远程git remote prune
-删除已在远程上删除的任何本地远程跟踪分支设置属性:
git set-url
-设置一个url,或替换远程的urlgit set-url --add
-将一个url附加到远程的url列表中git set-url --delete
-删除所有与模式匹配的urlgit set-branches
-更改跟踪的分支集git set-branches --add
-追加,而不是完全替换当前跟踪的分支列表git set-head
-设置 default 远程分支(即遥控器的HEAD)git set-head --auto
- query remote为远程分支设置本地HEADgit set-head --delete
-删除 default 远程分支(即遥控器的HEAD)分支机构
协作
使用默认配置,当你
git clone
时,它会自动设置你的远程和跟踪分支。但是请注意,有一些配置设置将禁用或更改其工作方式。提示在
git fetch
和git push
上使用**--dry-run
**选项,先看看会发生什么,然后再进行操作。使用**
git fetch
**(可能通过调用git pull
)来更新远程提交的本地副本,以使您保持最新状态。如果不包含,则使用默认值。您可以在
[remote "<remote-name>"]
下的fetch=
属性中看到.git/config
中的默认值。这可能看起来像这样:语法为
[+]?<source>:<destination>
。这意味着从.git/refs/heads/*
获取refs(通常是commits和tags),这些refs是远程仓库中的普通简单分支,并将它们放入我们的本地.git/refs/remotes/origin/*
分支,这是我们的跟踪分支。很酷,嗯!顺便说一句,“+”表示即使这不会是一个快进也要更新。使用**
git push <remote> <branch>
**将本地提交发送到您有写入权限的远程仓库。我希望我没弄错。