演習 - Git を試す
最初のリポジトリを作成する前に、Git がインストールされ、構成されていることを確認する必要があります。 Git は Azure Cloud Shell にプレインストールされているため、右側の Cloud Shell で Git を使用できます。
Git の構成
Cloud Shell で、Git がインストールされていることを再確認するには、「
git --version」と入力します。git --versionヒント
[コピー] ボタンを使用して、コマンドをクリップボードにコピーできます。 貼り付けるには、Cloud Shell ターミナルで新しい行を右クリックし、[ 貼り付け] を選択するか、Shift + Insert キーボード ショートカット (macOS の場合は ⌘+ V) を使用します。
次の例のような出力が表示されます。
git version 2.7.4Git を構成するには、いくつかのグローバル変数 (
user.nameとuser.email) を定義する必要があります。 どちらもコミットを行うために必要です。次のコマンドを使用して、Cloud Shell で自分の名前を設定します。 使用したいユーザー名に
<USER_NAME>を置き換えてください。git config --global user.name "<USER_NAME>"次に、次のコマンドを使用して
user.email構成変数を作成し、<USER_EMAIL>を電子メール アドレスに置き換えます。git config --global user.email "<USER_EMAIL>"次のコマンドを実行して、変更が行われたことを確認します。
git config --list出力に次の例のような 2 つの行が含まれていることを確認します。 名前と電子メール アドレスは、例で示されているものとは異なります。
user.name=User Name user.email=user-name@contoso.com
Git リポジトリを設定する
Git は、特定のフォルダー内のファイルに対する変更を確認することによって機能します。 作業ツリー (プロジェクト ディレクトリ) として機能するフォルダーを作成し、変更の追跡を開始できるように Git に通知します。 Git リポジトリをそのフォルダーに初期化して、変更の追跡を開始するよう Git に指示します。
まず、プロジェクトの空のフォルダーを作成し、その中に Git リポジトリを初期化します。
Cats という名前のフォルダーを作成します。 このフォルダーは、作業ツリーとも呼ばれるプロジェクト ディレクトリになります。 プロジェクト ディレクトリには、プロジェクトに関連するすべてのファイルが格納されます。 この演習では、Web サイトとそのコンテンツを作成するためのファイルが保存される場所について説明します。
mkdir Catscdコマンドを使用して、プロジェクト ディレクトリに移動します。cd Cats次に、新しいリポジトリを初期化し、既定のブランチの名前を
mainに設定します。Git バージョン 2.28.0 以降を実行している場合は、次のコマンドを使用します。
git init --initial-branch=mainまたは、次のコマンドを使用します。
git init -b main以前のバージョンの Git の場合は、これらのコマンドを使用します。
git init git checkout -b maininitialize コマンドを実行すると、次の例のような出力が表示されます。
Initialized empty Git repository in /home/<user>/Cats/.git/ Switched to a new branch 'main'次に、
git statusコマンドを使用して、作業ツリーの状態を表示します。git statusGit はこの出力で応答します。これは、
mainが現在のブランチであることを示します。 (これは唯一のブランチでもあります)。今のところ大丈夫です。On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)lsコマンドを使用して、作業ツリーの内容を表示します。ls -aディレクトリに .git という名前のサブディレクトリが含まれていることを確認します。 (通常、Linux ではピリオドで始まるファイル名とディレクトリ名が非表示になるため、
-aでlsオプションを使用することが重要です)。このフォルダーは Git リポジトリであり、Git が作業ツリーのメタデータと履歴を格納するディレクトリです。通常、 .git ディレクトリでは直接何も行いません。 Git は、作業ツリーの状態が変化するにつれてメタデータを更新し、ファイル内の変更を追跡します。 このディレクトリはハンズオフですが、Git にとって非常に重要です。
Git からヘルプを表示する
Git には、ほとんどのコマンドライン ツールと同様に、コマンドとキーワードを検索するために使用できる組み込みのヘルプ関数があります。
次のコマンドを入力して、Git でできることに関するヘルプを表示します。
git --helpこのコマンドは、次の出力を表示します。
usage: git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches checkout Switch branches or restore working tree files commit Record changes to the repository diff Show changes between commits, commit and working tree, etc merge Join two or more development histories together rebase Forward-port local commits to the updated upstream head tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
Git で使用できるさまざまなオプションを確認し、各コマンドには独自のヘルプ ページが付属しています。詳細を調べ始めるときに注意してください。 これらのコマンドのすべてがまだ意味をなすわけではありませんが、VCS の使用経験がある場合は、よく知られているものがあります。
次のレッスンでは、試したコマンドと Git の基本について詳しく学習します。