6
6

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メモ

Last updated at Posted at 2015-09-22

はじめに

イマイチGitの操作に自信が持てない自分のために、マニュアルを読みながら参考になりそうなコマンドをまとめてみることにしました。Gitで困ったら、そのたびにググってたけど、いつまで経っても身につかないので。

[参考]

初期設定

Git全体で有効な設定。設定は~/.gitconfigに保存される。--globalオプションで指定可能。

$ git config --global --list
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

リポジトリごとに有効な設定。設定はリポジトリごとの.git/cofigに保存される。

$ git config --list
$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

[参考]

Gitリポジトリの取得

リポジトリの初期化。

$ cd /path/to/dir
$ git init

ファイルをリポジトリに追加してコミット。

$ git add README.md
$ git commit -m 'first commit'

既存リポジトリのクローン。最後にディレクトリ名を付けると、クローン先のディレクトリ名を指定できる。

$ git clone git://github.com/schacon/grit.git
$ git clone git://github.com/schacon/grit.git mygrit

[参考]

変更内容の反映

リポジトリの状態の確認。

$ git status

.gitignoreファイルには、gitで管理したくないファイルを記述する。色々と正規表現が使える。

$ cat .gitignore
# コメント。これは無視されます
# .a ファイルは無視
*.a
# しかし、lib.a ファイルだけは .a であっても追跡対象とします
!lib.a
# ルートディレクトリの TODO ファイルだけを無視し、サブディレクトリの TODO は無視しません
/TODO
# build/ ディレクトリのすべてのファイルを無視します
build/
# doc/notes.txt は無視しますが、doc/server/arch.txt は無視しません
doc/*.txt
# doc/ ディレクトリの .txt ファイル全てを無視します
doc/**/*.txt

変更したけどまだステージしてないファイルの差分確認。ステージしたけどコミットしていない差分の確認には--stagedまたは--cachedオプションをつける。

$ git diff
$ git diff --staged
git commit
git commit -m 'commit comment'
git commit -a -m 'commit comment'

ファイルの削除。

$ rm filename
$ git rm filename

ファイル名の変更。

$ git mv file_from file_to

[参考]

コミット履歴の閲覧

ログの閲覧。-pオプションで差分も確認できる。-2オプションで直近2コミット分の履歴を確認できる。--statusオプションで統計情報を参照できる。--graphオプションでコミットの履歴をアスキーアートのグラフで参照できる。

$ git log
$ git log -p
$ git log -p -2
$ git log --status
$ git log --graph

[参考]

作業のやり直し

コミットのやり直し。

$ git commit -m 'first commit'
$ git add 忘れていたファイル
$ git commit --amend

ステージしたファイルの取り消し。

$ git reset HEAD benchmarks.rb

ファイルへの変更の取り消し。

$ git checkout -- benchmarks.rb

[参考]

リモートでの作業

リモートリポジトリの一覧表示。-vオプションをつけると、リモートリポジトリのURLも表示する。show originでリモートリポジトリ(origin)の詳細を確認。

$ git remote
$ git remote -v
$ git remote show origin

リモートリポジトリのクローン。

$ git clone git://github.com/schacon/ticgit.git

リモートリポジトリの追加。

$ git remote add pb git://github.com/paulboone/ticgit.git

init+remote addとcloneって何が違うんだろ。と、思ったので調べてみたメモ。

リモートリポジトリのデータを取得。実行してデータを同期すると、すべてのブランチで操作ができるようになる。

$ git fetch

自動的にフェッチを行い、リモートブランチの内容を現在のブランチにマージ。

$ git pull

手元のmasterブランチを、リモートのoriginにプッシュ。

$ git push origin master

リモートを参照する名前の変更。下記の例では、pbからpaulへ名前を変更する場合。

$ git remote rename pb paul

リモートの削除。

$ git remote rm paul

[参考]

ブランチ

ブランチ作成と切り替え。

$ git branch testing
$ git checkout testing

masterブランチへの切り替え。

$ git checkout master

checkout-bオプションをつけて実行すると、ブランチの新規作成と切り替えを同時に実行できる。

$ git checkout -b feature/iss53

ブランチで作成した内容をmasterにマージする。

$ git checkout master
$ git merge feature/iss53

[参考]

ブランチの管理

現在のブランチ一覧を表示。-vオプションはブランチの詳細情報を表示。--mergedオプションはマージしたブランチ名を表示。--no-mergedオプションはまだマージしてないブランチ一覧を表示。

$ git branch
$ git branch -v
$ git branch --merged
$ git branch --no-merged

ブランチの削除。まだマージできていないものは、-dオプションでは削除できない。強制的に削除する場合は、-Dオプションを付けて実行する。

$ git branch -d testing

[参考]

リベース

リベース。別ブランチの内容を現在のブランチに取り込む。他にも色々とリベースのテクニックがありそう。詳細は参考を読んでおくべし。

$ git checkout experiment
$ git rebase master

[参考]

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?