0
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コマンドの備忘録

0
Last updated at Posted at 2026-03-29

Gitコマンドの備忘録

随時更新予定です。なかなか覚えられないGitコマンドを少しずつ追加していこうと思います。

1. 基本コマンド

Git Bash
# ローカルリポジトリをブランチ名 main で作成
git init -b main

# ローカルリポジトリの状態を確認
git status

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

# コミットを作成
git commit -m "Initial Commit"

# リモートリポジトリと紐づけ (GitHubの場合)
git remote add origin https://github.com/ユーザ名/リポジトリ名.git

# プッシュ (メインブランチに)
git push origin main

GitHub CLI を利用して、プッシュ時にリモートリポジトリを作成することもできます。

Git Bash
gh repo create リポジトリ名 --private --source=. --remote=origin --push

2. 元に戻す

2.1. コミット前の変更

Git Bash
# ⚠️アンステージ状態の変更を破棄する
git checkout .

# すべてのステージ済み変更をアンステージする
git restore --staged .

2.2. プッシュ前のコミット後の変更を取り消す

Git Bash
# コミットを取り消してステージは残す
git reset --soft HEAD~1

# コミットを取り消してステージも解除
git reset HEAD~1

# ⚠️コミットもステージも取り消して変更破棄
git reset --hard HEAD~1

3. ブランチ

Git Bash
# 別の既存ブランチに移動
git checkout ブランチ名

# 今あるブランチの変更を維持して、新しいブランチを作成 & 切替
git switch -c 新しいブランチ名

4. 設定

4.1. 現在の設定を確認する

Git Bash
# グローバル設定を確認
git config --global user.name
git config --global user.email

# ローカル設定を確認
git config --local user.name
git config --local user.email

4.2. 現在の設定を変更

Git Bash
# グローバル設定
git config --global user.email "your_email"
git config --global user.name "your_name"

# ローカル設定
git config --local user.email "your_email"
git config --local user.name "your_name"

4.3. 開発環境が Windows 本番環境が Linux の場合

開発環境での改行文字を CRLF、本番環境の改行文字を LF としてチーム内で統一したい場合に便利な設定を紹介します。まず Git 管理のルートディレクトリに .gitattributes ファイルを次のように追加します。

.gitattributes
# リモートリポジトリの設定 (内部保持の改行文字を `LF` で統一)
* text eol=lf

# 改行文字が変更されたら困るファイル拡張子を登録
*.png binary
*.jpg binary
*.bmp binary
*.ico binary
  • eolEnd Of Line : (改行) の省略語

画像・音声などのバイナリファイルは、改行コードを別の意味で利用しているため、改行コードが勝手に変更されると、ファイルが壊れます。

改行コード
CR : \r : 0x0d
LF : \n : 0x0a

上記のファイル .gitattributes をリモートリポジトリにプッシュすると、リモートリポジトリ内部での改行文字を LF で統一してくれます。

次に Windows側の作業環境の推奨設定を示します。次のコマンドを実行してください。

Git Bash
git config --local core.autocrlf true

このコマンドにより、pushするとき CRLFLF、pullするとき LFCRLF になります。コマンド実行による設定値は .git/config に保存されます。

.git/config
[core]
  autocrlf = true
0
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
0
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?