这是长答案。
Remotes:
如果您以协作方式使用 Git,您可能需要将您的提交与其他计算机或位置同步。每台机器或位置称为remote,用 Git 的术语来说,每个分支可能有一个或多个分支。大多数情况下,您只会有一个名为origin
。要列出所有遥控器,请运行git remote
:
$ git remote
bitbucket
origin
您可以通过运行来查看这些远程名称是哪些位置的快捷方式git remote -v
:
$ git remote -v
bitbucket [email protected]:flimm/example.git (fetch)
bitbucket [email protected]:flimm/example.git (push)
origin [email protected]:Flimm/example.git (fetch)
origin [email protected]:Flimm/example.git (push)
每个远程都有一个目录.git/refs/remotes/
:
$ ls -F .git/refs/remotes/
bitbucket/ origin/
您机器上的分支:
TLDR:在本地计算机上,您拥有三种类型的分支:本地非跟踪分支、本地跟踪分支和远程跟踪分支。在远程计算机上,您只有一种类型的分支。
1. 当地分支机构
您可以通过运行来查看计算机上所有本地分支的列表git branch
:
$ git branch
master
new-feature
每个本地分支都有一个文件.git/refs/heads/
:
$ ls -F .git/refs/heads/
master new-feature
您的计算机上有两种类型的本地分支:非跟踪本地分支和跟踪本地分支。
1.1 非追踪本地分支机构
非跟踪本地分支不与任何其他分支关联。您可以通过运行来创建一个git branch <branchname>
.
1.2.追踪本地分支机构
跟踪本地分支与另一个分支(通常是远程跟踪分支)相关联。您可以通过运行来创建一个git branch --track <branchname> [<start-point>]
.
您可以使用以下命令查看哪一个本地分支机构正在跟踪分支机构git branch -vv
:
$ git branch -vv
master b31f87c85 [origin/master] Example commit message
new-feature b760e04ed Another example commit message
从该命令的输出中,您可以看到本地分支master
正在跟踪远程跟踪分支origin/master
,以及当地分行new-feature
没有跟踪任何东西。
查看哪些分支正在跟踪分支的另一种方法是查看.git/config
.
跟踪本地分支机构很有用。它们让你可以奔跑git pull
and git push
,而不指定使用哪个上游分支。如果该分支未设置为跟踪另一个分支,您将收到如下错误:
$ 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>
2. 远程跟踪分支(仍在您的机器上)
您可以通过运行来查看计算机上所有远程跟踪分支的列表git branch -r
:
$ git branch -r
bitbucket/master
origin/master
origin/new-branch
每个远程跟踪分支下都有一个文件.git/refs/remotes/<remote>/
:
$ tree -F .git/refs/remotes/
.git/refs/remotes/
├── bitbucket/
│ └── master
└── origin/
├── master
└── new-branch
将远程跟踪分支视为远程计算机所包含内容的本地缓存。您可以使用以下命令更新远程跟踪分支git fetch
, which git pull
在幕后使用。
尽管远程跟踪分支的所有数据都存储在本地计算机上(如缓存),但它仍然不会被称为本地分支。 (至少,我不会这样称呼它!)它只是被称为远程跟踪分支。
远程机器上的分支:
您可以通过运行来查看所有远程分支(即远程机器上的分支)git remote show <remote>
:
$ git remote show origin
* remote origin
Fetch URL: [email protected]:Flimm/example.git
Push URL: [email protected]: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)
This git remote
命令通过网络查询远程计算机有关其分支的信息。它不会更新本地计算机上的远程跟踪分支,请使用git fetch
or git pull
为了那个原因。
从输出中,您可以通过查看“远程分支”标题(忽略标记为“过时”的行)来查看远程计算机上存在的所有分支。
如果您可以登录到远程计算机并在文件系统中找到存储库,则可以查看其下的所有分支refs/heads/
.
备忘单:
-
要安全地删除本地分支,无论是跟踪分支还是非跟踪分支:
git branch -d <branchname>
-
要强制删除本地分支,无论是跟踪分支还是非跟踪分支:
git branch -D <branchname>
-
要删除远程跟踪分支:
git branch -rd <remote>/<branchname>
-
要创建新的本地非跟踪分支:
git branch <branchname> [<start-point>]
-
要创建新的本地跟踪分支:(请注意,如果<start-point>
被指定并且是一个远程跟踪分支,例如origin/foobar
,那么--track
标志自动包含在内)
git branch --track <branchname> [<start-point]
Example:
git branch --track hello-kitty origin/hello-kitty
-
要删除远程计算机上的分支:
git push --delete <remote> <branchname>
-
要删除所有过时的远程跟踪分支,即远程计算机上不再存在的相应分支:
git remote prune <remote>
您可能已经注意到,在某些命令中,您使用<remote>/<branch>
和其他命令,<remote> <branch>
。例子:git branch origin/hello-kitty
and git push --delete origin hello-kitty
.
这看起来可能是任意的,但有一个简单的方法可以记住何时使用斜杠以及何时使用空格。当您使用斜杠时,您指的是自己计算机上的远程跟踪分支,而当您使用空格时,您实际上是在通过网络处理远程计算机上的分支。