forked from swcarpentry/git-novice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussion.html
130 lines (130 loc) · 11 KB
/
discussion.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
<!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" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/bootstrap/bootstrap-responsive.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>
<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-fluid">
<div class="span10 offset1">
<h1 class="title">Version Control with Git</h1>
<h2 class="subtitle">Discussion</h2>
<h2 id="frequently-asked-questions">Frequently Asked Questions</h2>
<p>People often have questions about Git beyond the scope of the core material. Students who have completed the rest of the lessons might find value in looking through the following topics.</p>
<p>Note that since this material isn't essential for basic Git usage, it won't be covered by the instructor.</p>
<h3 id="more-advanced-git-configuration">More Advanced Git Configuration</h3>
<p>In <a href="01-backup.md">A Better Kind of Backup</a>, we used <code>git config --global</code> to set some default options for Git. It turns out that these configuration options get stored in your home directory in a plain text file called <code>.gitconfig</code>.</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">cat</span> ~/.gitconfig</code></pre>
<pre class="output"><code>[user]
name = Vlad Dracula
email = vlad@tran.sylvan.ia
[color]
ui = true
[core]
editor = nano</code></pre>
<p>This file can be opened in your preferred text editor. (Note that it is recommended to continue using the <code>git config</code> command, as this helps avoid introducing syntax errors.)</p>
<p>Eventually, you will want to start customizing Git's behaviour. This can be done by adding more entries to your <code>.gitconfig</code>. The available options are described in the manual:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --help</code></pre>
<p>In particular, you might find it useful to add aliases. These are like shortcuts for longer git commands. For example, if you get sick of typing <code>git checkout</code> all the time, you could run the command:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global alias.co checkout</code></pre>
<p>Now if we return to the example from <a href="01-backup.md">A Better Kind of Backup</a> where we ran:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> checkout f22b25e mars.txt</code></pre>
<p>we could now instead type:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> co f22b25e mars.txt</code></pre>
<div id="styling-gits-log" class="challenge">
<h2>Styling Git's Log</h2>
<p>A good target for customization is output from the log. The default log is quite verbose but gives no graphical hints such as information about which commits were done locally and which were pulled from remotes.</p>
<p>Use <code>git log --help</code> and <code>git config --help</code> to look for different ways to change the log output.</p>
<p>How do you expect the log to look after running the following commands?</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global alias.lg <span class="st">"log --graph"</span>
$ <span class="kw">git</span> config --global log.abbrevCommit true
$ <span class="kw">git</span> config --global format.pretty oneline
$ <span class="kw">git</span> lg</code></pre>
<p>You can try the commands out and if you decide you don't like the effect, you can get rid of them by running:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> config --global --unset alias.lg
$ <span class="kw">git</span> config --global --unset log.abbrevCommit
$ <span class="kw">git</span> config --global --unset format.pretty</code></pre>
</div>
<div id="version-controlling-the-gitconfig" class="challenge">
<h2>Version Controlling the Gitconfig</h2>
<p>In the previous challenge, we suggested using the <code>--unset</code> flag to delete unwanted options from <code>.gitconfig</code>. Another way to roll back changes is to version control your <code>.gitconfig</code> using Git.</p>
<p>Go to GitHub and do a search for "gitconfig". You will hopefully see hundreds of repositories in which people have stored their own Git configuration files. Sort them by most number of stars and have a look at the top few. Do you see any ideas for things you might want to try out in your own <code>.gitconfig</code>?</p>
<p>(Remember to check that they're using an open source license before you clone their work for your own use.)</p>
</div>
<h3 id="non-text-files">Non-text Files</h3>
<p>Recall when we discussed <a href="03-conflicts.html">Conflicts</a> there was a challenge that asked:</p>
<blockquote>
<p>What does Git do when there is a conflict in an image or some other non-textual file that is stored in version control?</p>
</blockquote>
<p>We will now revisit this in more detail.</p>
<p>Many people want to version control non-text files, such as images, PDFs and Microsoft Office or LibreOffice documents. It is true that Git can handle these filetypes (which fall under the banner of "binary" file types). However, just because it <em>can</em> be done doesn't mean it <em>should</em> be done.</p>
<p>Much of Git's magic comes from being able to do line-by-line comparisons ("diffs") between files. This is generally easy for programming source code and marked up text. For non-text files, a diff can usually only detect that the files have changed but can't say how or where.</p>
<p>This has various impacts on Git's performance and will make it difficult to compare different versions of your project.</p>
<p>For a basic example to show the difference it makes, we're going to go see what would have happened if Dracula had tried using outputs from a word processor instead of plain text.</p>
<p>Create a new directory and go into it:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mkdir</span> planets-nontext
$ <span class="kw">cd</span> planets-nontext</code></pre>
<p>Use a program such as Microsoft Word or LibreOffice Writer to create a new document. Enter the same text that we began with before:</p>
<pre class="output"><code>Cold and dry, but everything is my favorite color</code></pre>
<p>Save the document into the <code>planets-nontext</code> directory with the name of <code>mars.doc</code>. Back in the terminal, run the usual commands for setting up a new Git repository:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> init
$ <span class="kw">git</span> add mars.doc
$ <span class="kw">git</span> commit -m <span class="st">"Starting to think about Mars"</span></code></pre>
<p>Then make the same changes to <code>mars.doc</code> that we (or Vlad) previously made to <code>mars.txt</code>.</p>
<pre class="output"><code>Cold and dry, but everything is my favorite color
The two moons may be a problem for Wolfman</code></pre>
<p>Save and close the word processor. Now see what Git thinks of your changes:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">git</span> diff</code></pre>
<pre class="output"><code>diff --git a/mars.doc b/mars.doc
index 53a66fd..6e988e9 100644
Binary files a/mars.doc and b/mars.doc differ</code></pre>
<p>Compare this to the earlier <code>git diff</code> obtained when using text files:</p>
<pre class="output"><code>diff --git a/mars.txt b/mars.txt
index df0654a..315bf3a 100644
--- a/mars.txt
+++ b/mars.txt
@@ -1 +1,2 @@
Cold and dry, but everything is my favorite color
+The two moons may be a problem for Wolfman</code></pre>
<p>Notice how plain text files give a much more informative diff. You can see exactly which lines changed and what the changes were.</p>
<p>An uninformative <code>git diff</code> is not the only consequence of using Git on binary files. However, most of the other problems boil down to whether or not a good diff is possible.</p>
<p>This isn't to say you should <em>never</em> use Git on binary files. A rule of thumb is that it's okay if the binary file won't change very often, and if it does change, you don't care about merging in small differences between versions.</p>
<p>We've already seen how a word processed report will fail this test. An example that passes the test is a logo for your organization or project. Even though a logo will be stored in a binary format such as <code>jpg</code> or <code>png</code>, you can expect it will remain fairly static through the lifetime of your repository. On the rare occasion that branding does change, you will probably just want to replace the logo completely rather than merge little differences in.</p>
<div id="non-text-version-control" class="challenge">
<h2>Non-text Version Control</h2>
<p>How would you write a program to diff non-text files?</p>
<p>If you go through two files byte by byte, it might be easy to find the first place where differences begin. However, what if two stretches in the middle of a file are exactly the same but have different start locations? Or if a stretch of bytes in one file is identical to two different stretches in the other file, which should be privileged as <em>the</em> matching part?</p>
<p>Try Googling for phrases such as <code>non-text diff</code> and <code>binary version control</code> to see what ideas and solutions other people have come up with. (You might also see some more arguments about why you shouldn't use binary files with Git.)</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:admin@software-carpentry.org">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="http://software-carpentry.org/v5/js/bootstrap/bootstrap.min.js"></script>
</body>
</html>