LoginSignup
1
1

More than 3 years have passed since last update.

gitに慣れるための最低限のコマンド - 変更内容の記録 編

Last updated at Posted at 2019-06-03

gitは覚えることが非常に多いため、最初に挫折しないためにも、まずはよく使うコマンドの使い方を覚え、流れに沿って手元で試し、コマンドに慣れることが大事だと考える。

「分散型バージョン管理システムって何?」「リポジトリって何?」という疑問にはこの資料では言及しない。知りたくなった時にググってほしい。

本資料は gitに慣れるための最低限のコマンド - 環境の構築 編 を経て、実際にリモートリポジトリに手元の変更をpushするまでを解説する。

ファイルを変更したら、addしてcommitしてpushしてプルリク!

1. cd /path/to/dir/

作業ディレクトリに移動する。例えば作業ディレクトリが/Users/hikarumaruyama/src/github.com/hmaruyama/test-pagesなら、以下のコマンドになる。

$ cd /Users/hikarumaruyama/src/github.com/hmaruyama/test-pages

2. git fetch && git merge origin/master

最新のリモートリポジトリの内容を取り込んで(fetch)、ローカルリポジトリに反映(merge)する。

fetchしてmergeする前に、ローカルリポジトリのmasterブランチにチェックアウトする。

$ git checkout master
$ git fetch
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 24 (delta 7), reused 19 (delta 7), pack-reused 0
Unpacking objects: 100% (24/24), done.
From github.com:hmaruyama/test-pages
   ae59945..778c83b  master     -> origin/master
$ git merge origin/master
Updating ae59945..778c83b
Fast-forward
 src/en/product/ocean/index.html                  | 4 +---
 src/en/product/ripple/index.html                 | 4 +---
 src/en/product/wave/index.html                   | 2 +-
 src/en/products/wave/faq/index.html              | 2 +-
 src/jp/product/ocean/index.html                  | 4 +---
 src/jp/product/ripple/index.html                 | 4 +---
 src/jp/{products => product}/wave/faq/index.html | 2 +-
 src/jp/product/wave/index.html                   | 2 +-
 8 files changed, 8 insertions(+), 16 deletions(-)
 rename src/jp/{products => product}/wave/faq/index.html (99%)

3. git checkout -b <ブランチ名>

masterブランチでは基本的に作業しない。
ローカルリポジトリのmasterブランチから新しいブランチを作成してチェックアウトする。

$ git checkout -b fix_bug

4. git add <ファイル名>

変更したいファイルを変更したら、インデックスに追加する。インデックスとは?

ファイル名を指定する代わりに、カレントディレクトリを表す.を指定することで、カレントディレクトリに存在する全てのファイルを一気にインデックスに追加することが可能。

$ git add .

5. git commit -m "コメント"

インデックスに存在するファイルを作業ブランチにコミットする。
コメント内容は、変更する理由をのべるのが良い(変更箇所はコードを見れば分かるため)。

$ git commit -m "タイポを修正しました。"

6. git push origin <ブランチ名>

コミット内容を、リモートリポジトリにプッシュする。

$ git push origin fix_bug
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