【初心者向け】はじめてのGitコマンド - add、commit、pushの基本操作をマスターしよう

Tech Trends AI
- 3 minutes read - 519 wordsはじめに:Gitの基本的な流れを理解しよう
プログラミングを始めたばかりの方にとって、Gitは「難しそう」「コマンドがたくさんあって覚えられない」と感じるかもしれません。しかし、実際にはたった3つのコマンド(add、commit、push)を覚えるだけで、基本的なファイル管理ができるようになります。
この記事では、Git初心者の方が最初に覚えるべき3つのコマンドについて、具体例を交えながら分かりやすく解説します。
Gitの3つの重要な場所
コマンドを学ぶ前に、Gitが管理している3つの場所について理解しましょう。
1. ワーキングディレクトリ(作業領域)
- あなたが実際にファイルを編集している場所
- 普段コードを書いているフォルダのことです
2. ステージングエリア(インデックス)
- コミット予定のファイルを一時的に保管する場所
- 「次回のコミットに含める変更」を準備する場所
3. リポジトリ(ローカル・リモート)
- 変更履歴が永続的に保存される場所
- ローカル:あなたのPC内のリポジトリ
- リモート:GitHubなどのサーバー上のリポジトリ
1. git add - ファイルをステージングエリアに追加
基本的な使い方
# 特定のファイルを追加
git add ファイル名
# 例:index.htmlを追加
git add index.html
# 複数ファイルを同時に追加
git add index.html style.css script.js
# すべての変更ファイルを追加
git add .
# 特定の拡張子のファイルをすべて追加
git add *.js
実例:新しいHTMLファイルを追加する場合
# 現在の状態を確認
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
# index.htmlをステージングエリアに追加
$ git add index.html
# 再度状態を確認
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: index.html
よく使うaddのパターン
# カレントディレクトリの全ファイル(推奨)
git add .
# より広範囲(サブディレクトリも含む)
git add --all
# 削除されたファイルも含めて全て
git add -A
2. git commit - 変更をリポジトリに記録
基本的な使い方
# コミットメッセージと一緒にコミット
git commit -m "コミットメッセージ"
# 例
git commit -m "初回のHTMLファイル作成"
良いコミットメッセージの書き方
推奨例:
git commit -m "ログイン機能を追加"
git commit -m "CSSスタイルを修正: ヘッダーの色を変更"
git commit -m "バグ修正: パスワードリセット機能の不具合を解決"
避けるべき例:
git commit -m "修正" # 何を修正したか分からない
git commit -m "test" # 内容が不明
git commit -m "あああ" # 意味不明
実例:ファイル変更をコミットする場合
# ファイルを編集後、addしてcommit
$ git add index.html
$ git commit -m "トップページのタイトルを変更"
[main 1a2b3c4] トップページのタイトルを変更
1 file changed, 2 insertions(+), 1 deletion(-)
# コミット履歴を確認
$ git log --oneline
1a2b3c4 トップページのタイトルを変更
5d6e7f8 初回のHTMLファイル作成
3. git push - ローカルの変更をリモートリポジトリに送信
基本的な使い方
# 基本のpush(初回設定後)
git push
# 詳細指定(初回や設定変更時)
git push origin main
# 初回pushの場合(上流ブランチの設定)
git push -u origin main
実例:GitHubにコードをアップロード
# 現在の状態確認
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
# リモートリポジトリにpush
$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/username/repository.git
5d6e7f8..1a2b3c4 main -> main
完全な作業フローの実例
実際の開発でよくある流れを見てみましょう。
シナリオ:Webページに新機能を追加
# 1. 現在の状況確認
$ git status
On branch main
nothing to commit, working tree clean
# 2. ファイルを編集(エディタで contact.html を作成)
# 3. 変更内容を確認
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
contact.html
# 4. ファイルをステージングエリアに追加
$ git add contact.html
# 5. 状態を再確認
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: contact.html
# 6. コミット実行
$ git commit -m "お問い合わせページを追加"
[main a1b2c3d] お問い合わせページを追加
1 file changed, 25 insertions(+)
# 7. リモートリポジトリにpush
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 445 bytes | 445.00 KiB/s, done.
To https://github.com/username/mywebsite.git
1a2b3c4..a1b2c3d main -> main
よくある間違いと対処法
1. addを忘れてcommitした場合
# エラーが表示される
$ git commit -m "ファイル追加"
On branch main
Untracked files:
newfile.js
nothing added to commit but untracked files present
# 解決方法
$ git add newfile.js
$ git commit -m "ファイル追加"
2. コミットメッセージを間違えた場合
# 直前のコミットメッセージを修正
git commit --amend -m "正しいコミットメッセージ"
3. pushでエラーが出る場合
# リモートの変更を先に取得
git pull
# その後にpush
git push
まとめ:3つのコマンドでGitの基本をマスター
Git操作の基本は、この3つのコマンドの繰り返しです:
git add: 変更したファイルをステージングエリアに追加git commit: ステージングエリアの変更をローカルリポジトリに記録git push: ローカルリポジトリの変更をリモートリポジトリ(GitHub等)に送信
覚えておきたい基本パターン
# 日常的な作業フロー
git add . # すべての変更をステージング
git commit -m "変更内容" # コミット実行
git push # リモートに送信
最初はこの3つのコマンドを確実に覚えて、慣れてきたら他のGitコマンド(git pull, git status, git logなど)も覚えていきましょう。
次のステップ
git statusでファイルの状態確認git logでコミット履歴の確認git pullでリモートから最新の変更を取得- ブランチ機能を使った並行開発
Gitは最初は難しく感じるかもしれませんが、この3つのコマンドを使いこなせれば、安全にコードを管理できるようになります。実際に手を動かして練習してみてください!