LoginSignup
0
0

More than 3 years have passed since last update.

初めてのGitコマンド一覧【初心者向け】

Last updated at Posted at 2020-09-26

エンジニア4ヶ月目のtomoakiです。
実務でよく使っているGitコマンドについて、備忘録も兼ねてまとめていきます。

現場に入りたてのエンジニアがよく使うコマンドを中心に記載しているので、過不足あると思いますがご容赦ください。m(_ _)m
適宜、追加していく予定です。

git init

gitプロジェクトを新規作成します。
作成したいディレクトリに移動してからgit initコマンドを叩いてください。

$ git init
Initialized empty Git repository in <ディレクトリ名>/.git/

git clone

指定したリポジトリ名のプロジェクトを、指定したディレクトリにコピーします。

$ git clone <リポジトリ名> <ディレクトリ名>

Cloning into '<ディレクトリ名>'...
remote: Enumerating objects: 190, done.
remote: Counting objects: 100% (190/190), done.
remote: Compressing objects: 100% (128/128), done.
remote: Total 190 (delta 53), reused 172 (delta 38), pack-reused 0
Receiving objects: 100% (190/190), 70.62 KiB | 420.00 KiB/s, done.
Resolving deltas: 100% (53/53), done.

git status

ワーキングツリー、ステージングエリアの状況を確認します。
変更したファイルや新規追加したファイルがステージングエリアにある場合は以下のように表示されます。

$ git status

On branch <ブランチ名>
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   <変更したファイル名>

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    <新規作成したファイル名>

ステージングエリアに追加したファイルがある場合は、以下のように表示されます。

$ git status

On branch <ブランチ名>
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   <ステージングエリアに追加したファイル名>

git add

ワーキングツリーで編集したファイルをステージングエリアにアップします。

# ファイル名を指定して追加する場合
$ git add <ファイル名>

# ステージングエリアの全てのファイルを追加する場合(.は全てのファイルを意味します)
$ git add .

# 編集したファイルの一部を追加する場合
$ git add -p

-pオプションをつけて実行すると、以下が表示されます。
表示された箇所を追加する場合は「y」、追加しない場合は「n」、さらに細かく指定する場合は「s」を入力します。

(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?

-pオプションは、便利なので覚えておくと何かと役に立ちます。

git commit

ステージングエリアに上がっているファイルをローカルリポジトリにコミットします。
-mオプションで、コミットメッセージを追加できます。

$ git commit -m "<コミットメッセージ>"

[master aa1db46] <コミットメッセージ>
 8 files changed, 67 insertions(+), 21 deletions(-)
 create mode 100644 <コミットしたファイル名>

git commit --amend

コミットメッセージを修正したい場合は、git commit --amendコマンドが使えます。
開いたvimエディタで直接コミットメッセージを編集できます。

なお、git commit --amendで修正して良いのは、git pushする前のコミットのみです。
git push後だとコンフリクトが発生するので、注意してください。

$ git commit --amend

↓↓ vimエディタが開き、コミットメッセージを編集できる

<コミットメッセージ>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Sep 26 14:59:19 2020 +0900
#
# On branch master
# Changes to be committed:
#       modified:   <コミットしたファイル名>

git push

ローカルリポジトリにコミットしたファイルをリモートリポジトリにプッシュします。
git pushコマンドを実行する前に、git remote addコマンドでリモートリポジトリを登録しておいてください。

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

Enumerating objects: 34, done.
Counting objects: 100% (34/34), done.
Delta compression using up to 8 threads
Compressing objects: 100% (17/17), done.
Writing objects: 100% (18/18), 2.56 KiB | 2.56 MiB/s, done.
Total 18 (delta 10), reused 0 (delta 0)
remote: Resolving deltas: 100% (10/10), completed with 9 local objects.
To <リポジトリ名>

続く…

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