LoginSignup
8
9

More than 5 years have passed since last update.

【備忘録】Gitのコマンド

Last updated at Posted at 2014-08-05

今まで使ったことのあるGitのコマンドのメモ。
随時変更していきます。

リポジトリの初期化

$ git init

ファイルの状態の確認

$ git status

インデックスに登録

$ git add <file>
$ git add .

同じファイル内でコミットを分けたい時

$ git add -p

とコマンドを叩くと

 <div class="contents">
+<h1>test</h1>
-<h1>TEST!!!!!</h1>
 <p>sample text sample text</p>
Stage this hunk [y,n,q,a,d,/,e,?]?

と聞かれるのでとりあえず、y(インデックスに登録)かn(インデックスに登録しない)で。

インデックスに登録したものを取り消す(git addの取り消し)

$ git reset HEAD <file>

リポジトリに記録

-mを付けないと、vimが立ち上がる。

windowsで、コマンドプロンプトなどを使っている人は、シングルクォーテーションではなくダブルクォーテーションを使わないとエラーになる。

$ git commit
$ git commit -m 'comment'

リモートリポジトリに登録

$ git push <リポジトリ名> <ブランチ名>

リポジトリのコピー

$ git clone <url>

リモートリポジトリの変更内容を取り込む

$ git pull <リポジトリ名> <ブランチ名>

リモートリポジトリの変更内容を確認する

$ git fetch <リポジトリ名> <ブランチ名>

差分を表示する

ワークツリーとインデックスの差分を表示。

$ git diff

直前のコミットでの差分

$ git diff HEAD^ HEAD

コミット履歴を見る

$ git log

-pは各コミットのdiffを表示する

$ git log -p

直前のコミットにファイルを追加したい

追加忘れたファイルをaddしてから。

$ git commit --amend

直前のコミットコメントを変更したい

addとかせずにこのまま。

$ git commit --amend

ローカルのファイルはそのままで直前のコミットを取り消す

$ git reset --soft HEAD^

ローカルの変更を元に戻す

$ git checkout <file>
$ git checkout .

ブランチを確認する

$ git branch

リモートブランチも確認したい

$ git branch -a

ブランチを作成する

$ git branch <ブランチ名>

ブランチを作成して、チェックアウトする

$ git checkout -b <ブランチ名>

リモートブランチをローカルにチェックアウトする

例)リモートのブランチ名が remotes/origin/test の場合
ローカルのブランチ名はリモートと同じにする、はず

$ git checkout -b test origin/test

ブランチを削除する

$ git branch -d <ブランチ名>

ブランチ名を変更する

旧ブランチにいる場合は、新ブランチ名だけで良い

$ git branch -m <旧ブランチ名> <新ブランチ名>

リモートブランチを削除する

ローカルブランチを消してからpush

$ git branch -d <ブランチ名>
$ git push <リポジトリ名> :<ブランチ名>

リモートブランチの一覧を更新する

$ git fetch --prune

Gitのユーザー情報設定

$ git config --global user.name "name"
$ git config --global user.email "sample@sample.com"

リポジトリごとに変更したい場合は、--globalを取る。(git initしてないとだめ)

$ git config user.name "name"
$ git config user.email "sample@sample.com"

リモートリポジトリのURLを設定する

$ git remote origin <url>

リモートリポジトリのURLを変更する

$ git remote set-url origin <url>
8
9
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
8
9