0
4

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完全攻略チートシート|基本操作から高度なテクニックまで

Posted at

はじめに

Gitは開発者にとって欠かせないバージョン管理システムです。本記事では、よく使うGitコマンドをチートシート形式で紹介します。

基本操作

Gitの設定

# ユーザー名とメールアドレスの設定
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 設定の確認
git config --list

リポジトリの作成とクローン

# 新しいリポジトリの作成
git init

# 既存のリポジトリをクローン
git clone <repository_url>

ステージングとコミット

# 変更をステージング(追加)
git add <file>

# すべての変更をステージング
git add .

# コミットを作成
git commit -m "コミットメッセージ"

ブランチ操作

ブランチの作成と切り替え

# ブランチの作成
git branch <branch_name>

# ブランチの切り替え
git checkout <branch_name>

# 作成と同時に切り替え
git checkout -b <branch_name>

ブランチの統合(マージ)

# ブランチを現在のブランチにマージ
git merge <branch_name>

ブランチの削除

# ローカルブランチの削除
git branch -d <branch_name>

# リモートブランチの削除
git push origin --delete <branch_name>

リモートリポジトリ操作

リモートの登録と確認

# リモートリポジトリの追加
git remote add origin <repository_url>

# リモートリポジトリの確認
git remote -v

プッシュとプル

# リモートにプッシュ
git push origin <branch_name>

# 最新の変更を取得
git pull origin <branch_name>

フェッチとマージ

# リモートの変更を取得(自動でマージしない)
git fetch origin

# 取得した変更をマージ
git merge origin/<branch_name>

変更の管理

変更の確認

# 変更の一覧を確認
git status

# 変更の詳細を確認
git diff

変更の取り消し

# ステージング前の変更を取り消し
git checkout -- <file>

# ステージング済みの変更を取り消し
git reset HEAD <file>

ログの確認

# コミットログを表示
git log

# 簡潔なログ表示
git log --oneline

その他の便利なコマンド

# 最新のコミットを上書き(要注意)
git commit --amend

# コミット履歴を操作(要注意)
git rebase -i HEAD~n

# 強制プッシュ(慎重に)
git push origin <branch_name> --force

高度な操作

一時的に変更を退避(stash)

# 作業中の変更を一時保存
git stash

# スタッシュ一覧を表示
git stash list

# 最新のスタッシュを適用
git stash apply

# スタッシュを削除
git stash drop

特定のコミットを別のブランチに適用(cherry-pick)

# 指定したコミットを現在のブランチに適用
git cherry-pick <commit_hash>

バグ修正のためのバイセクト(bisect)

# バグのあるコミットを特定
git bisect start
git bisect bad   # 現在のバージョンをbadとする
git bisect good <commit_hash>  # 既知の正常なバージョンを指定

リファレンスログの確認(reflog)

# ローカルの操作履歴を表示
git reflog

まとめ

Gitの基本的な操作をチートシートとしてまとめました。日々の開発で活用し、効率的にバージョン管理を行いましょう!

0
4
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
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?