LoginSignup
0
0

More than 1 year has passed since last update.

現場でよく使う Git コマンド集

Last updated at Posted at 2020-06-16

現場でコピペシリーズ Git版リリースです。

2.23辺りからの追加変更(以下はcheckoutの代替)

Checkout従来の複数機能の整理

ブランチ作成・切替
修正取消
コミット取消・修正

ファイル取消

git restore <ファイル名>

ステージング取消

git reset

コミット取消

git revert <コミットID>  # 指定コミットの取消
git restore --source <コミットID> <ファイル名> # 特定のコミットに変更

従来機能

リモート設定

git remote -v        # リモート確認
git remote add origin    # リモート名追加
git remote rm <リモート名>  # リモートの削除
git remote set-url           # リモート設定 
git fetch origin ブランチ名 # リモート最新情報でローカルorigin/master更新

Clone & Pull

git clone <リモートリポジトリURL>/<クローン先のディレクトリ>
git config pull.rebase false   # merge
git config pull.rebase true    # rebase
git config pull.ff only        # fast-forward only

コンフリクト解消後pull

git reset --hard HEAD
git pull origin master --rebase

ブランチ

git branch -a               # 全てのブランチ閲覧
git checkout <切替えるブランチ>   # ブランチ切替(古い)
git switch   <切替えるブランチ>    # ブランチ切替
git checkout -b  <作成ブランチ名> # ブランチ作成
git checkout -b  <作成ブランチ名> <リモートブランチ> # リモートブランチから作成
git switch -c <作成ブランチ>
git push origin     <追加するリモートブランチ> # リモートにブランチ追加
git push -u origin  <作成するリモートブランチ名>
git branch -d    <マージ後の削除するローカルブランチ>
git branch -D    <マージしてないローカルブランチ削除>
git branch -m       <旧ブランチ> <新ブランチ>
git push origin    :<削除するリモートブランチ> 
git push origin     <新ブランチ>

マージ (ブランチのコミット履歴を残す(並行))

git checkout master
git merge <作業ブランチ>

リベース (コミット履歴を一本化する)

git chekout <作業ブランチ>
git rebase master # ブランチ先頭 + マスタ
# rebase 後に
git merge          # マスタにブランチを取り込む

変更取消(ワーキングツリー(ステージング前))

git checkout .
git restore .

ステージング

git add .          # 全てステージング
git add -a                  # 変更したもの全て
git add -n           # ステージング対象表示
git add -u <ステージング対象ファイル>

ステージング取消

git reset --hard HEAD    # ワーキングツリ+インデックス+HEAD 取消し
git reset --mixed           # インデックス+HEAD 取消し
git soft  --soft            # HEAD 取消し
git reset --soft HEAD^      # 直前のコミット取消し
git reset  --hard HEAD~n    # Ǹ個前まで戻す
git reset --hard ORIG_HEAD  # reset の取消
git restore --staged <ファイル名>
git rm --cached             # ステージング削除

###Commit & Push

git fetch origin <リモートブランチ名> 
                     # リモートから最新情報をローカル(origin/master)に持ってくる
git merge                   # ローカルorigin/master ローカルmasterマージ
git reflog                  # コミット履歴閲覧
git commit --amend          # メッセージ編集してコミット
git commit -m "メッセージ"   
git archive         # 変更のあったものだけ抽出
git push origin HEAD    #リモートにPush
git push oriigin <ブランチ> #リモートにPush

Commit 取消

git log
git revert <コミットID>
git checkout <コミットID>

Push 取消

git reset --hard HEAD   # ローカルコミット取消
git push -r origin <リモートブランチ>

タグ

git tag    <tag名>          # 最新のコミットにタグを付ける
git tag -a -m <メッセージ>
git tag 
git tag -l 'tag.*'          # 正規表現でtagを検索
git push   <tag名>          # タグ反映
git show   <tag名>          # タグ確認
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