1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git 基本操作

1
Posted at

Git基本操作ガイド

はじめに

Gitはバージョン管理システムとして広く使われているツールです。この記事では、初心者の方でもすぐに使えるGitの基本操作をまとめました。

目次

  1. Gitの初期設定
  2. リポジトリの作成
  3. ファイルの追加とコミット
  4. 変更履歴の確認
  5. ブランチ操作
  6. リモートリポジトリとの連携
  7. よく使うコマンド一覧

1. Gitの初期設定

Gitを初めて使う場合は、ユーザー名とメールアドレスを設定します。

git config --global user.name "あなたの名前"
git config --global user.email "your.email@example.com"

設定内容を確認するには以下のコマンドを実行します。

git config --list

2. リポジトリの作成

新規リポジトリの作成

新しいプロジェクトでGitを使い始める場合は、プロジェクトのディレクトリで以下を実行します。

git init

既存リポジトリのクローン

既存のリポジトリをコピーする場合は以下のコマンドを使います。

git clone https://github.com/username/repository.git

3. ファイルの追加とコミット

ファイルのステージング

変更したファイルをステージングエリアに追加します。

# 特定のファイルを追加
git add ファイル名

# すべての変更をまとめて追加
git add .

# 特定の拡張子のファイルを追加
git add *.js

コミット

ステージングされた変更をリポジトリに記録します。

git commit -m "コミットメッセージ"

addとcommitを一度に行う場合(追跡済みファイルのみ):

git commit -am "コミットメッセージ"

.gitignoreの設定

管理したくないファイルやディレクトリを.gitignoreファイルに記述します。

# 例
node_modules/
*.log
.env
.DS_Store

4. 変更履歴の確認

ステータスの確認

現在の変更状況を確認します。

git status

コミット履歴の確認

コミットの履歴を表示します。

# 詳細な履歴
git log

# 1行で簡潔に表示
git log --oneline

# グラフ形式で表示
git log --graph --oneline --all

差分の確認

変更内容の詳細を確認します。

# ワーキングディレクトリとステージングエリアの差分
git diff

# ステージングエリアと最新コミットの差分
git diff --staged

# 特定のファイルの差分
git diff ファイル名

5. ブランチ操作

ブランチの作成

新しいブランチを作成します。

git branch ブランチ名

ブランチの切り替え

指定したブランチに切り替えます。

git checkout ブランチ名

# または(Git 2.23以降)
git switch ブランチ名

ブランチの作成と切り替えを同時に実行

git checkout -b ブランチ名

# または(Git 2.23以降)
git switch -c ブランチ名

ブランチの一覧表示

# ローカルブランチの一覧
git branch

# リモートブランチを含む一覧
git branch -a

ブランチのマージ

現在のブランチに他のブランチをマージします。

# マージしたいブランチに切り替え
git checkout main

# 指定したブランチをマージ
git merge 機能ブランチ名

ブランチの削除

# マージ済みブランチの削除
git branch -d ブランチ名

# 強制削除
git branch -D ブランチ名

6. リモートリポジトリとの連携

リモートリポジトリの追加

git remote add origin https://github.com/username/repository.git

リモートリポジトリの確認

git remote -v

プッシュ(push)

ローカルの変更をリモートリポジトリに送信します。

# 初回プッシュ時
git push -u origin main

# 2回目以降
git push

プル(pull)

リモートリポジトリの最新状態をローカルに取り込みます。

git pull origin main

# または
git pull

フェッチ(fetch)

リモートの変更を取得しますが、マージはしません。

git fetch origin

7. よく使うコマンド一覧

コマンド 説明
git init 新規リポジトリを作成
git clone <URL> リポジトリをクローン
git status 現在の状態を確認
git add <file> ファイルをステージング
git commit -m "message" 変更をコミット
git push リモートにプッシュ
git pull リモートから取得してマージ
git branch ブランチ一覧を表示
git checkout <branch> ブランチを切り替え
git merge <branch> ブランチをマージ
git log コミット履歴を表示
git diff 変更の差分を表示

便利なTips

直前のコミットメッセージを修正

git commit --amend -m "新しいコミットメッセージ"

ファイルをステージングから取り消し

git reset HEAD ファイル名

変更を取り消し(ワーキングディレクトリ)

git checkout -- ファイル名

# または(Git 2.23以降)
git restore ファイル名

参考リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?