5
4

More than 5 years have passed since last update.

作業時によく使用するgitコマンド

Last updated at Posted at 2014-06-17

状態遷移

スクリーンショット 2014-06-16 12.39.37.png

良く使うコマンド

git status

現在の状態を確認する。

~/test/git_test3 (aaa ✘)✹✚✭ ᐅ git status
# On branch aaa
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.md
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       b.txt

よく見ると次に何をすべきか教えてくれてる。

git add

Stagedに進める(コミット対象にする)

git add FILES

まとめてaddしたい場合はいかが便利

  • 全ての変更点をadd
git add -A
  • 変更もしくは追加をadd(削除ファイルは対象外)
git add .
  • 変更のみadd(新規ファイルは対象外)
git add -u
  • ファイル内の一部のみaddする
git add -p

git checkout (ここではindexから戻す部分のみ説明)

指定されたファイルをStaged(index)の状態に戻す。
* Working Directoryの状態でcheckoutすればindexからファイルを取得してくるので変更が無い状態に戻る。

git checkout FILE_NAME

Working Directoryの内容全てを元に戻す

git checkout .

git commit

変更点を確定する。リポジトリの履歴に登録される。

git reset

HistoryのHEADの位置を変更する。

スクリーンショット 2014-06-16 15.58.32.png

HistroryのHEADとは最新のコミットのことである。

  • Historyを一個前にする(例えば間違ってコミットした場合)

変更した内容をそのままにしたい場合

git reset --soft HEAD~

完全に指定したhistoryに変更する(変更点は破棄される)。

git reset --hard HEAD~

git stash

一時的に他の作業を行いたい場合に変更点を退避させる。

退避

git stash save '一覧で表示されるメッセージ'

# saveは省略できるのでstashのみでもOK
git stash

# Untracked も対象にしたい場合は「-u」オプションを使う
git stash -u

一覧

git stash list

復元

# 最後にstashしたものを復元
git stash pop

# 複数stashしてる場合は引数を指定する
git stash pop stash@{1}

git fetch

リモートの情報を取得します。

git rebase

Histroyを綺麗にします。
本プロジェクトで採用しているgitフローは以下なのでfeatureブランチをローカルに適用する場合もpullではなくrebaseを使います。
http://qiita.com/astrsk_hori/items/659ce848139f1a452c6c

rebaseを使う

git rebase origin/master

pullもオプション指定ならOK

git pull --rebase

git push

プルリクエストするためにリモートに転送する。

git push origin BRANCH_NAME

強制push

git push -f origin BRANCH_NAME
  • 強制pushは作業ブランチしか実行しないこと!

git diff

  • 差分確認

HEADと比較

git diff

git diff hoge.txt

BRANCH比較

git diff --name-only hoge origin/master

git log

マージコミットからマージしたファイル一覧を取得

git log -m -1 --name-status
5
4
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
5
4