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?

More than 3 years have passed since last update.

Git コマンド メモ

Last updated at Posted at 2022-04-14

git branch

  • local branch の 一覧表示

git switch

  • branch の 移動

git restore .

  • add 前の変更を無かったことにする

git restore —staged

  • add 済み( staging 中) の 変更を取り消す

git fetch

  • リモートの状態取得。branch 一覧の更新によく使う

git log

  • コミットの履歴を表示

git log --graph --oneline

  • コミットの流れをグラフィカルに表示

git show

  • コミット毎の差分を表示

git describe <参照>

  • 直近のタグから <参照>までの差分を表示
  • <参照> には ブランチ・タグ指定・コミットハッシュなどが指定可能

git status -s

  • 下記の3種のファイルの一覧表示
    • git add されているけどまだ git commit されていないファイルの一覧
    • 編集・変更・削除されているが、まだ git add されていないファイルの一覧
    • Git管理されていない、かつ .gitignore で管理除外対象にもされていないものの一覧

git pull

  • リモートから取得

    git pull —rebase

    • -f で プッシュされたコミットのあるブランチで、最新状態へ

git add -p

  • ファイル内の部分毎にステージング
    • y : この塊をステージングする
    • n : この塊はステージングしない
    • s : この塊を分割する
    • q : 終了

git commit -m "commit message."

  • ステージング中のファイルをコミットする

git push

  • コミットをプッシュする

git stash

  • コミットしていない変更(add済み 未add 両方)を退避する

    git stash apply

    • 退避した作業を、現在のbranchに戻す

    git stash list

    • 一覧

    git stash drop

    • 退避したものの削除

git cherrypic

  • 特定のコミットを現在のbranch に取り込む

git commit --amend

  • ステージングを、直近のコミットに反映する
    • 使用例:直近のコミットメッセージを修正する
    • 作業途中で保存用に行ったコミットに、続きの作業内容を追加する
    • 直近でない commit を操作する場合には、↓2つの進め方がある
      • 後述の git rebase -i と組み合わせて、一旦対象のcommit を branch 末尾にする
      • cherry-pick を順に進めていく中で、amend する

git revert

  • 指定の commit を打ち消す commit を作成する
    • ソースコード上は変更はなかった体となる。
    • リモートの master ブランチにマージされたものを取り消す際はこのコマンド

git rebase -i

  • -i は インタラクティブの i

  • いくつかのコミットをまとめて一つのコミットにする

    <commit-id> の 指定例

    • git rebase -i master
      • master からの分岐commit から編集
    • git rebase -i HEAD~~~~
      • HEAD が示すcommit から ~ の数遡った所から編集
    • git rebase -i コミットハッシュ値
      • コミットハッシュ値のコミットから HEAD までを編集

    エディタが開いて指定モードに入ってから

    • p, pick : そのコミットを使用する
    • r, reword : コミットを使用するが、メッセージを編集する
    • e, edit : コミットを使用するが、コミット内容を変更する
    • s, squash : squash 直前のコミットにまとめる、コミットメッセージを編集する
    • f, fixup : fixup 直前のコミットにまとめる、コミットメッセージは編集しない
    • d, drop: コミットを使用しない

git merge

  • コンフリクト解消

Git - ブランチとマージの基本

git rebase

  • branch の 起点の変更

Git - リベース

pull request の コンフリクト解消

GitHub の Webページから行う方法

GitHub でのマージ コンフリクトを解決する

ローカルのコンソールで行う方法

step 0. 状況:pull request を 作成したが コンフリクトが発生してた

step 1. ローカルの master を最新にして、ローカルのmaster に対して merge

git switch master
git pull
git switch <my-branch>
git merge master

step 2. コンフリクトが発生していたら、対象ファイルを修正する

step 3. 修正を ステージング、コミットとして push

# ブランチは <my-branch> のまま
git add -p
git commit -m "commit message."
git push origin <my-branch> 

step 4. 修正がpull request に反映されているはず

参考情報

こわくないGit

こわくない Git

サンドボックスで学べる

Learn Git Branching

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?