1
1

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 config --global user.name "name"
# メールアドレスの設定
$ git config --global user.email "email"

リポジトリ作成

$ git init
# Gitで管理しないファイルは、
# .gitignoreファイルを作成して、そこに無視したいファイルやディレクトリ名を書く

リモートリポジトリをクローンする

$ git clone リモートリポジトリのパス

リポジトリの状態を確認する

$ git status

ファイルやディレクトリをステージングエリアに登録する

$ git add ファイル…
$ git add ディレクトリ…
# プロジェクトのすべてのファイルとディレクトリをGitの管理下に置く
$ git add .

ステージの内容をコミットする

$ git commit
# 一行のコミットメッセージでコミットする
$ git commit -m "コミットメッセージ"

コミットの履歴を見る

$ git log
$ git log --online
$ git log -- ファイル名
$ git log --all --graph

差分を表示する

# 作業ツリーとステージの差を表示する
$ git diff

# ステージとHEADの差を表示する
$ git diff --staged

# 作業ツリーとHEADの差を表示する
$ git diff HEAD

# コミットの間で変更した差を表示する
$ git diff コミット1 コミット2

# ファイルを指定して、そのファイルについての差分を表示する
$ git diff コミット1 コミット2 -- ファイル

# 1つ前のHEADとの比較
$ git diff HEAD^ HEAD

# 2個前のコミットとの比較
$ git diff HEAD~2 HEAD

ローカルリポジトリでの操作を取り消す

# ワークツリーの変更を取り消す
$ git restore ファイル

# ステージングエリアの登録を取り消す
$ git restore --staged ファイルパスまたはディレクトリパス

# ワークツリーとステージングエリアの変更を同時に取り消す
$ git restore --staged --worktree ファイルパスまたはディレクトリパス

コミットの情報を表示する

# 直近のコミット内容を表示
$ git show

# 指定したコミットの内容を表示
$ git show コミット

ステージの内容を見る

# ステージ内になるファイルの一覧を表示する
$ git ls-files

# 詳細情報も併せて表示する
$ git ls-files -s

ファイル名、ディレクトリ名を修正

$ git mv 元の名前 新しい名前

ファイルやディレクトリをステージから取り除く

$ git rm ファイル
$ git rm -r ディレクトリ

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

$ git remote -v

ブランチ操作

# ローカルのブランチの確認をする
$ git branch

# リモートのブランチを確認する
$ git branch -r

# すべてのブランチを確認する
$ git branch -a

# ブランチ作成
$ git branch 作成するブランチ名

# ブランチ切り替え
$ git switch 切り替え先のブランチ名

# ブランチ作成と同時にスイッチする
$ git switch -c 作成し、切り替えたいブランチ名

変更をリモートリポジトリに反映する

$ git push
$ git push プッシュ先のリモートリポジトリの名前 プッシュするブランチ名
#例)$ git push origin testbranch

リモートからローカルに反映するコマンド

$ git pull プル先のリモートリポジトリの名前 プルするブランチ
#例)$ git pull origin main

$ git fetch フェッチ先のリモートリポジトリ
#例)$ git fetch origin

# ローカルリポジトリに存在しないブランチを取得する
$ git fetch origin
$ git switch ブランチ名

マージする

# 今いるブランチにマージする
$ git merge マージしたいブランチ名

過去のコミットに移動する

$ git checkout コミット名
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?