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?

こつこつ貯めてきた git コマンド

Posted at
やりたいこと コマンド 補足
未ステージの変更をクリア git checkout — .
未追跡の変更を全て削除する git clean -fd -d はディレクトリも含める
未ステージのファイルを指定して変更を取り消 git checkout -- ファイル名
未ステージの変更(新規ファイルも含めて)の一覧を取得 git ls-files -mo —exclude-standard m:修正されたファイル
o:新規ファイル
exclude-standard:gitignore対象のファイルは表示しない
ステージしたファイル一覧を見る git status
git diff —cached —name-only
ブランチ名を確認 git branch
ブランチを切る git checkout -b 作成ブランチ名 作成元ブランチ名 作成ブランチ名をなくせば現在のブランチからきることになる
ローカルで作成したブランチをリモートにあげる git push -u origin (ローカル)ブランチ名 ブランチ名に指定した値はリモートのブランチ名となるため、基本的にはローカルブランチと同じものにした方が良い。

uオプションは今pushしたローカルのブランチと作成するリモートのブランチを今後ペアとして扱うためのオプション。
pushやpullする時にいちいちリモートブランチを指定しなくて済むようになる。
fetchした後にpullする内容があるかどうか確認 git status Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.こんな感じで表示されればpullする
指定したファイルをステージから降ろす git reset HEAD ファイル名 git2.23以前のコマンド
新しいのだと以下
git restore --staged ファイル名

HEADはなくていい
ステージ状態のファイルを全てunstageする git reset
前回のコミットをやめる git reset —soft HEAD^ コミットだけやめてステージ状態は保つ
前回のコミットをやめる git reset —hard HEAD^ コミットごと消す
現在のブランチのHEADと現在の状態の差分をファイル単位で見る git diff HEAD -- path/to/file
ローカルブランチの一覧 git branch
リモートとローカルのブランチ一覧 git branch -a
ブランチの削除 git branch -d ブランチ名
git branch -D ブランチ名
dはマージ済みのブランチを削除
Dは未マージでも削除
既にマージされたリモートブランチをgit branch -aで表示させなくする git fetch --prune git fetchで常にプルーンしたい場合は
git config --global fetch.prune true
リビジョンからブランチを切る git checkout -b 新しいブランチ名 <コミットID> 例:git checkout -b feature/test abc1234
ローカルで既にコミットしているが、リモートのコミットを適用した後そのコミットを追加したい git pull —rebase 自分のコミットがけつに来るようにpullしてくれる→マージコミットを減らせる
現在の変更を一時退避する git stash

git stash save -u "コメント"
stashした変更はstashリストというgit内の保存領域に確保される。
git stash popなどしない限り残りづける

-uでuntrackedなファイルもstash可能
現在の変更を一時退避する改 git stash push -m "コメント" 2.13より後のバージョンで使用可能。ステージされたファイルをstashする。ステージ関係なく全部stashする場合は-uオプション
退避した変更を元に戻す git stash pop 特定のstashをpopしたいとき
git stash pop stash@{1}
stashした内容の一覧を見る git stash list
stashリストを全部消す git stash clear
特定のstashを消す git stash drop stash@{0}
リモートブランチAをスイッチしたローカルブランチA'にA'から切ったローカルブランチBをコミット履歴を残さずにマージしたい(Bで作業している間にAやA'でコミットが行われている場合) Bブランチ
git fetch origin Aブランチ
git rebase origin/Aブランチ

A'ブランチ
git pull
git merge Bブランチ
マージするブランチのコミットがマージ先ブランチのコミットの先端の場合は履歴を残さずにマージが可能っぽい
fast-forward っていうらしい
追跡対象のファイルになってるけどビルドなどで生じる変更をignoreしたい git update-index --skip-worktree [ファイル名]
↑でignoreさせるようにしたファイルの追跡を再開したい git update-index --no-skip-worktree [ファイル名]
↑でignoreしたファイルの一覧が見たい git ls-files -v | grep ^S -v はignoreされているファイルを出力する。skip-worktreeの場合はS~で表示される
gitのconfig設定を見る git config --global --list
gitコマンドのエイリアスを設定する ex)
git config --global alias.ls 'ls-files --exclude-standard'
設定したエイリアスを削除 git config --global --unset alias.<エイリアス名>
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?