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コマンドコピペ元

Last updated at Posted at 2022-02-18

gitコマンドで使うたびにググっているようなものをまとめてコピペできるようにしておく。逐次追加予定

git add を取り消す(変更内容は取り消さない)

$git reset HEAD hoge

直前のコミットのメッセージを変更する

$git commit --amend -m "変更後のメッセージ"

git addですでにgit管理下にあるファイルの変更をステージングする

$git add -u

git add -A (-all)でもいいけど、git管理下にないファイルもステージングしてしまうので、git add -uしてからgit add ファイル名で個別に対応した方がミスが減ると思う。

特定コミットを別ブランチにマージする

$git cherry-pick "コミット番号"
  • -eでコミットメッセージを変更できる
  • -nでコミットせずにマージだけしてくれる
  • 参考 : https://git-scm.com/docs/git-cherry-pick
    複数コミットに対してまとめて実施
$git cherry-pick "コミット対象の1つ前のコミット".."コミット対象の最後のコミット"

Untrackedファイルの一覧を取得する

$git ls-files -o --exclude-standard
  • -o(--others) : git 管理外のファイルを対象とする
  • `--exclude-starndard : .gitignoreや.git/info/excludeなど除外対象になっているファイルを含む

さらにファイルをバックアップする

  • cpioコマンドに渡すことでファイルのバックアップを作ってくれる
$git ls-files -o --exclude-standard -z | cpio -pmd0 出力先ディレクトリ

リモートリポジトリ削除

$git remote rm リポジトリ

直前のコミットの打ち消し

$git reset --soft(--hard) HEAD^
  • soft : コミットを無かったことにして変更自体はワークスペースに戻る
  • hard : コミットを無かったことにして変更自体もワークスペースから削除する

特定のファイルだけ、コミット前の状態に戻す

$git restore --source=コミット番号^ path/to/file.txt

ローカルの状態からブランチを作成してリモートにpushする

検証用の設定とかをfeatureブランチに入れたくないときに、ローカルの状態から検証用ブランチを切ってそちらにコミットするときに使ったり

ブランチ作成する元ブランチに切り替える

$git checkout develop

ブランチ作成

$git checkout -b ブランチ名

ブランチをリモートに登録

$git push -u origin ブランチ名

あとはいつも通りgit addしてcommit, pushすればよろし

git commit, rebaseで利用するエディタを変更する

$git config --global core.editor <エディタ名>
# git config --global core.editor vi
# git config --global core.editor code -> エラーになってできなかった

reabseでコミットをまとめる

こちら参照
https://qiita.com/takke/items/3400b55becfd72769214

  • コミットメッセージを残さない場合は、メッセージを残さないコミットをpick -> fixupにする

リモートリポジトリが存在しないローカルブランチを削除する

リモートに存在しないローカルブランチをリストアップ。

$git branch -vv | grep ': gone]' | awk '{print $1}'

ローカルブランチ削除

$git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d

ローカルブランチにpushしていないコミットがあっても矯正削除

$git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

※リモートと紐づいているローカルブランチをリモートトラッキングブランチというらしい

git diff

ファイル名だけ取得

git diff --name-only

ブランチ比較

$git diff ブランチA ブランチB

カレントブランチと別のブランチ

$git diff ブランチB

特定コミットと比較

$git diff コミット番号

コミット間で変更のあるファイルを抽出する

- リポジトリルートで実行しないとエラーになるので注意
$git archive ブランチ名 `git diff --name-only コミットfrom コミットto --diff-filter=d` -o diff.zip

参考: https://www.crecafe.co.jp/crelab/creative/3286/

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?