LoginSignup
0
0

More than 5 years have passed since last update.

GITの覚書

Last updated at Posted at 2019-01-29

コミットを整理する git rebase -i or git commit --amend

不要なコミット、例えばADDし忘れたファイルを追加するだけのコミットとかいらないので消したり修正したいとき

# 1つ前のコミットを書き換える方法
$ git commit --amend -m "書き換えたいメッセージ"
# 書き換えないで追加する方法
# git add 追加したいファイル
# git commit --amend --no-edit

# それ以外のコミットを書き換える方法。(もちろん直前も可)
$ git rebase -i コミットID
# するとエディタが起動
  pickと書いてあるところをeditやdropなどコマンドの説明に書いてあるように編集し、保存。`:wq`

-----コマンド一覧に従う↓
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
-----
→ editとした場合、また編集のエディタが起動し、コミットの内容を書き換えることができる。

誰かがリモートブランチを削除した場合

#リモート上の削除されたブランチがあるかどうか確認
$ git remote show origin
#リモート上の削除されたブランチをローカルから消す
$ git fetch --prune
又は
$ git pull --prune
  • 上記がめんどくさい場合git configに設定
#グローバルに設定。--prune付きでfetchとpullが実行されるようになる。
$ git config --global fetch.prune true
$ git fetch

犯人捜し

git blameコマンドを使うことで、各行が変更された最終コミットを知ることができる。

$ git blame ファイル名

# インデントコミットを無視
$ git blame -w 

# 他
# `-L` オプションで行数を指定できる

コミットはせずに変更を退避

# saveはなくてもOK
$ git stash save
# 新規ファイルも含めて保存
$ git stash -u

# 保存したものを確認
$ git stash list
stash@{X}がstashの名前。WIP onのあとはブランチ名

# 戻す
$ git stash apply stash@{0}
stash名を指定しない場合は、直近に退避された変更を戻す
# ADDされた状態で戻す場合はindexをつける
$ git stash apply stash@{0} --index

# 削除する
$ git stash drop stash@{0}
# すべて削除する
$git stash clear

# 戻す&削除はpop。らくちん
$ git stash pop stash@{0}

gitの理解が深まる記事

Gitのリポジトリの中身をなるべく正確に理解する

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