2.1 Git Basics

来源:互联网 发布:女人被强奸 知乎 编辑:程序博客网 时间:2024/06/06 09:13
f you can read only one chapter to get going with Git, this is it.This chapter covers every basic command you need to do the vast majority of the things you’ll eventually spend your time doing with Git.By the end of the chapter, you should be able to configure and initialize a repository, begin and stop tracking files, and stage and commit changes.We’ll also show you how to set up Git to ignore certain files and file patterns, how to undo mistakes quickly and easily, how to browse the history of your project and view changes between commits, and how to push and pull from remote repositories.

Getting a Git Repository

You can get a Git project using two main approaches.The first takes an existing project or directory and imports it into Git.The second clones an existing Git repository from another server.

Initializing a Repository in an Existing Directory

If you’re starting to track an existing project in Git, you need to go to the project’s directory. If you’ve never done this, it looks a little different depending on which system you’re running:

for Linux:

$ cd /home/user/your_repository

for Mac:

$ cd /Users/user/your_repository

for Windows:

$ cd /c/user/your_repository

and type:

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files – a Git repository skeleton.At this point, nothing in your project is tracked yet.(SeeGit Internals for more information about exactly what files are contained in the.git directory you just created.)

If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit.You can accomplish that with a fewgit add commands that specify the files you want to track, followed by agit commit:

$ git add *.c$ git add LICENSE$ git commit -m 'initial project version'

We’ll go over what these commands do in just a minute.At this point, you have a Git repository with tracked files and an initial commit.

Cloning an Existing Repository

If you want to get a copy of an existing Git repository – for example, a project you’d like to contribute to – the command you need isgit clone.If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is "clone" and not "checkout".This is an important distinction – instead of getting just a working copy, Git receives a full copy of nearly all data that the server has.Every version of every file for the history of the project is pulled down by default when you rungit clone.In fact, if your server disk gets corrupted, you can often use nearly any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there – see Git on the Server for more details).

You clone a repository with git clone [url].For example, if you want to clone the Git linkable library called libgit2, you can do so like this:

$ git clone https://github.com/libgit2/libgit2

That creates a directory named “libgit2”, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version.If you go into the newlibgit2 directory, you’ll see the project files in there, ready to be worked on or used.If you want to clone the repository into a directory named something other than “libgit2”, you can specify that as the next command-line option:

$ git clone https://github.com/libgit2/libgit2 mylibgit

That command does the same thing as the previous one, but the target directory is calledmylibgit.

Git has a number of different transfer protocols you can use.The previous example uses thehttps:// protocol, but you may also see git:// or user@server:path/to/repo.git, which uses the SSH transfer protocol.Git on the Server will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each.