forked from swcarpentry/git-novice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-collab.html
192 lines (192 loc) · 13.5 KB
/
06-collab.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Version Control with Git</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="title">Version Control with Git</h1>
<h2 class="subtitle">Collaborating</h2>
<div id="learning-objectives" class="objectives">
<h2>Learning Objectives</h2>
<ul>
<li>Explain what remote repositories are and why they are useful.</li>
<li>Explain what happens when a remote repository is cloned.</li>
<li>Explain what happens when changes are pushed to or pulled from a remote repository.</li>
</ul>
</div>
<p>Version control really comes into its own when we begin to collaborate with other people. We already have most of the machinery we need to do this; the only thing missing is to copy changes from one repository to another.</p>
<p>Systems like Git allow us to move work between any two repositories. In practice, though, it's easiest to use one copy as a central hub, and to keep it on the web rather than on someone's laptop. Most programmers use hosting services like <a href="http://github.com">GitHub</a> or <a href="http://bitbucket.org">BitBucket</a> to hold those master copies; we'll explore the pros and cons of this in the final section of this lesson.</p>
<p>Let's start by sharing the changes we've made to our current project with the world. Log in to GitHub, then click on the icon in the top right corner to create a new repository called <code>planets</code>:</p>
<div class="figure">
<img src="fig/github-create-repo-01.png" alt="Creating a Repository on GitHub (Step 1)" />
<p class="caption">Creating a Repository on GitHub (Step 1)</p>
</div>
<p>Name your repository "planets" and then click "Create Repository":</p>
<div class="figure">
<img src="fig/github-create-repo-02.png" alt="Creating a Repository on GitHub (Step 2)" />
<p class="caption">Creating a Repository on GitHub (Step 2)</p>
</div>
<p>As soon as the repository is created, GitHub displays a page with a URL and some information on how to configure your local repository:</p>
<div class="figure">
<img src="fig/github-create-repo-03.png" alt="Creating a Repository on GitHub (Step 3)" />
<p class="caption">Creating a Repository on GitHub (Step 3)</p>
</div>
<p>This effectively does the following on GitHub's servers:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mkdir</span> planets
$ <span class="kw">cd</span> planets
$ <span class="kw">git</span> init</code></pre>
<p>Our local repository still contains our earlier work on <code>mars.txt</code>, but the remote repository on GitHub doesn't contain any files yet:</p>
<div class="figure">
<img src="fig/git-freshly-made-github-repo.svg" alt="Freshly-Made GitHub Repository" />
<p class="caption">Freshly-Made GitHub Repository</p>
</div>
<p>The next step is to connect the two repositories. We do this by making the GitHub repository a <a href="reference.html#remote">remote</a> for the local repository. The home page of the repository on GitHub includes the string we need to identify it:</p>
<div class="figure">
<img src="fig/github-find-repo-string.png" alt="Where to Find Repository URL on GitHub" />
<p class="caption">Where to Find Repository URL on GitHub</p>
</div>
<p>Click on the 'HTTPS' link to change the <a href="reference.html#protocol">protocol</a> from SSH to HTTPS.</p>
<div id="https-vs-ssh" class="callout">
<h2>HTTPS vs SSH</h2>
<p>We use HTTPS here because it does not require additional configuration. After the workshop you may want to set up SSH access, which is a bit more secure, by following one of the great tutorials from <a href="https://help.github.com/articles/generating-ssh-keys">GitHub</a>, <a href="https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git">Atlassian/BitBucket</a> and <a href="https://about.gitlab.com/2014/03/04/add-ssh-key-screencast/">GitLab</a> (this one has a screencast).</p>
</div>
<div class="figure">
<img src="fig/github-change-repo-string.png" alt="Changing the Repository URL on GitHub" />
<p class="caption">Changing the Repository URL on GitHub</p>
</div>
<p>Copy that URL from the browser, go into the local <code>planets</code> repository, and run this command:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> remote add origin https://github.com/vlad/planets</code></pre>
<p>Make sure to use the URL for your repository rather than Vlad's: the only difference should be your username instead of <code>vlad</code>.</p>
<p>We can check that the command has worked by running <code>git remote -v</code>:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> remote -v</code></pre>
<pre class="output"><code>origin https://github.com/vlad/planets.git (push)
origin https://github.com/vlad/planets.git (fetch)</code></pre>
<p>The name <code>origin</code> is a local nickname for your remote repository: we could use something else if we wanted to, but <code>origin</code> is by far the most common choice.</p>
<p>Once the nickname <code>origin</code> is set up, this command will push the changes from our local repository to the repository on GitHub:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> push origin master</code></pre>
<pre class="output"><code>Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 821 bytes, done.
Total 9 (delta 2), reused 0 (delta 0)
To https://github.com/vlad/planets
* [new branch] master -> master
Branch master set up to track remote branch master from origin.</code></pre>
<div id="proxy" class="callout">
<h2>Proxy</h2>
<p>If the network you are connected to uses a proxy there is an chance that your last command failed with "Could not resolve hostname" as the error message. To solve this issue you need to tell Git about the proxy:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global http.proxy http://user:[email protected]
$ <span class="kw">git</span> config --global https.proxy http://user:[email protected]</code></pre>
<p>When you connect to another network that doesn't use a proxy you will need to tell Git to disable the proxy using</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global --unset http.proxy
$ <span class="kw">git</span> config --global --unset https.proxy</code></pre>
</div>
<div id="password-managers" class="callout">
<h2>Password Managers</h2>
<p>If your operating system has a password manager configured, <code>git push</code> will try to use it when it needs your username and password. If you want to type your username and password at the terminal instead of using a password manager, type</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">unset</span> <span class="ot">SSH_ASKPASS</span></code></pre>
<p>You may want to add this command at the end of your <code>~/.bashrc</code> to make it the default behavior.</p>
</div>
<p>Our local and remote repositories are now in this state:</p>
<div class="figure">
<img src="fig/github-repo-after-first-push.svg" alt="GitHub Repository After First Push" />
<p class="caption">GitHub Repository After First Push</p>
</div>
<div id="the--u-flag" class="callout">
<h2>The '-u' Flag</h2>
<p>You may see a <code>-u</code> option used with <code>git push</code> in some documentation. It is related to concepts we cover in our intermediate lesson, and can safely be ignored for now.</p>
</div>
<p>We can pull changes from the remote repository to the local one as well:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> pull origin master</code></pre>
<pre class="output"><code>From https://github.com/vlad/planets
* branch master -> FETCH_HEAD
Already up-to-date.</code></pre>
<p>Pulling has no effect in this case because the two repositories are already synchronized. If someone else had pushed some changes to the repository on GitHub, though, this command would download them to our local repository.</p>
<p>For the next step, get into pairs. Pick one of your repositories on GitHub to use for collaboration.</p>
<div id="practicing-by-yourself" class="callout">
<h2>Practicing by yourself</h2>
<p>If you're working through this lesson on your own, you can carry on by opening a second terminal window, and switching to another directory (e.g. <code>/tmp</code>). This window will represent your partner, working on another computer. You won't need to give anyone access on GitHub, because both 'partners' are you.</p>
</div>
<p>The partner whose repository is being used needs to give the other person access. On GitHub, click the settings button on the right, then select Collaborators, and enter your partner's username.</p>
<div class="figure">
<img src="fig/github-add-collaborators.png" alt="Adding collaborators on GitHub" />
<p class="caption">Adding collaborators on GitHub</p>
</div>
<p>The other partner should <code>cd</code> to another directory (so <code>ls</code> doesn't show a <code>planets</code> folder), and then make a copy of this repository on your own computer:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> clone https://github.com/vlad/planets.git</code></pre>
<p>Replace 'vlad' with your partner's username (the one who owns the repository).</p>
<p><code>git clone</code> creates a fresh local copy of a remote repository.</p>
<div class="figure">
<img src="fig/github-collaboration.svg" alt="After Creating Clone of Repository" />
<p class="caption">After Creating Clone of Repository</p>
</div>
<p>The new collaborator can now make a change in their copy of the repository:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cd</span> planets
$ <span class="kw">nano</span> pluto.txt
$ <span class="kw">cat</span> pluto.txt</code></pre>
<pre class="output"><code>It is so a planet!</code></pre>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> add pluto.txt
$ <span class="kw">git</span> commit -m <span class="st">"Some notes about Pluto"</span></code></pre>
<pre class="output"><code> 1 file changed, 1 insertion(+)
create mode 100644 pluto.txt</code></pre>
<p>then push the change to GitHub:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> push origin master</code></pre>
<pre class="output"><code>Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/vlad/planets.git
9272da5..29aba7c master -> master</code></pre>
<p>Note that we didn't have to create a remote called <code>origin</code>: Git does this automatically, using that name, when we clone a repository. (This is why <code>origin</code> was a sensible choice earlier when we were setting up remotes by hand.)</p>
<p>We can now download changes into the original repository on our machine:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> pull origin master</code></pre>
<pre class="output"><code>remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/vlad/planets
* branch master -> FETCH_HEAD
Updating 9272da5..29aba7c
Fast-forward
pluto.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 pluto.txt</code></pre>
<div id="github-timestamp" class="challenge">
<h2>GitHub Timestamp</h2>
<p>Create a repository on GitHub, clone it, add a file, push those changes to GitHub, and then look at the <a href="reference.html#timestamp">timestamp</a> of the change on GitHub. How does GitHub record times, and why?</p>
</div>
</div>
</div>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/git-novice">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>