Git

基本操作

Git

init、clone、add、commit、status

初期設定

リポジトリの作成とグローバル設定

init.sh bash
# グローバル設定
git config --global user.name  "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"   # VSCode
git config --global init.defaultBranch main
git config --global pull.rebase false            # merge戦略
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate"

# 設定確認
git config --list
git config user.name

# リポジトリ初期化
git init
git init my-project  # ディレクトリ作成+初期化

# リモートからクローン
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-name  # フォルダ名指定
git clone --depth 1 https://github.com/user/repo.git # shallow clone

ステージングとコミット

add、commit、status、diff

commit.sh bash
# 状態確認
git status
git status -s  # 短縮表示

# ステージング
git add file.txt          # 特定ファイル
git add src/              # ディレクトリ
git add .                 # 全変更ファイル
git add -p                # 変更の一部だけ(パッチモード)
git add -u                # 追跡済みファイルのみ

# 差分確認
git diff                  # ワーキングツリー vs ステージ
git diff --staged         # ステージ vs 最新コミット
git diff HEAD~1 HEAD      # コミット間の差分

# コミット
git commit -m "feat: ユーザー認証を追加"
git commit -am "fix: バグ修正"  # add + commit(追跡済みファイルのみ)
git commit --amend            # 直前のコミットを修正(未pushのみ)
git commit --amend --no-edit  # メッセージを変えずに修正

# .gitignore
echo "node_modules/" >> .gitignore
echo ".env" >> .gitignore
git rm --cached file.txt  # 追跡から除外(ファイルは残す)