トップ 開発環境 Git 使い方 Git Bash

Git Bashとは―Gitのコマンドラインツール。使い方を解説。

Gitのコマンドラインツールは、Git Bashといいます。使い方をまとめています。

▲記事トップへ

目次

この記事の目次です。

1. Git Bashとは
2. Windows版の起動方法
3. Git Bashのヘルプドキュメント
4. 共通コマンド
5. Gitコマンド
- Gitユーザ情報の登録(git config --global user.name/user.email)
- Gitの設定確認(git config --list)
- Gitの設定の削除(git config --unset)
- リポジトリの新規作成(git init)
- リポジトリの状態確認(git status)
- ステージングエリアに追加する(git add)
- ファイルをコミットする(git commit)
- ヒストリーを表示する(git log)
- ヒストリーと変更ファイルを表示する(git log --status)
- リポジトリ内のファイル一覧を表示する(git ls-files)
- 差分を確認する(git diff)
- 複製してリモートリポジトリを作る(git clone --bare)
- リモートリポジトリからクローンを作る(git clone)
- リモートリポジトリに変更を反映する(git push)
- リモートリポジトリと同期する(git pull)

もっと知識を広げるための参考
更新履歴

1. Git Bashとは

Git Bashは、Gitのコマンドラインツールです。

2. Windows版の起動方法

スタートメニューからGit Bashを探して起動することができます。 もしくはコマンドプロンプトやエクスプローラーのアドレスに「git bash」と入力してEnterを押すと起動できます。

起動時のGit Bash
起動時のGit Bash

3. Git Bashのヘルプドキュメント

git helpでGit Bashのヘルプドキュメントが表示できます。

$ git help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --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
   restore   Restore working tree files
   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
   diff      Show changes between commits, commit and working tree, etc
   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
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   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.
See 'git help git' for an overview of the system.

4. 共通コマンド

Git Bashではgitコマンド以外にも使える共通コマンドがあります。

cd:ディレクトリの変更

cdコマンドでディレクトリを変更できます。

$ cd /

pwd:現在のディレクトリの表示

pwdコマンドを実行すると現在いるディレクトリのパスが表示されます。

$ pwd
/

5. Gitコマンド

共通コマンド以外のGit Bashで使えるGitコマンドです。

Gitユーザ情報の登録(git config --global user.name/user.email)

Gitは、「誰がバージョン管理を利用しているか」を管理するので、ユーザ情報を登録しておく必要があります。

以下は、ユーザ情報登録の例(ユーザ名とメールアドレスの登録例)です。 ユーザ名とメールアドレスの部分をご自身のものに置き換えて実行します。

$ git config --global user.name "ユーザ名"
$ git config --global user.email "メールアドレス"

Gitの設定確認(git config --list)

Gitの設定は以下のコマンドで確認できます。

$ git config --list

Gitの設定の削除(git config --unset)

Gitの設定を間違えてしまった場合は、以下のコマンドで取り消せます。 キーの部分を取り消す設定項目に置き換えて実行します。例えばユーザ名の場合はuser.nameになります。

$ git config --unset キー

リポジトリの新規作成(git init)

リポジトリの新規作成はgit initコマンドで行えます。

以下は、C:\tmpにworking_dirというディレクトリを作成して、このディレクトリをリポジトリにする例です。

$ cd /c/tmp
$ mkdir working_dir
$ cd working_dir/
$ git init
Initialized empty Git repository in C:/tmp/working_dir/.git/

リポジトリの状態確認(git status)

git statusコマンドでリポジトリの状態が確認できます。

$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

この例は「git init」でリポジトリを作成した直後に「git status」コマンドを実行した出力です。

試しにtest.txtというファイルを置いて「git status」を実行すると以下のように表示されます。

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

nothing added to commit but untracked files present (use "git add" to track)

test.txtは「Untracked files:」となり、追跡対象ではない旨のメッセージが表示されます。 そうです。gitはファイルをリポジトリのディレクトリに配置するだけでは追跡管理されません。 管理対象にするには、リポジトリに追加する操作を行って、追跡管理対象として登録する必要があります。

ステージングエリアに追加する(git add)

gitはファイルをリポジトリのディレクトリに配置するだけでは追跡管理されません。 「git add」でリポジトリに追加する操作を行って、追跡管理対象として登録することができます。

$ git add test.txt

「git add」を行った後に、「git status」を実行すると以下のように表示されます。

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test.txt

ファイルが追跡対象として登録されました。 「git add」を行うと待合室(ステージングエリア)に格納された状態になります。

でもバージョン管理で重要なコメント付きの履歴は登録されていません。 履歴を残すには別途コミット操作が必要です。

ファイルをコミットする(git commit)

「git add」でステージングエリアに格納されるとコミットできます。 Gitのコミットは「git commit」で行えます。

test.txtを「はじめてのコミット」とコメントをつけて、コミットした例です。

$ git commit -m "はじめてのコミット"
[master (root-commit) 868bedf] はじめてのコミット
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

「-m」はMessageの略で、「868bedf」はSHA1 IDです。 「100644」はファイルのアクセス権です。

「git status」の結果は以下です。クリーンな状態になりました。

$ git status
On branch master
nothing to commit, working tree clean

ヒストリーを表示する(git log)

「git log」でGitリポジトリのヒストリー(コミット履歴)を表示できます。

