0
0

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コマンドメモ

0
Last updated at Posted at 2021-03-06

基本的な使い方

git init

リポジトリの作成をする
一番初めに必ず実行する

git add -A
もしくは
git add .

プロジェクトのすべてのファイルをリポジトリに追加する

git status

Gitにプロジェクトのファイルを追加すると、最初はステージング(Staging)という一種の待機用リポジトリに置かれ、コミットを待ちます。安全のため、いきなりコミットしないようになっているのです。ステージングの状態を知るにはstatusコマンドを使います。

git commit -m "Initialize repository"
Initialize repositoryは例なので、任意でコミット名を変える

ステージングエリアで控えている変更を本格的にリポジトリに反映(コミット)するには、commitコマンドを使います。
mフラグを使うと、コミットメッセージをコマンドラインで直接指定できます。-mフラグを使わない場合はシステムのデフォルトのエディタが開き、そこでコミットメッセージを入力します。

git log

logコマンドでコミットメッセージの履歴を参照できます。

ブランチを使用する場合

流れ

トピックブランチ作成

ブランチ上で変更をコミット

トピックブランチをmasterブランチにマージ

(ブランチを削除)

git checkout -b (ブランチ名)

トピックブランチ(短期間だけ使う一時的なブランチ)はcheckoutと-bフラグを使って作成できます。

git branch
 master
* modify-README

git branchは、すべてのローカルブランチを一覧表示します。「*」はそのブランチが現在使用中であることを表します。

git commit -a -m "(コミット名:命令形かつ現在進行形)"

ブランチ上でコミット

git checkout master
git merge (マージしたいブランチ名)

コミット後、上記のコマンドでブランチをmasterブランチにマージ

git branch -d (ブランチ名)

変更をマージ後、上記のコマンドでトピックブランチを削除して終了。
トピックブランチの削除は必須ではありません。実際、トピックブランチを削除せずにそのままにしておくことはよく行われています。トピックブランチを削除せずに残しておけば、トピックブランチとmasterブランチを交互に行き来して、キリの良い所で変更をマージする事ができます。

git branch -D (ブランチ名)

トピックブランチ上の変更を破棄することができる
-dフラグと異なり、-Dフラグは変更をマージしていなくてもブランチを削除してくれます。

GitHubへのプッシュ

git push (origin masterは省略可)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?