リポジトリのセットアップ

リポジトリのセットアップ

このチュートリアルでは、Git の重要コマンドの概要を簡単に説明します。最初のリポジトリのセットアップに関するセクションでは、バージョン管理を行うプロジェクトを新たに開始する場合に必要となる各種コマンドの解説を行います。残りのセクションでは日常的に使用する Git コマンドを紹介します。

この章を学び終えると、Git リポジトリの作成、プロジェクトスナップショットの安全な保存、プロジェクト履歴の閲覧などができるようになります。

git init

The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new empty repository. Most of the other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.

Executing git init creates a .git subdirectory in the project root, which contains all of the necessary metadata for the repo. Aside from the .git directory, an existing project remains unaltered (unlike SVN, Git doesn't require a .git folder in every subdirectory).

使用法

git init

Transform the current directory into a Git repository. This adds a .git folder to the current directory and makes it possible to start recording revisions of the project.

git init <directory>

Create an empty Git repository in the specified directory. Running this command will create a new folder called <directory containing nothing but the .git subdirectory.

git init --bare <directory>

Initialize an empty Git repository, but omit the working directory. Shared repositories should always be created with the --bare flag (see discussion below). Conventionally, repositories initialized with the --bare flag end in .git. For example, the bare version of a repository called my-project should be stored in a directory called my-project.git.

ディスカッション

Compared to SVN, the git init command is an incredibly easy way to create new version-controlled projects. Git doesn’t require you to create a repository, import files, and check out a working copy. All you have to do is cd into your project folder and run git init, and you’ll have a fully functional Git repository.

However, for most projects, git init only needs to be executed once to create a central repository—developers typically don't use git init to create their local repositories. Instead, they'll usually use git clone to copy an existing repository onto their local machine.

ベアリポジトリ

The --bare flag creates a repository that doesn’t have a working directory, making it impossible to edit files and commit changes in that repository. Central repositories should always be created as bare repositories because pushing branches to a non-bare repository has the potential to overwrite changes. Think of --bare as a way to mark a repository as a storage facility, opposed to a development environment. This means that for virtually all Git workflows, the central repository is bare, and developers local repositories are non-bare.

Git チュートリアル: ベアリポジトリ

Since git clone is a more convenient way to create local copies of a project, the most common use case for git init is to create a central repository:

ssh <user>@<host>
cd path/above/repo
git init --bare my-project.git

First, you SSH into the server that will contain your central repository. Then, you navigate to wherever you’d like to store the project. Finally, you use the --bare flag to create a central storage repository. Developers would then clone my-project.git to create a local copy on their development machine.

git clone

The git clone command copies an existing Git repository. This is sort of like svn checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.

利用者の便宜のため、クローンを行うと、元のリポジトリをポイントする origin という名称のリモート接続を自動的に作成します。これにより、極めて簡単に中央リポジトリとの通信を行うことができます。

使用法

git clone <repo>

Clone the repository located at <repo> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.

git clone <repo> <directory>

Clone the repository located at <repo> into the folder called <directory> on the local machine.

ディスカッション

If a project has already been set up in a central repository, the git clone command is the most common way for users to obtain a development copy. Like git init, cloning is generally a one-time operation—once a developer has obtained a working copy, all version control operations and collaborations are managed through their local repository.

リポジトリ間コラボレーション

Git における「作業コピー」が SVN リポジトリからコードをチェックアウトして得られる作業コピーとは大きく異なる概念であることを理解しなければなりません。SVN とは異なり、Git では作業コピーと中央リポジトリとの間に実質的な相違はなく、両者とも完全な Git リポジトリです。

This makes collaborating with Git fundamentally different than with SVN. Whereas SVN depends on the relationship between the central repository and the working copy, Git’s collaboration model is based on repository-to-repository interaction. Instead of checking a working copy into SVN’s central repository, you push or pull commits from one repository to another.

Git チュートリアル: リポジトリと作業ディレクトリのコラボレーション Git チュートリアル: リポジトリ間コラボレーション

Of course, there’s nothing stopping you from giving certain Git repos special meaning. For example, by simply designating one Git repo as the “central” repository, it’s possible to replicate a Centralized workflow using Git. The point is, this is accomplished through conventions rather than being hardwired into the VCS itself.

The example below demonstrates how to obtain a local copy of a central repository stored on a server accessible at example.com using the SSH username john:

git clone ssh://john@example.com/path/to/my-project.git
cd my-project
# Start working on the project

The first command initializes a new Git repository in the my-project folder on your local machine and populates it with the contents of the central repository. Then, you can cd into the project and start editing files, committing snapshots, and interacting with other repositories. Also note that the .git extension is omitted from the cloned repository. This reflects the non-bare status of the local copy.

git config

The git config command lets you configure your Git installation (or an individual repository) from the command line. This command can define everything from user info to preferences to the behavior of a repository. Several common configuration options are listed below.

使用法

git config user.name <name>

Define the author name to be used for all commits in the current repository. Typically, you’ll want to use the --global flag to set configuration options for the current user.

git config --global user.name <name>

現在のユーザーが行うすべてのコミットのオーサー名を設定します。

git config --global user.email <email>

現在のユーザーが行うすべてのコミットに関してそのオーサー E メールアドレスを設定します。

git config --global alias.<alias-name> <git-command>

Git コマンドのショートカットを作成します。

git config --system core.editor <editor>

Define the text editor used by commands like git commit for all users on the current machine. The <editor> argument should be the command that launches the desired editor (e.g., vi).

git config --global --edit

グローバルな設定ファイルをテキストエディターで開くコマンドで、これを使用して手動で設定ファイルを編集することができます。

ディスカッション

All configuration options are stored in plaintext files, so the git config command is really just a convenient command-line interface. Typically, you’ll only need to configure a Git installation the first time you start working on a new development machine, and for virtually all cases, you’ll want to use the --global flag.

Git のオプション設定は 3 つのファイルに分けて保存され、以下のコマンドを用いて、個々のリポジトリ、ユーザー、システム全体の設定内容をそれぞれ確認することができます:

  • <repo>/.git/config – Repository-specific settings.
  • ~/.gitconfig – User-specific settings. This is where options set with the --global flag are stored.
  • $(prefix)/etc/gitconfig – System-wide settings.

これらのファイルのオプション設定が矛盾する場合は、ユーザー固有の設定がシステム全体に関する設定に優越し、各リポジトリ固有の設定がユーザー固有の設定に優越します。これらのファイルを開くと、次のような内容が表示されます:

[user]
name = John Smith
email = john@example.com
[alias]
st = status
co = checkout
br = branch
up = rebase
ci = commit
[core]
editor = vim

You can manually edit these values to the exact same effect as git config.

Git のインストール後に最初にするべきことは、ユーザーの名前と E メールアドレスを設定し、いくつかのデフォルト設定をカスタマイズすることです。初期設定コマンドは通常次のようなものになります:

# Tell Git who you are
git config --global user.name "John Smith"
git config --global user.email john@example.com
# Select your favorite text editor
git config --global core.editor vim
# Add some SVN-like aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.up rebase
git config --global alias.ci commit

This will produce the ~/.gitconfig file from the previous section.

Ready to learn Git?

Try this interactive tutorial.

今すぐ始める