$ git log
commit 868bedfd82ce236f5a868e6e311df995d2320a60 (HEAD -> master)
Author: <ユーザ名> <メールアドレス>
Date:   Tue Sep 14 16:22:26 2021 +0900

    はじめてのコミット

ヒストリーと変更ファイルを表示する(git log --status)

「git log」に「--status」オプションをつけるとヒストリーに加えて変更ファイルも表示できます。

$ git log --stat
commit 868bedfd82ce236f5a868e6e311df995d2320a60 (HEAD -> master)
Author: <ユーザ名> <メールアドレス>
Date:   Tue Sep 14 16:22:26 2021 +0900

    はじめてのコミット

 test.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

リポジトリ内のファイル一覧を表示する(git ls-files)

リポジトリ内のファイルのリストは「git ls-files」で表示できます。

$ git ls-files
test.txt

差分を確認する(git diff)

「git diff」でどこに変更があったか差分が確認できます。

たとえば、test.txtに「Hello World!!」と追記して保存すると「git status」で以下のように表示されます。

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

「modified:test.txt」と表示され、test.txtに変更があったことがわかりますね。 この状態で「git diff」を実行すると以下のようにコミットされたファイルと現在のファイルの記述の差が表示されます。

$ git diff
diff --git a/test.txt b/test.txt
index e69de29..9369771 100644
--- a/test.txt
+++ b/test.txt
@@ -0,0 +1 @@
+Hello World!!

複製してリモートリポジトリを作る(git clone --bare)

「git clone --bare」で作成したリポジトリをもとにリモートリポジトリ(ベアリポジトリ)を作成することができます。

以下は同じPC内で複製によるリモートリポジトリを作成する例です。

$ pwd
/c/tmp

$ ls
working_dir/

「C:\tmp」にこれまでに作成した「working_dir」がある状態で以下のようにコマンドを実行します。

$ git clone --bare working_dir origin.git

リモートリポジトリができました。

$ ls
origin.git/  working_dir/

リモートリポジトリからクローンを作る(git clone)

「git clone <リモートリポジトリ> <作成するローカルリポジトリ>」でリモートリポジトリからクローンが作れます。

$ git clone C:/tmp/origin.git working_clone_dir
Cloning into 'working_clone_dir'...
done.

複製が行えました。

$ cd working_clone_dir/
$ git log
commit 868bedfd82ce236f5a868e6e311df995d2320a60
Author: <ユーザ名> <メールアドレス>
Date:   Tue Sep 14 16:22:26 2021 +0900

    はじめてのコミット

リモートリポジトリに変更を反映する(git push)

「git push」でリモートリポジトリに変更が反映できます。

まずローカルリポジトリで変更をコミットします。ここではtest2.txtというファイルを追加します。

$ cd working_clone_dir/
$ git add test2.txt
$ git commit -m "ファイルの追加"
[master 6541668] ファイルの追加
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt

「git push」でリモートリポジトリに変更を反映します。

$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 285 bytes | 285.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)

リモートリポジトリと同期する(git pull)

「git pull」でリモートリポジトリと同期することができます。

別のリポジトリ(working_clone_dir)から変更されたリモートリポジトリ(origin.git)の状態でリポジトリ(working_dir)を更新してみます。

$ cd working_dir
$ ls
test.txt

同期前はtest.txtのみです。

$ git pull C:/tmp/origin.git master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From C:/tmp/origin
 * branch            master     -> FETCH_HEAD
Updating d1897b2..6541668
Fast-forward
 test2.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt

「git pull」を行うとリモートリポジトリの変更が反映されます。

$ ls
test.txt  test2.txt

もっと知識を広げるための参考

もっと知識を広げるための参考です。

Git

Gitとは、分散型バージョン管理システムです。読み方は「ジット」ではなく「ギット」です。Gitとはどのようなものかまとめています。

詳細

更新履歴

更新履歴になります。

戻る

スポンサーリンク

サイト内のページ

言語
C・C++ /HTML /Java /JavaScript /PHP /シェルスクリプト

開発環境
Ant /Burp /Eclipse /Fiddler /gcc /gdb /Git /g++ /JDK /JMeter /JUnit /Teraterm /ZAP

技術・仕様
Ajax /CORBA /Jakarta EE(旧称J2EE、Java EE) /JNI

ライブラリ/Framework/CMS
bootstrap /jQuery /FuelPHP /Lucene /MyBatis /Seasar2 /Spring /Struts /WordPress

Web API
Google Maps

ITインフラOSとミドルウェア
Linux /Windows /シェル
ActiveMQ /Tomcat /MariaDB /MySQL /Nagios /Redis /Solr

ITインフラサーバー
公開Webサーバー

ITインフラネットワーク
プログラミング /構築

ITインフラセキュリティ
公開サーバーのセキュリティ

PC製品
ZOTAC

SI
ホームページの作り方

その他
IT用語 /ITスキル体系

スポンサーリンク

関連サイト内検索ツール

zealseedsおよび関連サイト内のページが検索できます。

IPアドレス確認ツール

あなたのグローバルIPアドレスは以下です。

3.131.13.194

HTMLの表示色確認ツール

パスワード生成ツール

文字数のプルダウンを選択して、取得ボタンを押すと「a~z、A~Z、0~9」の文字を ランダムに組み合わせた文字列が表示されます。

ここに生成されます。

スポンサーリンク