py-py’s blog

何か書くよ

基本的なGit操作

使えないのは自分が苦しいので、本当に基礎的なものだけどこれだけあればとりあえず仕事はできる、というものを書いていく。

前提
- Gitはインストール済み
- プロジェクトでgit cloneした後の操作を想定。そのため本当に0からの作業ではないことに注意

ブランチ作成

git branch branch_name

# 以下ならtestブランチ作成  
git branch test

ブランチへ移動

git checkout branch_name

# testブランチへ移動
git checkout test

編集したファイルをステージングする

git add file_name

# index.htmlをaddする
git add index.html

# カレントディレクトリ配下のファイルで編集したもの全てをaddする
git add .

ステージングしたものをcommitする

git commit -m "コメント"

pushする

git push

pushする際に

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

と怒られることがある。

これは今のブランチで、pushする先がわからないと言われている。

このエラーメッセージの下には

git push --set-upstream origin branch_name

と書かれている。これはpushする先をデフォルトでorigin branch_nameにする。
これにより「git push」とすると、origin branch_nameへpushするようになる。

ちなみにこれは「git pull」のときも同じように怒られることがある。

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

デフォルトの参照先がわからず、pullができないと言われている。
これを解消にするには

git branch --set-upstream-to=origin/branch_name branch_name

とすればよい。 test環境であれば

git branch --set-upstream-to=origin/test test

とする。
各ブランチのデフォルト参照先はどこかを調べるには

git branch -vv

とすると分かる。

参考

qiita.com

qiita.com

ブランチから最新のコードをもってきたい場合はpullをする

git pull

masterブランチへ別ブランチの変更点を取り込みたい時はmergeをする。
ここでは別ブランチをdevなどとする

# masterブランチへ移動する
git checkout master

# devブランチの内容をmasterへ取り込む
git merge origin dev

# コンフリクトがない場合、そのままpush
git push

# コンフリクトがある場合、衝突している箇所を手動修正→commit→push
git commit -m"コンフリクトを解消"
git push