Skip to content

Instantly share code, notes, and snippets.

@gatosyocora
Last active May 25, 2021 17:22
Show Gist options
  • Save gatosyocora/b4cbd232373c4bc6a8852486a3a8acab to your computer and use it in GitHub Desktop.
Save gatosyocora/b4cbd232373c4bc6a8852486a3a8acab to your computer and use it in GitHub Desktop.
忘れがちなgitのコマンドをまとめる
  • ファイルの変更はそのままで直前のコミットを取り消す(Windows)

    $git reset --soft "HEAD^"

  • gitignoreの変更を適用(管理しなくなったファイルの履歴も残さない(push前))

    1. $git rm -r --cached .
    2. $git add .
    3. $git commit --amend --no-edit
  • ファイルを履歴ごと削除する

    https://gist.github.com/ktx2207/3167fa69531bdd6b44f1

    1. $git checkout testbranch
    2. $git filter-branch --tree-filter "rm -f <ファイルパス>" HEAD -- .
    3. $git filter-branch -f --tree-filter "rm -f <ファイルパス>" HEAD --all
    4. $git reflog expire --expire=now --all
    5. $git gc --aggressive --prune=now
    6. $git push --force origin master
  • 一時的にchangedを退避させる/戻す

    • git stash push : 退避
    • git stash pop : 戻す
  • 後からコミットに含める/修正する

    1. stageに何もない状態にする
    2. git rebase -i HEAD~[さかのぼるコミット数]
    3. vimでコミット一覧が表示される
    4. iキーで入力モードにして、対象のコミットのpickeまたはeditに変更 -> ciwe
    5. Escキーで入力モードを終わって、:wqで保存
    6. ファイルを変更してgit add hoge -> git commit --amend
    7. push済みならgit push -fで上書きする

git commit --amend --no-edit

  • git push -forceのpull版

    1. $ git fetch
    2. $ git reset --hard origin/<branch_name>
  • submoduleの更新

    1. $ git submodule init 既にしたことあったらなくてよい
    2. $ git submodule update --remote
  • submoduleの更新(任意のコミットへ)

  1. 上記submoduleの更新1と2をする
  2. cd hoge でsubmoduleのリポジトリへ移動
  3. git checkout <commitID>
  • submodule内のファイルの更新
  1. cd <submoduleのフォルダ>
  2. git add .
  3. git commit -m "<コミットメッセージ>"
  4. git push origin HEAD:<ブランチ名>
  • tagの削除
    • ローカル: git tag -d <tag名>
    • リモート: git push --delete origin <tag名>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment