LoginSignup
7
10

More than 3 years have passed since last update.

Git Tips

Last updated at Posted at 2015-11-30

rmで削除したファイルをstage(index)に上げる

git add <target> --update

ローカルブランチの掃除

tarcking branch

リモートリポジトリから削除されたブランチをローカルからも削除する。

git fetch -p

local branch

雑だが、下のコマンドでリモートにマージ済みのブランチが削除される。

git branch | xargs git branch -d

マージされていないブランチの場合はエラーを吐いて次のブランチの削除を試行する。

error: The branch 'unmerged-branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D unmerged-branch'.

サブモジュールも一緒に取ってくる

git clnoe --recursive git@github.com:hoge/fuga.git

他のブランチのファイルの変更を取り込む

git checkout <ブランチ名> -- <対象ファイルのパス>

gitの管理対象から特定のファイル、ディレクトリを削除する

ファイルも一緒に削除する

git rm [削除したいファイル]

ディレクトリごと削除する

git rm -r [削除したいディレクトリ]

gitの管理対象からは外すがファイルは残す

--cachedオプションを付けることにより、ファイルを残したまま管理対象から外すことができる。

git rm —-cached [削除したいファイル]

※ ファイルを残した場合は必ず.gitignoreに追記するように。

マージコミットをcherry-pickする

git cherry-pick -m 1 [コミットハッシュ]

cherry-pickをコミットせずに取り込む

$ git cherry-pick --no-commit [コミットハッシュ]

コミットが属しているブランチ一覧

git branch --contains [コミットハッシュ]

HEADとINDEXの差分を表示する

git diff --cached

rebase時に備えてコミットを関連付けておく

rebase時にfixupする予定のコミットをするとき、下記のように対象のコミットを fixup オプションで指定しておく

$ git commit --fixup=<target commit>

するとrebase時に自動的にソートされてfixupコマンドが指定される

$ git rebase -i --autosquash <commit>

pick 0b4d33848 hoge
fixup 4e644a862 fixup! fuga
pick da7553cfe piyo

特定ファイルのログを見る

$ git log -p <filepath>

addを取り消す(stageから除外する)

$ git reset HEAD <filepath>

特定ファイルのみHEADに戻す

$ git checkout HEAD -- <対象ファイル>

untracked fileを削除する

git clean のオプションについてのまとめ | gotohayato.com - https://gotohayato.com/content/104/

# 強制削除
$ git clean -f

# ディレクトリが含まれる時
$ git clean -df
7
10
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
7
10