今回もこちらのUdemyで学習した内容をまとめます!
(自分で調べた内容を加えています)

Gitコマンドまとめ最終回です!
stash
Aという機能の開発中に急遽Bを先に開発してほしいとの指示が…!
そんな時Aの作業分をどこかに退避して、Bが終わった後に再度手元に戻したい。
なんてことが起こるのです。そんな時に使えるのが「stash」!
・作業ブランチでの変更を一時的にstashに退避
git stash
・stashの一覧を表示
git stash list
・最新のstashをワーキングツリーに戻す
git apply
・特定のstashをワーキングツリーに戻す
git stash apply stash@{n}
@{n}のnにはインデックス番号を入れる。n=0が最新のstash。古いほど数字が大きい。
他のstashコマンドでも使われるので覚えておきましょう!
・最新のstashを削除
git stash drop
・特定のstashを削除
git stash drop stash@{n}
・applyしてdropする
git stash pop
・特定のstashをapplyしてdrop
git stash pop stash@{n}
・全てのstashを削除
git stash clear
rebase
「コミットを細かくしすぎて多くなってしまった!」とか「別々のブランチで開発していたコミットを1本に繋げて見やすくしたい!」という場合に使えます。
ログを綺麗にしたい時に活躍します。
ただしmergeと違って新しいコミットを作成することになるので、リモートでやってしまうと他の人のコミットまで消失してしまう可能性が…!
rebaseはローカルでpushしていない内容だけに留めておきましょう。
もう一度言います、rebaseはpushする前にやるんだ!!やりたかったらな!!
詳しくはこちらのブログがわかりやすかったです。

・複数のコミットを1つにまとめる
git rebase -i HEAD~数字 <----------ex)HEAD~2
「数字」には最新(HEAD)のコミットから何個目までをまとめるか指定する必要があります。
// 結果
hint: Waiting for your editor to close the file...
pick 300bf1e h1タグをindex.htmlに追加 <----------この行の先頭を変更する
pick 11bdb6a pタグをindex.htmlに追加 <----------この行の先頭を変更する
# Rebase 56dbbad..11bdb6a onto 56dbbad (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
# to this position in the new commits. The <ref> is
# updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
コマンドの説明が出てくるので説明に従ってやりたいことを決めましょう。
決まったら上の方にある行を編集します。今回はsquashにしてみます。
pick 300bf1e h1タグをindex.htmlに追加
s 11bdb6a pタグをindex.htmlに追加 <----------先頭を「s」に変更
終わったら「:wq」で保存してviエディターを閉じます。
「:wq」を入力しても閉じない場合はescキーを押してみましょう。
// 結果
# This is a combination of 2 commits. <----------ここから
# This is the 1st commit message:
h1タグをindex.htmlに追加 --編集して新たなコミットメッセージを書く--
# This is the commit message #2:
pタグをindex.htmlに追加 <----------ここまで
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Apr 25 11:08:35 2024 +0900
#
# interactive rebase in progress; onto 56dbbad
# Last commands done (2 commands done):
# pick 300bf1e h1タグをindex.htmlに追加
# squash 11bdb6a pタグをindex.htmlに追加
# No commands remaining.
# You are currently rebasing branch 'main' on '56dbbad'.
#
# Changes to be committed:
# modified: index.html
ここからここまでの箇所に統合後の新たなコミットメッセージを記入します。
文字入力ができない場合は「i」を一度押してINSERTモードに切り替えましょう。
入力後、コマンドモードで保存して閉じたい場合はescキーを押してから:wqです。
するとbashの画面に戻るので、git logを実行すると統合された新しいコミットが出来上がっています!
・別ブランチのコミットを1本に繋げる
git rebase 繋ぎ先のブランチ名
引数にはコミットハッシュ値を指定することも可能です。
その場合は繋ぎ先の最新のコミットハッシュ値を入力します。
エイリアスを設定する
エイリアスとは「あだ名」という意味であり、コマンドや機能に付与する別名のことです。
辞書登録などしたことありますか?あんな感じです。ショートカットキーも当てはまりますね。
エイリアスを登録して使いこなせばスピードアップ間違いなし!
・エイリアスを作る設定画面に入る
git config --global --edit
・[alias]を追加して登録
[user]
email = 414ayaka@gmail.com
name = ayakakobayashi
[init]
defaultBranch = main
[pull]
rebase = false
[alias] <----------これ以下を追記
cm = commit -m
これは git cm
と打てば git commit -m
を打ったことと同義になります。
空のディレクトリを保存する
これはコマンドではないのですが講義で紹介されていたのでまとめます。
エディターで空のディレクトリを作成した場合、Gitでは変更として認識されません。
Gitに管理させたい場合は「空のディレクトリの直下に.gitkeepというファイルを作成する」ことで管理下に置くことができます!
コミットにタグをつけて管理する
・作業ブランチの最新コミットにタグ付け
git tag 任意のタグ名
タグ名は「v1.0」など、本番環境にリリースしたバージョンを名前にすることが多そう
・特定のコミットにコメントを書いてタグ付け
git tag -a 任意のタグ名 -m 'コメント' コミットハッシュ値
・タグをpush
git push origin タグ名
pushしないとリモートのコミット履歴に反映しないので注意。
・タグ一覧を表示
git tag
・ローカルリポジトリのタグを削除
git tag -d タグ名
・リモートリポジトリのタグを削除
git push origin --delete タグ名
まとめ
3回に渡るGitコマンドまとめ、いかがでしたでしょうか?
rebaseの操作でエディターが切り替わりまくるので操作に慣れなきゃなーと思ったのと、pushした後にrebaseしてしまうと大変なことになりかねないので注意ですね!
開発現場でたくさんGitを使って、ブログなんて見なくても使いこなせるよー!となれるように、普段から利用しときたいですね。
ここまで読んでくださり、ありがとうございました。
コメント