After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened. The most basic and powerful tool to do this is the git log command.
These examples use a very simple project called simplegit that I crated for demonstrations :
By default, with no arguments, git log lists the commits made in that repository in reverse chronological order. That is, the most recent commits show up first. As you can see, this command lists each commit with its SHA-1 checksum, the author’s name and e-mail, the date written, and the commit message.
A huge number and variety of options of this command are available to show you exactly what you’re looking for. Here, I’ll show you some of the most-used options. One of the more helpful options is -p, which shows the diff introduced in each commit. You can also use -1, which limits the output to only the last entry :
You can also use a series of summarizing options. For example, if you want to see some abbreviated stats for each commit, you can use the --stat option :
As you can see, the --stat option prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end. Another really useful option is --pretty. This option changes the log output to formats other than the default. A few prebuilt options are available for you to use. The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. In addition, the short, full, and fuller options show the output in roughly the same format but with less or more information, respectively :
The most interesting option is format, which allows you to specify your own log output format. This is especially useful when you’re generating output for machine parsing—because you specify the format explicitly, you know it won’t change with updates to Git :
Table 2-1 lists some of the more useful options that format takes :
The oneline and format options are particularly useful with another log option called --graph. This option adds a nice little ASCII graph showing your branch and merge history, in which you can see your copy of the Grit project repository :
Those are only some simple output-formatting options to git log—there are many more. Table 2-2 lists the options I’ve covered so far and some other common formatting options that may be useful, along with how they change the output :
- Limiting Log Output
In addition to output-formatting options, git log takes a number of useful limiting options—that is, options that let you show only a subset of commits. You’ve seen one such option already—the -1 option, which show only the latest commit. In reality, you’re unlikely to use that often, because
Git by default pipes all output through a pager so you see only one page of log output at a time. However, the time-limiting options such as --since and --until are very useful. For example, this command gets the list of commits made in the last two weeks :
This command works with lots of formats—you can specify a specific date ("2008-01-15") or a relative date such as "2 years 1 day 3 minutes ago".
You can also filter the list to commits that match some search criteria. The --author option allows you to filter on a specific author, and the --grep option lets you search for keywords in the commit messages. (Note that if you want to specify both author and grep options, you have to add --all-match or the command will match commits with either.) In Table 2-3, I list these and a few other common options for your reference :
For example, if you want to see which commits in the Git source code history were committed by Junio Hamano and were not merges in the month of October 2008, you can run something like this :
- Using a GUI to Visualize History
If you like to use a more graphical tool to visualize your commit history, you may want to take a look at a Tcl/Tk program called gitk that is distributed with Git. It is basically a visual git log tool, and it accepts nearly all the filtering options that git log does. If you type gitk on the command line in your project, you should see something like below figure :
Undoing Things :
At any stage, you may want to undo something. Here, I’ll review a few basic tools for undoing changes that you’ve made. Be careful, because you can’t always undo some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.
- Changing Your Last Commit
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to try that commit again, you can run commit with the --amend option :
This command takes your staging area and uses it for the commit. If you have made no changes since your last commit (for instance, you run this command it immediately after your previous commit), then your snapshot will look exactly the same and all you’ll change is your commit message.
The same commit-message editor fires up, but it already contains the message of your previous commit. You can edit the message the same as always, but it overwrites your previous commit. As an example, if you commit and then realize you forgot to stage the changes in a file 'TEST1' you wanted to add to this commit of file 'TEST', you can do something like this :
- Unstaging a Staged File
The next two sections demonstrate how to wrangle your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you :
Right below the “Changes to be committed” text, it says "git reset HEAD
The command is a bit strange, but it works. The 'TEST' file is modified but once again unstaged.
- Unmodifying a Modified File
What if you realize that you don’t want to keep your changes to the 'TEST' file? How can you easily unmodify it—revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, git status tells you how to do that, too. In the last example output, the unstaged area looks like this :
It tells you pretty explicitly how to discard the changes you’ve made (at least, the newer versions of Git, 1.6.1 and later, do this—if you have an older version, I highly recommend upgrading it to get some of these nicer usability features). Let’s do what it says :
You can see that the changes have been reverted. You should also realize that this is a dangerous command: any changes you made to that file are gone—you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file. If you just need to get it out of the way, I’ll go over stashing and branching in the next chapter, these are generally better ways to go.
Working with Remotes :
To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. You can have several of them, each of which generally is either read-only or read/write for you. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.
Managing remote repositories includes knowing how to add remote repositories, remove remotes that are no longer valid, manage various remote branches and define them as being tracked or not, and more. In this section, I’ll cover these remote-management skills.
- Showing Your Remotes
To see which remote servers you have configured, you can run the git remote command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin—that is the default name Git gives to the server you cloned from :
You can also specify -v, which shows you the URL that Git has stored for the shortname to be expanded to :
- Adding Remote Repositories
I’ve mentioned and given some demonstrations of adding remote repositories in previous sections, but here is how to do it explicitly. To add a new remote Git repository as a shortname you can reference easily, run git remote add [shortname] [url] :
Now you can use the string pb on the command line in lieu of the whole URL. For example, if you want to fetch all the information that Paul has but that you don’t yet have in your repository, you can run git fetch pb :
- Fetching and Pulling from Your Remotes
As you just saw, to get data from your remote projects, you can run :
The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time. (I’ll go over what branches are and how to use them in much more detail in Chapter 3.)
If you cloned a repository, the command automatically adds that remote repository under the name orign. So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the fetch command pulls the data to your local repository—it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
If you have a branch set up to track a remote branch (see the next section and Chapter 3 for more information), you can use the git pull command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the git clone command automatically sets up your local master branch to track the remote master branch on the server you cloned from (assuming the remote has a master branch). Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.
- Pushing to Your Remotes
When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: git push [remote-name] [branch-name]. If you want to push your master branch to your origin server (again, cloning generally sets up both of those names for you automatically), then you can run this to push your work back up to the server :
This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push. See Chapter 3 for more detailed information on how to push to remote servers.
- Inspecting a Remote
If you want to see more information about a particular remote, you can use the git remote show [remote-name] command. If you run this command with a particular shortname, such as origin, you get something like this :
This command shows which branch is automatically pushed when you run git push on certain branches. It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple branches that are automatically merged when you run git pull.
- Removing and Renaming Remotes
If you want to rename a reference, in newer versions of Git you can run git remote rename to change a remote’s shortname. For instance, if you want to rename pb to paul, you can do so :
It’s worth mentioning that this changes your remote branch names, too. What used to be referenced at pb/master is now at paul/master.
If you want to remove a reference for some reason—you’ve moved the server or are no longer using a particular mirror, or perhaps a contributor isn’t contributing anymore—you can use git remote rm [short-name] :
Tagging :
Like most VCSs, Git has the ability to tag specific points in history as being important. Generally, people use this functionality to mark release points (v1.0, and so on). In this section, you’ll learn how to list the available tags, how to create new tags, and what the different types of tags are.
- Listing Your Tags
Listing the available tags in Git is straightforward. Just type git tag :
This command lists the tags in alphabetical order; the order in which they appear has no real importance.
You can also search for tags with a particular pattern. The Git source repo, for instance, contains more than 240 tags. If you’re only interested in looking at the 1.4.2 series, you can run this :
- Creating Tags
Git uses two main types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change—it’s just a pointer to a specific commit.Annotated tags, however, are stored as full objects in the Git database. They’re check-summed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.
Annotated Tags
Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the tag command :
The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can type it in. You can see the tag data along with the commit that was tagged by using the git show command :
That shows the tagger information, the date the commit was tagged, and the annotation message before showing the commit information.
Lightweight Tags
Another way to tag commits is with a lightweight tag. This is basically the commit checksum stored in a file—no other information is kept. To create a lightweight tag, don’t supply the -a, -s, or -m option :
This time, if you run git show on the tag, you don’t see the extra tag information. The command just shows the commit :
- Tagging Later
You can also tag commits after you’ve moved past them. Suppose your commit history looks like this :
Now, suppose you forgot to tag the project at v0.11, which was at the 'Tag test' commit. You can add it after the fact. To tag that commit, you specify the commit checksum (or part of it) at the end of the command :
- Sharing Tags
By default, the git push command doesn’t transfer tags to remote servers. You have to explicitly push tags to a shared server after you create them. This process is just like sharing remote branches—you can run git push origin [tagname] :
If you have a lot of tags that you want to push up at once, you can also use the --tags option to the git push command. This transfers to the remote server all of your tags that aren’t already there :
Now, when someone else clones or pulls from your repository, they will get all your tags as well.
沒有留言:
張貼留言