-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathindex.html
321 lines (271 loc) · 14.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
---
layout: zh_reference
---
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes">书</a>
</span>
分享与更新项目
</h2>
<div class="block">
<p>
Git 并不像 Subversion 那样有个中心服务器。
目前为止所有的命令都是本地执行的,更新的只是本地的数据库。
要通过 Git 与其他开发者合作,你需要将数据放到一台其他开发者能够连接的服务器上。
Git 实现此流程的方式是将你的数据与另一个仓库同步。在服务器与客户端之间并没有实质的区别 ——
Git 仓库就是 Git 仓库,你可以很容易地在两者之间同步。
</p>
<p>
一旦你有了个 Git 仓库,不管它是在你自己的服务器上,或者是由 GitHub 之类的地方提供,
你都可以告诉 Git 推送你拥有的远端仓库还没有的数据,或者叫 Git 从别的仓库把差别取过来。
</p>
<p>
联网的时候你可以随时做这个,它并不需要对应一个 <code>commit</code> 或者别的什么。
一般你会本地提交几次,然后从你的项目克隆自的线上的共享仓库提取数据以保持最新,将新完成的合并到你完成的工作中去,然后推送你的改动会服务器。
</p>
<p class="nutshell">
<b>简而言之</b> 使用 <code>git fetch</code> 更新你的项目,使用 <code>git push</code> 分享你的改动。
你可以用 <code>git remote</code> 管理你的远程仓库。
</p>
</div>
</div>
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-remote">文档</a>
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Showing-Your-Remotes">书</a>
</span>
<a name="remote">git remote</a>
<span class="desc">罗列、添加和删除远端仓库别名</span>
</h2>
<div class="block">
<p>不像中心化的版本控制系统(客户端与服务端很不一样),Git 仓库基本上都是一致的,并且并可以同步他们。
这使得拥有多个远端仓库变得容易 —— 你可以拥有一些只读的仓库,另外的一些也可写的仓库。
</p>
<p>
当你需要与远端仓库同步的时候,不需要使用它详细的链接。Git 储存了你感兴趣的远端仓库的链接的别名或者昵称。
你可以使用 <code>git remote</code> 命令管理这个远端仓库列表。
</p>
<h4>
git remote
<small>列出远端别名</small>
</h4>
<p>
如果没有任何参数,Git 会列出它存储的远端仓库别名了事。默认情况下,如果你的项目是克隆的(与本地创建一个新的相反),
Git 会自动将你的项目克隆自的仓库添加到列表中,并取名“origin”。
如果你执行时加上 <code>-v</code> 参数,你还可以看到每个别名的实际链接地址。
</p>
<pre>
<b>$ git remote</b>
origin
<b>$ git remote -v</b>
origin [email protected]:github/git-reference.git (fetch)
origin [email protected]:github/git-reference.git (push)
</pre>
<p>在此你看到了该链接两次,是因为 Git 允许你为每个远端仓库添加不同的推送与获取的链接,以备你读写时希望使用不同的协议。
</p>
<h4>
git remote add
<small>为你的项目添加一个新的远端仓库</small>
</h4>
<p>
如果你希望分享一个本地创建的仓库,或者你想要获取别人的仓库中的贡献 ——
如果你想要以任何方式与一个新仓库沟通,最简单的方式通常就是把它添加为一个远端仓库。
执行 <code>git remote add [alias] [url]</code> 就可以。
此命令将 <code>[url]</code> 以 <code>[alias]</code> 的别名添加为本地的远端仓库。
</p>
<p>
例如,假设我们想要与整个世界分享我们的 Hello World 程序。
我们可以在一台服务器上创建一个新仓库(我以 GitHub 为例子)。
它应该会给你一个链接,在这里就是“[email protected]:schacon/hw.git”。
要把它添加到我们的项目以便我们推送以及获取更新,我们可以这样:
</p>
<pre>
<b>$ git remote</b>
<b>$ git remote add github [email protected]:schacon/hw.git</b>
<b>$ git remote -v</b>
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
</pre>
<p>
像分支的命名一样,远端仓库的别名是强制的 —— 就像“master”,没有特别意义,但它广为使用,
因为 <code>git init</code> 默认用它;“origin”经常被用作远端仓库别名,就因为 <code>git clone</code>
默认用它作为克隆自的链接的别名。此例中,我决定给我的远端仓库取名“github”,但我叫它随便什么都可以。
</p>
<h4>
git remote rm
<small>删除现存的某个别名</small>
</h4>
<p>
Git addeth and Git taketh away. 如果你需要删除一个远端 —— 不再需要它了、项目已经没了,等等 —— 你可以使用 <code>git remote rm [alias]</code> 把它删掉。
</p>
<pre>
<b>$ git remote -v</b>
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
<b>$ git remote add origin git://github.com/pjhyett/hw.git</b>
<b>$ git remote -v</b>
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
origin git://github.com/pjhyett/hw.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
<b>$ git remote rm origin</b>
<b>$ git remote -v</b>
github [email protected]:schacon/hw.git (fetch)
github [email protected]:schacon/hw.git (push)
</pre>
<p class="nutshell">
<b>简而言之</b> 你可以用 <code>git remote</code> 列出你的远端仓库和那些仓库的链接。
你可以使用 <code>git remote add</code> 添加新的远端仓库,用 <code>git remote rm</code> 删掉已存在的那些。
</p>
</div>
</div>
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-fetch">文档</a>
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Fetching-and-Pulling-from-Your-Remotes">书</a>
</span>
<a name="fetch">git fetch</a>
<span class="desc">从远端仓库下载新分支与数据</span>
</h2>
<br/>
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-pull">文档</a>
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Fetching-and-Pulling-from-Your-Remotes">书</a>
</span>
<a name="pull">git pull</a>
<span class="desc">从远端仓库提取数据并尝试合并到当前分支</span>
</h2>
<div class="block">
<p>
Git 有两个命令用来从某一远端仓库更新。
<code>git fetch</code> 会使你与另一仓库同步,提取你本地所没有的数据,为你在同步时的该远端的每一分支提供书签。
这些分支被叫做“远端分支”,除了 Git 不允许你检出(切换到该分支)之外,跟本地分支没区别 ——
你可以将它们合并到当前分支,与其他分支作比较差异,查看那些分支的历史日志,等等。同步之后你就可以在本地操作这些。
</p>
<p>
第二个会从远端服务器提取新数据的命令是 <code>git pull</code>。
基本上,该命令就是在 <code>git fetch</code> 之后紧接着 <code>git merge</code> 远端分支到你所在的任意分支。
我个人不太喜欢这命令 —— 我更喜欢 <code>fetch</code> 和 <code>merge</code> 分开来做。少点魔法,少点问题。
不过,如果你喜欢这主意,你可以看一下 <code>git pull</code> 的
<a target="new" href="http://git-scm.com/docs/git-pull">官方文档</a>。
</p>
<p>
假设你配置好了一个远端,并且你想要提取更新,你可以首先执行 <code>git fetch [alias]</code>
告诉 Git 去获取它有你没有的数据,然后你可以执行 <code>git merge [alias]/[branch]</code>
以将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支。
那么,如果我是与两三个其他人合作 Hello World 项目,并且想要将我最近连接之后的所有改动拿过来,我可以这么做:
</p>
<pre>
<b>$ git fetch github</b>
remote: Counting objects: 4006, done.
remote: Compressing objects: 100% (1322/1322), done.
remote: Total 2783 (delta 1526), reused 2587 (delta 1387)
Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done.
Resolving deltas: 100% (1526/1526), completed with 387 local objects.
From github.com:schacon/hw
8e29b09..c7c5a10 master -> github/master
0709fdc..d4ccf73 c-langs -> github/c-langs
6684f82..ae06d2b java -> github/java
* [new branch] ada -> github/ada
* [new branch] lisp -> github/lisp
</pre>
<p>
可以看到自从上一次与远端仓库同步以后,又新赠或更新了五个分支。
“ada”与“lisp”分支是新的,而“master”、“clang”与“java”分支则被更新了。
在此例中,我的团队在合并入主分支之前,将提议的更新推送到远端分支以审核。
</p>
<p>
你可以看到 Git 做的映射。远端仓库的主分支成为了本地的一个叫做“github/master”的分支。
这样我就可以执行 <code>git merge github/master</code> 将远端的主分支和并入我的本地主分支。
或者,我可以 <code>git log github/master ^master</code> 看看该分支上的新提交。
如果你的远端仓库叫做“origin”,那远端主分支就会叫做 <code>origin/master</code>。几乎所有能在本地分支上执行的命令都可以在远端分支上用。
</p>
<p>
如果你有多个远端仓库,你可以执行 <code>git fetch [alias]</code> 提取特定的远端仓库,
或者执行 <code>git fetch --all</code> 告诉 Git 同步所有的远端仓库。
</p>
<p class="nutshell">
<b>简而言之</b> 执行 <code>git fetch [alias]</code> 来将你的仓库与远端仓库同步,提取所有它独有的数据到本地分支以合并或者怎样。
</p>
</div>
</div>
<div class="box">
<h2>
<span class="docs">
<a target="new" href="http://git-scm.com/docs/git-push">文档</a>
<a target="new" href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Pushing-to-Your-Remotes">书</a>
</span>
<a name="push">git push</a>
<span class="desc">推送你的新分支与数据到某个远端仓库</span>
</h2>
<div class="block">
<p>
想要与他人分享你牛鼻的提交,你需要将改动推送到远端仓库。
执行 <code>git push [alias] [branch]</code>,就会将你的 [branch] 分支推送成为 [alias] 远端上的 [branch] 分支。
让我们试试推送我们的主分支到先前添加的“github”远端仓库上去。
</p>
<pre>
<b>$ git push github master</b>
Counting objects: 25, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 2.43 KiB, done.
Total 25 (delta 4), reused 0 (delta 0)
To [email protected]:schacon/hw.git
* [new branch] master -> master
</pre>
<p>
挺简单。现在如果有人从该仓库克隆,他会得到我提交的完完全全的一份历史记录了。
</p>
<p>
如果有个像之前创建的“erlang”分支那样的主题分支,想只分享这个,该怎么办呢?你可以相应的只推送该分支。
</p>
<pre>
<b>$ git push github erlang</b>
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 652 bytes, done.
Total 6 (delta 1), reused 0 (delta 0)
To [email protected]:schacon/hw.git
* [new branch] erlang -> erlang
</pre>
<p>
现在当人们从该仓库克隆时,他们就会得到一个“erlang”分支以查阅、合并。
用这种方式,你可以推送任何分支到任何你有写权限的仓库。
如果你的分支已经在该仓库中了,它会试着去更新,如果它不再,Git 会把它加上。
</p>
<p>
最后一个当你推送到远端分支时会碰到的主要问题是,其他人在此期间也推送了的情况。
如果你和另一个开发者同时克隆了,又都有提交,那么当她推送后你也想推送时,默认情况下 Git 不会让你覆盖她的改动。
相反的,它会在你试图推送的分支上执行 <code>git log</code>,确定它能够在你的推送分支的历史记录中看到服务器分支的当前进度。
如果它在在你的历史记录中看不到,它就会下结论说你过时了,并打回你的推送。
你需要正式提取、合并,然后再次推送 —— 以确定你把她的改动也考虑在内了。
</p>
<p>
当你试图推送到某个以被更新的远端分支时,会出现下面这种情况:
</p>
<pre>
<b>$ git push github master</b>
To [email protected]:schacon/hw.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:schacon/hw.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
</pre>
<p>
你可以修正这个问题。执行 <code>git fetch github; git merge github/master</code>,然后再推送
</p>
<p class="nutshell">
<b>简而言之</b> 执行 <code>git push [alias] [branch]</code> 将你的本地改动推送到远端仓库。
如果可以的话,它会依据你的 [branch] 的样子,推送到远端的 [branch] 去。
如果在你上次提取、合并之后,另有人推送了,Git 服务器会拒绝你的推送,知道你是最新的为止。
</p>
</div>
</div>
<p><a href="{{ site.baseurl }}/zh/inspect">到 检查与比较»</a></p>