Repository navigation
Expand file tree
/
Copy pathGit.html
More file actions
308 lines (287 loc) · 19.3 KB
/
Copy pathGit.html
File metadata and controls
308 lines (287 loc) · 19.3 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Intro to Git — PythonCert 4.0 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Schedule Lightning talks" href="lightning_talks.html" />
<link rel="prev" title="Introductions" href="class_introduction.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="intro-to-git">
<span id="git"></span><h1>Intro to Git<a class="headerlink" href="#intro-to-git" title="Permalink to this headline">¶</a></h1>
<div class="section" id="what-is-git">
<h2>What is Git?<a class="headerlink" href="#what-is-git" title="Permalink to this headline">¶</a></h2>
<p>A “version control system”</p>
</div>
<div class="section" id="why-version-control">
<h2>Why Version Control?<a class="headerlink" href="#why-version-control" title="Permalink to this headline">¶</a></h2>
<div class="figure" id="id5">
<img alt="../_images/phd101212s.gif" src="../_images/phd101212s.gif" />
<p class="caption"><span class="caption-text">“Piled Higher and Deeper” by Jorge Cham: www.phdcomics.com</span></p>
</div>
<p>A history of everything everyone does to ‘your’ code</p>
<p>A graph of “states” in which the code has existed</p>
<p>That last one is a bit tricky, and is not necessary to understand right out of the gate. When you are ready, you can look at this supplement to gain a better understanding:</p>
<p><a class="reference internal" href="../supplemental/dev_environment/git_overview.html#git-overview"><span class="std std-ref">git Overview</span></a></p>
<p>There other versioning systems, such as Mercurial and Subversion (and commercial offerings), but Git is the most popular.</p>
<p>It is incredibly important to learn and understand versioning control to work as a developer today, so we will be incorporating Git into our work flow.</p>
</div>
<div class="section" id="setting-up-git">
<h2>Setting up Git<a class="headerlink" href="#setting-up-git" title="Permalink to this headline">¶</a></h2>
<p>You should have git installed on your machine and accessible from the command line. There will be a little bit of setup for git that you should only have to do once.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git config --global user.name <span class="s2">"Marie Curie"</span>
$ git config --global user.email <span class="s2">"marie@radioactive.com"</span>
</pre></div>
</div>
</div>
<div class="section" id="editor">
<h2>Editor<a class="headerlink" href="#editor" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>git needs an editor occasionally</li>
<li>default is VI, which is not very intuitive to non-Unix Geeks</li>
<li>Nano is simple, easy solution for Macs and Linux</li>
<li>Nano no longer available for windows, use Sublime or Notepad++</li>
</ul>
<dl class="docutils">
<dt>For windows users:</dt>
<dd><a class="reference external" href="https://uwpce-pythoncert.github.io/PythonCertDevel/supplemental/installing/git_editor_windows.html">https://uwpce-pythoncert.github.io/PythonCertDevel/supplemental/installing/git_editor_windows.html</a></dd>
</dl>
<p>Once you have chosen/installed an editor, configure git to use it:</p>
<p>nano
<code class="docutils literal"><span class="pre">$</span> <span class="pre">git</span> <span class="pre">config</span> <span class="pre">--global</span> <span class="pre">core.editor</span> <span class="pre">"nano</span> <span class="pre">-w"</span></code></p>
<p>sublime (mac)
<code class="docutils literal"><span class="pre">$</span> <span class="pre">git</span> <span class="pre">config</span> <span class="pre">--global</span> <span class="pre">core.editor</span> <span class="pre">"subl</span> <span class="pre">-n</span> <span class="pre">-w"</span></code></p>
<p>sublime (win)
<code class="docutils literal"><span class="pre">$</span> <span class="pre">git</span> <span class="pre">config</span> <span class="pre">--global</span> <span class="pre">core.editor</span> <span class="pre">"'c:/program</span> <span class="pre">files/sublime</span> <span class="pre">text</span> <span class="pre">2/sublime_text.exe'</span> <span class="pre">-w"</span></code></p>
<p>Notepad++ (Win)
<code class="docutils literal"><span class="pre">$</span> <span class="pre">git</span> <span class="pre">config</span> <span class="pre">--global</span> <span class="pre">core.editor</span> <span class="pre">"'c:/program</span> <span class="pre">files</span> <span class="pre">(x86)/Notepad++/notepad++.exe'</span> <span class="pre">-multiInst</span> <span class="pre">-notabbar</span> <span class="pre">-nosession</span> <span class="pre">-noPlugin"</span></code></p>
</div>
<div class="section" id="repositories">
<h2>Repositories<a class="headerlink" href="#repositories" title="Permalink to this headline">¶</a></h2>
<p>A repository is just a collection of files that ‘belong together’.</p>
<p>Since <code class="docutils literal"><span class="pre">git</span></code> is a <em>distributed</em> versioning system, there is no <strong>central</strong>
repository that serves as the one to rule them all. This simply means that all repositories should look the same.</p>
<p>However, to keep things sane, there is generally one “central” repository chosen that users check with for changes, for us this is GitHub.</p>
</div>
<div class="section" id="working-with-remotes">
<h2>Working with Remotes<a class="headerlink" href="#working-with-remotes" title="Permalink to this headline">¶</a></h2>
<p>With git, you work with <em>local</em> repositories and <em>remotes</em> that they are connected to.</p>
<div class="build docutils container">
<p>Git uses shortcuts to address <em>remotes</em>. Cloned repositories get an <em>origin</em> shortcut for free:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git remote -v
origin https://github.com/UWPCE-PythonCert/IntroPython-2017.git <span class="o">(</span>fetch<span class="o">)</span>
origin https://github.com/UWPCE-PythonCert/IntroPython-2017.git <span class="o">(</span>push<span class="o">)</span>
</pre></div>
</div>
<p>This shows that the local repo on my machine <em>originated</em> from the one in
the UWPCE-PythonCert gitHub account (it shows up twice, because there is
a shortcut for both fetch from and push to this remote)</p>
</div>
<div class="build docutils container">
<p>You can work on any project you wish to that has a public repository on Github. However, since you won’t have permission to edit most projects directly, there is such a thing as <em>forking</em> a project.</p>
<p>When you <em>fork</em> a repository, you make a copy of that repository in your own (Github) account.</p>
<p>When you have made changes that you believe the rest of the community will want to adopt, you make a <em>pull request</em> to the original project. The maintainer(s) of that project than have the option of accepting your changes, in which case your changes will become part of that project.</p>
<p>This is how we will be working in this class. When you want feedback on your work, you will make a <em>pull request</em> to the instructors.</p>
</div>
<p>Our class materials reside in a repository on <em>Github</em> in the <em>UWPCE-PythonCert</em> organization:</p>
<div class="figure">
<a class="center reference internal image-reference" href="../_images/remotes_start.png"><img alt="../_images/remotes_start.png" class="center" src="../_images/remotes_start.png" style="width: 50%;" /></a>
</div>
<p>Note that we will be using a different repository for class assignments than the repository for the class materials (this repository).</p>
<p>It will be a repository that is created just for this class, and will be called IntroPython-<em>quarter</em>.</p>
<p>In examples below it is called IntroToPython, so replace that in your head with the name of this year’s repository. :)</p>
<p>This new repository will include examples and we will add relevant materials (and exercise solutions) to it throughout the quarter.</p>
<p>There will be a folder called students at the top level, and everyone will create their own directory within it.</p>
<p>So, everyone will commit to this repository, and everyone will have access to everyone’s code.</p>
<p>This will make it easier to collaborate. Weirdly enough, collaborating is important for developing code, both for class and in the <em>real world</em>.</p>
<p>We will do a live demo of setting up a machine for working with this repository now.</p>
<p>The first thing we have to do is on the Github website. We will create a fork of the class repository from the <code class="docutils literal"><span class="pre">UWPCE-PythonCert</span></code> account on GitHub into your personal account. Please create a gitHub account if you don’t have one already.</p>
<p>Note: You do not have to use your real name to set up your git account.</p>
<div class="figure">
<a class="center reference internal image-reference" href="../_images/remotes_fork.png"><img alt="../_images/remotes_fork.png" class="center" src="../_images/remotes_fork.png" style="width: 50%;" /></a>
</div>
<p>Everyone should now have a copy of the class repository in their account on the GitHub website.</p>
<p>The next step is to make a <em>clone</em> of your fork on your own computer, which means that <strong>your fork</strong> in github is the <em>origin</em> (Demo):</p>
<div class="figure">
<a class="center reference internal image-reference" href="../_images/remotes_clone.png"><img alt="../_images/remotes_clone.png" class="center" src="../_images/remotes_clone.png" style="width: 50%;" /></a>
</div>
<p>Since you are working on a repository that you do not own, you will need to make a git shortcut to the
original repository, so that you can get changes made by other contributors before you start working.</p>
<p>You can add <em>remotes</em> at will, to connect your <em>local</em> repository or to other
copies of it in different remote locations.</p>
<div class="build docutils container">
<p>This allows you to grab changes made to the repository in these other
locations.</p>
<p>For our class, we will add an <em>upstream</em> remote to our local copy that points to the original copy of the material in the <code class="docutils literal"><span class="pre">UWPCE-PythonCert</span></code> account, and we will call it, appropriately, “upstream”</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git remote add upstream https://github.com/UWPCE-PythonCert/IntroPython2015.git
$ git remote -v
origin https://github.com/PythonCHB/IntroPython2015.git <span class="o">(</span>fetch<span class="o">)</span>
origin https://github.com/PythonCHB/IntroPython2015.git <span class="o">(</span>push<span class="o">)</span>
upstream https://github.com/UWPCE-PythonCert/IntroPython2015.git <span class="o">(</span>fetch<span class="o">)</span>
upstream https://github.com/UWPCE-PythonCert/IntroPython2015.git <span class="o">(</span>push<span class="o">)</span>
</pre></div>
</div>
</div>
<p>This should leave you in a situation that looks like this:</p>
<div class="figure">
<a class="center reference internal image-reference" href="../_images/remotes_upstream.png"><img alt="../_images/remotes_upstream.png" class="center" src="../_images/remotes_upstream.png" style="width: 50%;" /></a>
</div>
<p>To get the updates from your new remote, you’ll need first to fetch everything:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git fetch --all
Fetching origin
Fetching upstream
...
</pre></div>
</div>
<p>Then you can see the branches you have locally available:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/upstream/master
</pre></div>
</div>
<p>Finally, you can fetch and then merge changes from the upstream master.</p>
<p>Start by making sure you are on your own master branch:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git checkout master
</pre></div>
</div>
<p>This is <strong>really really</strong> important. Take the time to ensure you are where you think you are, iow, that your origin is your own github repository and that you are working on master from that remote.
You can use <a href="#id1"><span class="problematic" id="id2">:bash:`git remote -v`</span></a> and <a href="#id3"><span class="problematic" id="id4">:bash:`git branch -a`</span></a> to verify.</p>
<p>Now, fetch the upstream master branch and merge it into your master.
You can do this in one step with:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git pull upstream master
Updating 3239de7..9ddbdbb
Fast-forward
Examples/README.rst <span class="p">|</span> <span class="m">4</span> ++++
...
create mode <span class="m">100644</span> Examples/README.rst
...
</pre></div>
</div>
<p>Now all the changes from <em>upstream</em> are present in your local clone.
You should do this pull everytime you start to work on code.</p>
<p>In order to preserve the changes made by others in your fork on GitHub, you’ll have to push:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ git status
On branch master
Your branch is ahead of <span class="s1">'origin/master'</span> by <span class="m">10</span> commits.
<span class="o">(</span>use <span class="s2">"git push"</span> to publish your <span class="nb">local</span> commits<span class="o">)</span>
$ git push origin master
Counting objects: <span class="m">44</span>, <span class="k">done</span>.
...
$
</pre></div>
</div>
<p>(A simple <code class="docutils literal"><span class="pre">git</span> <span class="pre">push</span></code> will usually do the right thing)</p>
<p>You are now set up to work with this repository, and the next steps will be similar every time you work on code.</p>
<p><a class="reference internal" href="GitWorkflow.html#git-workflow"><span class="std std-ref">Git Workflow</span></a></p>
<p>Additional Notes:</p>
<p>Because of the way we have set up the class, you will be able
to see all work submitted to us from everyone in the class in
the students directory on your machine. This is not a bad thing.
And the files tend to be small.</p>
<p>We encourage sharing of knowledge in this class. Helping your
fellow students will also help you to better understand. Share
your code, and get use to giving/receiving feedback on how to
improve your code, if you are not already.</p>
<p>Each repository will have a directory called <code class="docutils literal"><span class="pre">.git</span></code> that is normally
not seen. This directory is how git keeps track of everything. Leave it alone. :)</p>
<p>Please do not set up a git repository inside another git repository, this can lead to heartache.</p>
<p>Absolutely, do NOT set up a git repository in your home root directory.
This will put everything in your home directory up on GitHub, and you do not want that.</p>
<p>Setting up new repositories can be confusing because when you clone a git repository it creates
the directory that will be the repository, but when you are creating a new
repository, you need to first be <strong>IN</strong> the directory in which you want the
repository to be rooted. Please ask if this does not make sense.</p>
<p>Additional Resources:</p>
<p>git tutorial:
<a class="reference external" href="https://try.github.io/levels/1/challenges/1">https://try.github.io/levels/1/challenges/1</a></p>
<p>basic git commands:
<a class="reference external" href="https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html">https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html</a></p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Intro to Git</a><ul>
<li><a class="reference internal" href="#what-is-git">What is Git?</a></li>
<li><a class="reference internal" href="#why-version-control">Why Version Control?</a></li>
<li><a class="reference internal" href="#setting-up-git">Setting up Git</a></li>
<li><a class="reference internal" href="#editor">Editor</a></li>
<li><a class="reference internal" href="#repositories">Repositories</a></li>
<li><a class="reference internal" href="#working-with-remotes">Working with Remotes</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="../class_schedule/session_1_01.html">Intro to Python Session 1</a><ul>
<li>Previous: <a href="class_introduction.html" title="previous chapter">Introductions</a></li>
<li>Next: <a href="lightning_talks.html" title="next chapter">Schedule Lightning talks</a></li>
</ul></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/Git.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2017, Christopher Barker, Cris Ewing, Christy Heaton, Maria McKinley, Rick Riehle, Joseph Schilz.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.2</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
|
<a href="../_sources/modules/Git.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>