1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gitコマンドまとめ

Posted at

git status

  • 前回ステージにあげたとき(add)とワークツリーとの差分
  • 前回リポジトリにあげたとき(commit)とステージとの差分

git diff

  • git diff {file_name(optional)} => addする前の差分
  • git diff --staged => addした後の差分

git log

  • git log => 変更履歴を表示
  • --online: 1行で表示
  • -p {file_name}: 特定のファイルの変奥履歴
  • -n {コミット数}: コミット数だけ差分を表示
  • --decorate: どのブランチがどのコミットを指しているか

git rm

  • git rm --cached {file_name}: ファイルを残してキャッシュだけ削除
  • git rm -r {dir_name}: ディレクトリ削除
  • git rm {file_name}: コミットからもワークツリーからも削除

git mv

  • git mv {old_file} {new_file}

= mv {old_file} {new_file} + git rm {oldfile} + git add {new_file}

git remote

  • git remote add origin {some_url.git}: "origin"というショートカットでurlを登録
  • git remote rm origin: "origin"ショートカットを削除
  • git remote rename {old_remote_name} {new_remote_name}: リモートの名前変更
  • git remote -v: リモートリポジトリの確認
  • git remote show {remote_name}: 細かいリモートの情報

aliasの作成

  • git config --global alias.ci commit: "commit" = "ci"

git checkout

  • git checkout -- {file_name}: add前のファイルの変更を取り消す(ステージと同じにする)
  • git checkout {branch_name}: ブランチの移動
  • git checkout -b {branch_name}: 作成移動

git reset

  • git reset HEAD {file_name}: add後のファイルの変更を取り消す(ローカルは変化なし)
  • git reset HEAD^: 現在のブランチの最新のコミットを取り消してステージングも消す

git commit

  • git commit --amend: 直前のcommitをやり直す(現在のステージの状態で上書き)
  • git commit -v: エディタでコミットコメントを打つ(詳細つき)

git fetch

  • git fetch {remote_name}: リモートリポジトリからローカルリポジトリに取ってくる

= ワークツリーには取ってこない(remotes/remote_name/branch_name)

> `git fetch origin`
> `git branch -a`
> `git checkout remotes/origin/master`
> `ls` // ローカルリポジトリの内容確認

git branch

  • git branch -a: ブランチ一覧
  • git branch -m {new_branch_name}: ブランチ名の変更(move)
  • git branch -d {branch_name}: ブランチの削除(masterにmergeされてない場合削除しない)
  • git branch -D {branch_name}: ブランチの削除(強制)
  • git branch {branch_name}: ブランチの作成(移動はしない)
  • git branch {branch_name} {remote}/{branch_name}: リモートのブランチから作る

git merge

  • git merge {remote_name}/{branch_name}: ワークツリーにローカルリポジトリを統合
  • git merge {branch_name}: ローカルブランチを統合

git pull

  • git pull {remote_name} {branch_name}: fetch + merge
  • git pull --rebase {remote_name} {branch_name}: rebase型のpull(内容だけ取得したい)

= git config --global pull.rebase trueでデフォルトをrebase型に

= git config branch.master.rebase trueでmasterは常にrebase型に
※ ただしワークツリーのブランチにマージされてしまうので注意

git rebase

  • git rebase {branch_name}: 親コミットを別のブランチに変更(GitHubにあげるとき注意)

    = コミットの履歴をきれいに整理してくれる

    git checkout feature
    git rebase master // 親コミットをmasterに
    git checkout master
    git merge feature // masterをfeatureと同じに

  • git rebase -i {commit_id}: 複数のコミットをやり直す

> `git rebase -i HEAD~3` // 直前3つのコミットをやり直す HEAD^2はマージの2個め
> --- pick gh21f6d ヘッダーを修正
> +++ edit gh21f6d ヘッダーを修正 // squash: 直前のコミットと1つにする
> `git commit --amend`
> `git rebase --continue` // 次のコミットへ進む

git tag

  • git tag: タグの一覧を表示
  • git tag -l {search_word}: 絞り込み検索
  • git tag -a {tag_name} -m {message}: メッセージ付きタグの作成
  • git tag {tag_name}: 軽量版タグの作成
  • git tag {tag_name} {commit_name}: 後からタグ付け

= タグをリモートにpushする場合には

> `git push {remote_name} {tag_name}` // タグひとつを送信
> `git push {remote_name} --tags` // 一斉送信

git show

  • git show {tag_name}: タグ付けられたコミットを表示

git stash

  • git stash: コミットしたくないけどブランチを変えたいときに一次避難
  • git stash list: スタッシュ一覧
  • git stash apply: 最新のスタッシュの復元
  • git stash apply --index: ステージの情報も復元
  • git stash apply {stash_name}: 特定のスタッシュの復元
> `git stash apply stash@{1}` // stash@{1}を指定
  • git stash drop: 最新のスタッシュの削除
  • git stash drop {stash_name}: 特定のスタッシュの削除
  • git stash drop clear: 全スタッシュの削除

git switch

  • git switch {branch_name}: ブランチの移動
  • git switch -c {branch_name}: ブランチを作りつつ移動

git restore

  • git restore {file_name}: add前のファイルの変更を取り消し

= git checkout -- {file_name}

  • git restore --staged {file_name}: add後のファイルの変更を取り消し
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?