【自分用メモ】gitで色々詰まったときにみる

自分の.gitconfig

github.com

取り消す系

直前のcommitをやり直す

$ git add -A
$ git commit --amend -m "Commit message"

git commitをやり直しする&取り消しする(「git commit --amend」と「git reset」)

addを取り消す

$ git reset HEAD <FILENAME>

git add の取り消し方法と、関連コマンドまとめ

masterへのpushを取り消す

# まずは以下2つのコマンドで特定のコミットまで戻る
$ git log --oneline # ログの番号確認
$ git reset --hard <上で確認した戻りたいcommitの番号>


$ git log --oneline # 目的の箇所まで戻れているか確認
$ git push -f

↓が詳しかった

【git】git pushを取り消す - tweeeetyのぶろぐ的めも

エラー対処

push時にerror: failed to push some refs to

$ git push origin :<BRANCH>
$ git push origin <BRANCH>

:をつけるとremoteのブランチをDeleteするということになる。 これはローカルブランチが絶対に正しいときに使う。

その他の操作

新規ファイルをstashする

# ✕
$ git stash
No local changes to save

# ◯
$ git stash -u

新規フォルダをstashする

# ✕
$ git status
On branch refactor-question-cron
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   db/sql/init.sql

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/


$ git stash -u
$ git status
On branch refactor-question-cron
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    sns_manager/
    # ↑フォルダがstashされてない

# ◯
$ git stash --include-untracked
$ git status
On branch refactor-question-cron
nothing to commit, working tree clean

ブランチにmasterの内容を反映させる

$ git checkout master
$ git pull origin master
$ git checkout <BRANCH_NAME>
$ git rebase <BRANCH_NAME>

Gitで開発ブランチにmasterの内容を反映させる方法 (git rebase)

ブランチを作り忘れた

$ git stash
$ git stash list
$ git checkout -b <BRANCH_NAME>

$ git stash pop # 実行後適用した状態は削除される
$ git stash apply # 削除されない

まだcommitしてない場合は以下でおk。

$ git checkout -b <BRANCH_NAME>

ブランチを作り忘れた時 - Qiita

特定のブランチをクローンする

$ git clone -b ブランチ名 リポジトリのアドレス

deleteしたファイルをstageにaddしたい

$ git add <FILENAME> --update

Git 、削除したファイルが stage に上手く上がらない時 - CHROMA

git logを見やすくしたい

# いい感じのコマンドを「git log」に登録して使えるようにする
$ git config --global alias.tree 'log --graph --all --format="%x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(magenta reverse)%d%Creset %s"'

git log を見やすくする - Qiita

conflictしたファイルの一覧を表示させる

$ git diff --name-only --diff-filter=U

# エイリアスを設定
$ git config --global alias.conf '!git ls-files -u | cut -f 2 | sort -u'

Git で競合ファイル一覧を楽に見る方法 - Qiita

設定したエイリアスを確認する

$ git config --global --list | grep alias\.