0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Git】よく使うコマンドまとめ

Last updated at Posted at 2020-03-11

はじめに

開発時に自分がよく使うgitのコマンドをまとめました。
コマンドを網羅しているわけではありませんので、ご了承ください。

開発スタート時に使うコマンド

リポジトリをローカルに複製する

git clone [リポジトリURL]

リポジトリURLはGithubで言うと、ここですね。
image.png

gitの設定を確認する

git config --list --[local|global|system]
  • system: システム全体
  • global: ユーザ全体
  • local: 対象リポジトリのみ

同じ設定項目が存在する場合、system < global < localの優先順位で適用されます。

出力はこんな感じです。

core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
:
:

設定変更

git config --global user.name [名前]
git config --global user.email [メールアドレス]
git config --global core.editor [エディタ名]
git config --global core.quotepath false
git config --global --add merge.ff false
git config --global --add pull.ff only

開発中に使うコマンド

ブランチを作成

git checkout -b [新しいブランチ名] [元のブランチ名]

今どうなっているか確認

git status

(何らかの編集をした後)ステージング

addする内容を確認する場合は、

git diff

で確認してから、

git add [コミットしたいファイルorディレクトリ]

git add の取り消し

git reset HEAD [ファイル名]

コミット

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

コミットの取り消し

git reset --hard HEAD^ # 直前のコミットの取り消し

--hard: ワーキングディレクトリの内容も取り消し
--soft: ワーキングディレクトリの内容はそのまま

打ち消しコミット

git revert [打ち消したいコミットのID]

追跡しているブランチを確認

git branch -vv

リモートブランチとの差分を確認

git diff [リモートブランチ名]..HEAD

リモートリポジトリに反映

git push -u origin [ブランチ名]

-uをつけると追跡ブランチとして設定されるので、次のpushからは、

git push

でOK。

リモートリポジトリの変更を取り込む前に差分を確認

git diff HEAD..[リモートブランチ名]

リモートリポジトリの変更を取り込む

git pull --rebase

ブランチの付け替え

git rebase --onto [新しい派生元ブランチ名] [現在の派生元ブランチ名] [付け替えるブランチ名]

開発(作業)の途中で別のブランチに切り替えなければならないとき

git stash

で変更を退避させてから、

git checkout [ブランチ名]

でブランチ切り替え。

stash(退避)した内容を確認する

git stash show -p stash@{N}

stash(退避)した内容を戻す

git stash apply

たまに使うコマンド

ブランチ削除

git branch -d [ブランチ名]

ブランチ強制削除

git branch -D [ブランチ名]

リモートリポジトリ追加

git remote add [リポジトリ名] [リポジトリURL]

コミット履歴を確認

git log
git log --oneline # 1行で簡易表示
git log --merges

HEADの履歴を確認

git reflog

コミットを取り込む

git cherry-pick [取り込むコミットID]
git cherry-pick [コミットID 1]..[コミットID 2] # コミットID 1~2を取り込む(※コミットID 1は含まないので、コミットID 1を取り込みたいときはコミットID 1の一つ前のコミットを指定する)
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?