知っていると便利な使い方
ステージの関連操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
### git addを取り消す $ git status -s ;; M file $ git reset HEAD file # 誤って不要なファイルをgit addしてしまったときに用いる ### ファイルをgitから削除する $ rm file $ git status -s ;; D file $ git rm file # 本来はgit rm fileだけで削除できるが、記載コマンドのような確認を挟んだ削除手順を推奨する ### git rmを取り消す $ git reset HEAD file $ git checkout file # file : fileにgit rmしたファイルのパスを指定する |
コミットの関連操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
### コミットメッセージのAutherを自分に設定する $ git config --local user.name rite $ git config --local user.email webmaster@rite.jp # 変更したら.git/configを確認するとよい ### コミットメッセージを複数行挿入する $ git commit -F- <<. >タイトル > # 空行をいれる >作業内容 >. # "."でEnd Of Message # <<. : "."の代わりにEOMなどを使っても良い ### コミットを上書きする $ git commit --amend # --amend : --amendで操作できるのは直前のコミットだけなので注意する # git commit --amend -m "変更メッセージ"でコミットメッセージの上書きもできる ### コミットを取り消す $ git log --oneline | head -n 1 ;; 5f71d01 Gitの使い方を編集 $ git reset --soft HEAD^ # HEAD^ : HEAD^はひとつ前のコミットを意味するので、直前のコミットである「5f71d01…」を取り消すことになる ### マージリクエスト後、マージ前でもつづけてコミットする $ git commit -m "a" $ git push origin foo # gitlab上でマージリクエストを出す $ git commit -m "b" $ git push origin foo # このやり方でもa,bの各コミットはきちんと反映される # pushした瞬間から、gitlab上で自動更新されていく |
ブランチの関連操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
### リモートブランチを確認する $ git branch -r # ただしgitlabから削除されたブランチは残り続ける ### すべてのブランチを確認する $ git branch -a # リモートブランチはremotes/、トラッキングブランチはorigin/のprefixをつけて表示される ### ブランチを切り替える $ git checkout bar # bar : bar部分に切り替え先のブランチを指定する ### ブランチを削除する $ git branch -d foo ### リモートブランチを削除する # github上から削除した方が簡単かつお手軽 |