First-Time Git Setup
Git comes with a tool called git config
that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables are stored in
-
~/.gitconfig
. You can make Git read and write to this file specifically by passing the--global
option. - Config file in the Git directory (that is,
.git/config
) of whatever repository you’re currently using: Specific to that single repository.
Your Identity
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
If you want to override this with a di erent name or email address for specific projects, you can run the command without the --global
option when you’re in that project.
Checking Your Settings
$ git config --list
Getting and Creating Projects
2.2. Recording Changes to the Respository
2.2.1. Checking the Status of Your Files
$ git status # gst
$ git status -s # short status
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
New files that aren’t tracked have a ??
next to them, new files that have been added to the staging area have an A
, modified files have an M
and so on. There are two columns to the output - the left hand column indicates that the file is staged and the right hand column indicates that it’s modified. So for example in that output, the README file is modified in the working directory but not yet staged, while the lib/simplegit.rb file is modified and staged. The Rakefile was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.
2.2.2. Ignoring Files
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.50]
The rules for the patterns you can put in the .gitignore file are as follows:
- Blank lines or lines starting with # are ignored.
- Standard glob patterns work.
- You can start patterns with a forward slash (/) to avoid recursivity.
- You can end patterns with a forward slash (/) to specify a directory.
- You can negate a pattern by starting it with an exclamation point (!).
2.2.3. Viewing Your Staged and Unstaged Changes [P.51]
2.2.4. Removing (Moving) Files
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 git rm
command does that.
Sometimes you may want to keep the file in your working tree but remove it from your staging area. This is particularly useful if you forgot to add something to your .gitignore
file and accidentally staged it, like a large log file or a bunch of .a compiled files. To do this, use the --cached
option.
You can pass files, directories, and file-glob patterns to the git rm command.
$ git rm README
$ git rm --cached README
$ git rm log/\*.log # git does its own filename expansion
$ git rm \*~ # remove files whose names end with a ~
$ git mv README data/README
2.3. Viewing the Commit History
The most basic and powerful tool to view the commit history is the git log
command. A huge number and variety of options to the git log command are available to show you exactly what you’re looking for. One of the more helpful options is -p
, which shows the difference introduced in each commit. You can also use -2
, which limits the output to only the last two entries.
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon
Date: Mon Mar 17 21:52:11 2008 -0700
changed the version number
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon
Date: Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon
Date: Sat Mar 15 10:31:28 2008 -0700
first commit
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon
Date: Mon Mar 17 21:52:11 2008 -0700
changed the version number
diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
$ git log --stat
$ git log --pretty=oneline
$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty=format:"%h %s" --graph
$ git log --since=2.weeks
2.4. Undoing Things
$ git commit --amend