epubjs
Version:
Render ePub documents in the browser, across many devices
410 lines (317 loc) • 24.3 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pro Git - professional version control</title>
<meta content="http://www.w3.org/1999/xhtml; charset=utf-8" http-equiv="Content-Type"/>
<link href="stylesheet.css" type="text/css" rel="stylesheet"/>
<style type="text/css">
@page { margin-bottom: 5.000000pt; margin-top: 5.000000pt; }</style>
</head>
<body class="calibre">
<h2 class="calibre4" id="calibre_pb_12">Recording Changes to the Repository</h2>
<p class="calibre3">You have a bona fide Git repository and a checkout or working copy of the files for that project. You need to make some changes and commit snapshots of those changes into your repository each time the project reaches a state you want to record.</p>
<p class="calibre3">Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else - any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and haven't edited anything. </p>
<p class="calibre3">As you edit files, Git sees them as modified, because you've changed them since your last commit. You stage these modified files and then commit all your staged changes, and the cycle repeats. This lifecycle is illustrated in Figure 2-1.</p>
<p class="calibre3"><img src="18333fig0201-tn.png" alt="Figure 2-1. The lifecycle of the status of your files." title="Figure 2-1. The lifecycle of the status of your files." class="calibre6"/></p>
<h3 class="calibre5">Checking the Status of Your Files</h3>
<p class="calibre3">The main tool you use to determine which files are in which state is the git status command. If you run this command directly after a clone, you should see something like this:</p>
<pre class="calibre9"><code class="calibre10">$ git status
# On branch master
nothing to commit (working directory clean)
</code></pre>
<p class="calibre3">This means you have a clean working directory-in other words, there are no tracked and modified files. Git also doesn't see any untracked files, or they would be listed here. Finally, the command tells you which branch you're on. For now, that is always master, which is the default; you won't worry about it here. The next chapter will go over branches and references in detail.</p>
<p class="calibre3">Let's say you add a new file to your project, a simple README file. If the file didn't exist before, and you run <code class="calibre10">git status</code>, you see your untracked file like so:</p>
<pre class="calibre9"><code class="calibre10">$ vim README
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# README
nothing added to commit but untracked files present (use "git add" to track)
</code></pre>
<p class="calibre3">You can see that your new README file is untracked, because it's under the "Untracked files" heading in your status output. Untracked basically means that Git sees a file you didn't have in the previous snapshot (commit); Git won't start including it in your commit snapshots until you explicitly tell it to do so. It does this so you don't accidentally begin including generated binary files or other files that you did not mean to include. You do want to start including README, so let's start tracking the file.</p>
<h3 class="calibre5">Tracking New Files</h3>
<p class="calibre3">In order to begin tracking a new file, you use the command <code class="calibre10">git add</code>. To begin tracking the README file, you can run this:</p>
<pre class="calibre9"><code class="calibre10">$ git add README
</code></pre>
<p class="calibre3">If you run your status command again, you can see that your README file is now tracked and staged:</p>
<pre class="calibre9"><code class="calibre10">$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
#
</code></pre>
<p class="calibre3">You can tell that it's staged because it's under the "Changes to be committed" heading. If you commit at this point, the version of the file at the time you ran git add is what will be in the historical snapshot. You may recall that when you ran git init earlier, you then ran git add (files) - that was to begin tracking files in your directory. The git add command takes a path name for either a file or a directory; if it's a directory, the command adds all the files in that directory recursively.</p>
<h3 class="calibre5">Staging Modified Files</h3>
<p class="calibre3">Let's change a file that was already tracked. If you change a previously tracked file called <code class="calibre10">benchmarks.rb</code> and then run your <code class="calibre10">status</code> command again, you get something that looks like this:</p>
<pre class="calibre9"><code class="calibre10">$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: benchmarks.rb
#
</code></pre>
<p class="calibre3">The benchmarks.rb file appears under a section named "Changed but not updated" - which means that a file that is tracked has been modified in the working directory but not yet staged. To stage it, you run the <code class="calibre10">git add</code> command (it's a multipurpose command - you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved). Let's run <code class="calibre10">git add</code> now to stage the benchmarks.rb file, and then run <code class="calibre10">git status</code> again:</p>
<pre class="calibre9"><code class="calibre10">$ git add benchmarks.rb
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
# modified: benchmarks.rb
#
</code></pre>
<p class="calibre3">Both files are staged and will go into your next commit. At this point, suppose you remember one little change that you want to make in benchmarks.rb before you commit it. You open it again and make that change, and you're ready to commit. However, let's run <code class="calibre10">git status</code> one more time:</p>
<pre class="calibre9"><code class="calibre10">$ vim benchmarks.rb
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
# modified: benchmarks.rb
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: benchmarks.rb
#
</code></pre>
<p class="calibre3">What the heck? Now benchmarks.rb is listed as both staged and unstaged. How is that possible? It turns out that Git stages a file exactly as it is when you run the git add command. If you commit now, the version of benchmarks.rb as it was when you last ran the git add command is how it will go into the commit, not the version of the file as it looks in your working directory when you run git commit. If you modify a file after you run <code class="calibre10">git add</code>, you have to run <code class="calibre10">git add</code> again to stage the latest version of the file:</p>
<pre class="calibre9"><code class="calibre10">$ git add benchmarks.rb
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
# modified: benchmarks.rb
#
</code></pre>
<h3 class="calibre5">Ignoring Files</h3>
<p class="calibre3">Often, you'll have a class of files that you don't want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:</p>
<pre class="calibre9"><code class="calibre10">$ cat .gitignore
*.[oa]
*~
</code></pre>
<p class="calibre3">The first line tells Git to ignore any files ending in .o or .a - object and archive files that may be the product of building your code. The second line tells Git to ignore all files that end with a tilde (<code class="calibre10">~</code>), which is used by many text editors such as Emacs to mark temporary files. You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. Setting up a .gitignore file before you get going is generally a good idea so you don't accidentally commit files that you really don't want in your Git repository.</p>
<p class="calibre3">The rules for the patterns you can put in the .gitignore file are as follows:</p>
<ul class="calibre7"><li class="calibre8">Blank lines or lines starting with # are ignored.</li>
<li class="calibre8">Standard glob patterns work.</li>
<li class="calibre8">You can end patterns with a forward slash (<code class="calibre10">/</code>) to specify a directory.</li>
<li class="calibre8">You can negate a pattern by starting it with an exclamation point (<code class="calibre10">!</code>).</li>
</ul><p class="calibre3">Glob patterns are like simplified regular expressions that shells use. An asterisk (<code class="calibre10">*</code>) matches zero or more characters; <code class="calibre10">[abc]</code> matches any character inside the brackets (in this case a, b, or c); a question mark (<code class="calibre10">?</code>) matches a single character; and brackets enclosing characters separated by a hyphen(<code class="calibre10">[0-9]</code>) matches any character between them (in this case 0 through 9) . </p>
<p class="calibre3">Here is another example .gitignore file:</p>
<pre class="calibre9"><code class="calibre10"># a comment - this is ignored
*.a # no .a files
!lib.a # but do track lib.a, even though you're ignoring .a files above
/TODO # only ignore the root TODO file, not subdir/TODO
build/ # ignore all files in the build/ directory
doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
</code></pre>
<h3 class="calibre5">Viewing Your Staged and Unstaged Changes</h3>
<p class="calibre3">If the <code class="calibre10">git status</code> command is too vague for you - you want to know exactly what you changed, not just which files were changed - you can use the <code class="calibre10">git diff</code> command. We'll cover <code class="calibre10">git diff</code> in more detail later; but you'll probably use it most often to answer these two questions: What have you changed but not yet staged? And what have you staged that you are about to commit? Although <code class="calibre10">git status</code> answers those questions very generally, <code class="calibre10">git diff</code> shows you the exact lines added and removed - the patch, as it were. </p>
<p class="calibre3">Let's say you edit and stage the README file again and then edit the benchmarks.rb file without staging it. If you run your <code class="calibre10">status</code> command, you once again see something like this:</p>
<pre class="calibre9"><code class="calibre10">$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: benchmarks.rb
#
</code></pre>
<p class="calibre3">To see what you've changed but not yet staged, type <code class="calibre10">git diff</code> with no other arguments:</p>
<pre class="calibre9"><code class="calibre10">$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..da65585 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
@commit.parents[0].parents[0].parents[0]
end
+ run_code(x, 'commits 1') do
+ git.commits.size
+ end
+
run_code(x, 'commits 2') do
log = git.commits('master', 15)
log.size
</code></pre>
<p class="calibre3">That command compares what is in your working directory with what is in your staging area. The result tells you the changes you've made that you haven't yet staged.</p>
<p class="calibre3">If you want to see what you've staged that will go into your next commit, you can use <code class="calibre10">git diff --cached</code>. (In Git versions 1.6.1 and later, you can also use <code class="calibre10">git diff --staged</code>, which may be easier to remember.) This command compares your staged changes to your last commit:</p>
<pre class="calibre9"><code class="calibre10">$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README2
@@ -0,0 +1,5 @@
+grit
+ by Tom Preston-Werner, Chris Wanstrath
+ http://github.com/mojombo/grit
+
+Grit is a Ruby library for extracting information from a Git repository
</code></pre>
<p class="calibre3">It's important to note that <code class="calibre10">git diff</code> by itself doesn't show all changes made since your last commit - only changes that are still unstaged. This can be confusing, because if you've staged all of your changes, <code class="calibre10">git diff</code> will give you no output.</p>
<p class="calibre3">For another example, if you stage the benchmarks.rb file and then edit it, you can use <code class="calibre10">git diff</code> to see the changes in the file that are staged and the changes that are unstaged:</p>
<pre class="calibre9"><code class="calibre10">$ git add benchmarks.rb
$ echo '# test line' >> benchmarks.rb
$ git status
# On branch master
#
# Changes to be committed:
#
# modified: benchmarks.rb
#
# Changed but not updated:
#
# modified: benchmarks.rb
#
</code></pre>
<p class="calibre3">Now you can use <code class="calibre10">git diff</code> to see what is still unstaged</p>
<pre class="calibre9"><code class="calibre10">$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index e445e28..86b2f7c 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -127,3 +127,4 @@ end
main()
##pp Grit::GitRuby.cache_client.stats
+# test line
</code></pre>
<p class="calibre3">and <code class="calibre10">git diff --cached</code> to see what you've staged so far:</p>
<pre class="calibre9"><code class="calibre10">$ git diff --cached
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..e445e28 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
@commit.parents[0].parents[0].parents[0]
end
+ run_code(x, 'commits 1') do
+ git.commits.size
+ end
+
run_code(x, 'commits 2') do
log = git.commits('master', 15)
log.size
</code></pre>
<h3 class="calibre5">Committing Your Changes</h3>
<p class="calibre3">Now that your staging area is set up the way you want it, you can commit your changes. Remember that anything that is still unstaged - any files you have created or modified that you haven't run <code class="calibre10">git add</code> on since you edited them - won't go into this commit. They will stay as modified files on your disk.
In this case, the last time you ran <code class="calibre10">git status</code>, you saw that everything was staged, so you're ready to commit your changes. The simplest way to commit is to type <code class="calibre10">git commit</code>:</p>
<pre class="calibre9"><code class="calibre10">$ git commit
</code></pre>
<p class="calibre3">Doing so launches your editor of choice. (This is set by your shell's <code class="calibre10">$EDITOR</code> environment variable - usually vim or emacs, although you can configure it with whatever you want using the <code class="calibre10">git config --global core.editor</code> command as you saw in Chapter 1). </p>
<p class="calibre3">The editor displays the following text (this example is a Vim screen):</p>
<pre class="calibre9"><code class="calibre10"># Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: README
# modified: benchmarks.rb
~
~
~
".git/COMMIT_EDITMSG" 10L, 283C
</code></pre>
<p class="calibre3">You can see that the default commit message contains the latest output of the <code class="calibre10">git status</code> command commented out and one empty line on top. You can remove these comments and type your commit message, or you can leave them there to help you remember what you're committing. (For an even more explicit reminder of what you've modified, you can pass the <code class="calibre10">-v</code> option to <code class="calibre10">git commit</code>. Doing so also puts the diff of your change in the editor so you can see exactly what you did.) When you exit the editor, Git creates your commit with that commit message (with the comments and diff stripped out).</p>
<p class="calibre3">Alternatively, you can type your commit message inline with the <code class="calibre10">commit</code> command by specifying it after a -m flag, like this:</p>
<pre class="calibre9"><code class="calibre10">$ git commit -m "Story 182: Fix benchmarks for speed"
[master]: created 463dc4f: "Fix benchmarks for speed"
2 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 README
</code></pre>
<p class="calibre3">Now you've created your first commit! You can see that the commit has given you some output about itself: which branch you committed to (master), what SHA-1 checksum the commit has (<code class="calibre10">463dc4f</code>), how many files were changed, and statistics about lines added and removed in the commit.</p>
<p class="calibre3">Remember that the commit records the snapshot you set up in your staging area. Anything you didn't stage is still sitting there modified; you can do another commit to add it to your history. Every time you perform a commit, you're recording a snapshot of your project that you can revert to or compare to later.</p>
<h3 class="calibre5">Skipping the Staging Area</h3>
<p class="calibre3">Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow. If you want to skip the staging area, Git provides a simple shortcut. Providing the <code class="calibre10">-a</code> option to the <code class="calibre10">git commit</code> command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the <code class="calibre10">git add</code> part:</p>
<pre class="calibre9"><code class="calibre10">$ git status
# On branch master
#
# Changed but not updated:
#
# modified: benchmarks.rb
#
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 files changed, 5 insertions(+), 0 deletions(-)
</code></pre>
<p class="calibre3">Notice how you don't have to run <code class="calibre10">git add</code> on the benchmarks.rb file in this case before you commit.</p>
<h3 class="calibre5">Removing Files</h3>
<p class="calibre3">To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The <code class="calibre10">git rm</code> command does that and also removes the file from your working directory so you don't see it as an untracked file next time around.</p>
<p class="calibre3">If you simply remove the file from your working directory, it shows up under the "Changed but not updated" (that is, <em class="calibre12">unstaged</em>) area of your <code class="calibre10">git status</code> output:</p>
<pre class="calibre9"><code class="calibre10">$ rm grit.gemspec
$ git status
# On branch master
#
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
#
# deleted: grit.gemspec
#
</code></pre>
<p class="calibre3">Then, if you run <code class="calibre10">git rm</code>, it stages the file's removal:</p>
<pre class="calibre9"><code class="calibre10">$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
# On branch master
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: grit.gemspec
#
</code></pre>
<p class="calibre3">The next time you commit, the file will be gone and no longer tracked. If you modified the file and added it to the index already, you must force the removal with the <code class="calibre10">-f</code> option. This is a safety feature to prevent accidental removal of data that hasn't yet been recorded in a snapshot and that can't be recovered from Git.</p>
<p class="calibre3">Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore. This is particularly useful if you forgot to add something to your <code class="calibre10">.gitignore</code> file and accidentally added it, like a large log file or a bunch of <code class="calibre10">.a</code> compiled files. To do this, use the <code class="calibre10">--cached</code> option:</p>
<pre class="calibre9"><code class="calibre10">$ git rm --cached readme.txt
</code></pre>
<p class="calibre3">You can pass files, directories, and file-glob patterns to the <code class="calibre10">git rm</code> command. That means you can do things such as</p>
<pre class="calibre9"><code class="calibre10">$ git rm log/\*.log
</code></pre>
<p class="calibre3">Note the backslash (<code class="calibre10">\</code>) in front of the <code class="calibre10">*</code>. This is necessary because Git does its own filename expansion in addition to your shell's filename expansion. This command removes all files that have the <code class="calibre10">.log</code> extension in the <code class="calibre10">log/</code> directory. Or, you can do something like this:</p>
<pre class="calibre9"><code class="calibre10">$ git rm \*~
</code></pre>
<p class="calibre3">This command removes all files that end with <code class="calibre10">~</code>.</p>
<h3 class="calibre5">Moving Files</h3>
<p class="calibre3">Unlike many other VCS systems, Git doesn't explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact - we'll deal with detecting file movement a bit later.</p>
<p class="calibre3">Thus it's a bit confusing that Git has a <code class="calibre10">mv</code> command. If you want to rename a file in Git, you can run something like</p>
<pre class="calibre9"><code class="calibre10">$ git mv file_from file_to
</code></pre>
<p class="calibre3">and it works fine. In fact, if you run something like this and look at the status, you'll see that Git considers it a renamed file:</p>
<pre class="calibre9"><code class="calibre10">$ git mv README.txt README
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: README.txt -> README
#
</code></pre>
<p class="calibre3">However, this is equivalent to running something like this:</p>
<pre class="calibre9"><code class="calibre10">$ mv README.txt README
$ git rm README.txt
$ git add README
</code></pre>
<p class="calibre3">Git figures out that it's a rename implicitly, so it doesn't matter if you rename a file that way or with the <code class="calibre10">mv</code> command. The only real difference is that <code class="calibre10">mv</code> is one command instead of three - it's a convenience function. More important, you can use any tool you like to rename a file, and address the add/rm later, before you commit.</p>
</body>
</html>