LoginSignup
11
1

More than 1 year has passed since last update.

Git初心者がSourcetreeに頼らずコマンドラインを叩いてみる

Posted at

はじめに

こんにちは。
私はun-T factory!でフロントエンジエンジニアをしています。
といっても入社したばかりの駆け出しで、有益な情報をかける訳もなく、、、
自身の勉強のため、アウトプットの良い機会として投稿させていただきます。

弊社の業務ではファイルをGitでバージョン管理しており、日頃からSourcetreeを使っています。
Sourcetreeはとても便利なのですが、「今どういう状態なのだろう?」と思うことが多々あり、ちゃんと理解せず使っているのが現状です。

この機会を利用して、CUIでコマンドを叩いてイメージできるようになろうというのがゴールです。

やること

  • 1.ディレクトリ作成〜リモートリポジトリにプッシュまで
  • 2.開発ブランチ作成〜masterブランチにマージ、開発ブランチの削除まで

叩いてみた(ディレクトリ作成〜リモートリポジトリにプッシュまで)

pwd

カレントディレクトリを参照

$ pwd

/Users/ユーザ名

現在、フォルダ名「ユーザ名」がカレントディレクトリ。

mkdir

ディレクトリを作成。make directoryの略。

$ mkdir un-t

cd

指定したディレクトリに移動。change directoryの略。

$ cd un-t

ユーザ名@ユーザ名Bookpuro un-t %

~(ホームディレクトリ)内のun-tに移動。
カレントディレクトリ内以外に移動する場合は絶対パス/区切り。(ex.○○/○○/un-t)

git init

$ git init

Initialized empty Git repository in /Users/ユーザ名/un-t/.git/

.git(リポジトリ管理するために必要なファイルがまとまっているディレクトリ)が作られ、リポジトリが作成される。
既存のリモートリポジトリをローカルに複製する場合はgit clone

git status

$ git status

On branch master 

No commits yet

nothing to commit (create/copy files and use "git add" to track)

ワーキングディレクトリ(ローカルで作業中のディレクトリ)やインデックス(次にコミットする予定のファイルがある場所。ステージングともいう)に状態を確認するコマンド。

現在、マスターブランチにおり、まだコミットがない状態を表している。

open .

$ open .

Finderが立ち上がります。
⌘ + shift + .で隠しファイル(今回は.git)が表示されます。

code .

$ code .

VScodeが立ち上がります。
test.htmlファイルを作成します。

git status

$ git status

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    test.html

nothing added to commit but untracked files present (use "git add" to track)

もう一度git statusで状態を確認しました。
Untracked files未追跡のファイル、つまりインデックスに上げていないファイルのを指しています。

git add

$ git add .

作成したtest.htmlがインデックスに上がりました。
git add .カレントディレクトリ内の全ての変更をインデックスに上げる。
git add ファイル名特定のファイル名だけをインデックスに上げる。

git commit

$ git commit -m "add: test.html"`

[master (root-commit) c22380d] add: test.html
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.html

コミットの内容が記載されている。
""内はコミットメッセージを入れる。(fix,add,upgradeなど)

git log

$ git log

commit c22380d2bc760c1ce24bb38dd9f25ceb9aa7ea0c (HEAD -> master)
Author: アカウント名 <メールアドレス>
Date:   Tue Nov 23 10:47:44 2021 +0900

    add: test.html

git logはコミット履歴を確認するコマンドです。
問題なければリモートにプッシュします。

git remote

$ git remote add origin githubで作ったリポジトリURL

どのリモートリポジトリに上げるか指定する必要があるので、githubで新規リポジトリを作成し、指定します。

git push

$ git push origin head

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ユーザ名/un-t.git
 * [new branch]      head -> master

リモートのun-tにプッシュされました。
originはリモートという意味で、origin ファイル名でプッシュする先を指定できます。
headは現在のブランチの最新を表すので、ブランチ名を省略しても現在のブランチにプッシュできます。

叩いてみた(開発ブランチ作成〜masterにマージ、開発ブランチの削除)

git branch

$ git branch

*master

現在、masterブランチにいます。

git checkout -b "ブランチ名"

$ git checkout -b develop

Switched to a new branch 'develop'

現在のブランチをコピーして新しいブランチを作ります。
また、作成した新しいブランチに切り替えも同時に行います。

git branch

$ git branch

* develop
  master

developブランチが作成され、masterブランチから切り替えられました。

code .

$ code .

再びVScodeを立ち上げ、test.htmlに「un-t.test」と書き込みます。

git diff

$ git diff

diff --git a/test.html b/test.html
index e69de29..c538226 100644
--- a/test.html
+++ b/test.html
@@ -0,0 +1 @@
+un-t.test
\ No newline at end of file

変更した箇所とインデックスの違いを確認できます。
un-t.test」というテキストが追加されているのがわかります。

git add

$ git add .

ワーキングツリーをインデックスに上げます。

git commit

$ git commit -m "add: un-t.test"

git commit -m "add: un-t.test"
[master 21d9672] add: un-t.test
 1 file changed, 1 insertion(+)

インデックスのものをコミットします。

git push

$ git push origin head

git push origin head
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 249 bytes | 249.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/ユーザ名/un-t/pull/new/master
remote: 
To https://github.com/ユーザ名/un-t.git
 * [new branch]      head -> develop

リモートdevelopブランチにプッシュします。

git checkout

$ git checkout master

Switched to branch 'master'

developブランチからmasterブランチに移動します。

git branch

$ git branch

   develop
*  master

masterブランチにいることを再度確認します。

git merge "マージしたいブランチ名"

$ git merge develop

Updating c22380d..21d9672
Fast-forward
 test.html | 1 +
 1 file changed, 1 insertion(+)

現在のブランチに指定したブランチをマージします。
この場合だとmaterブランチにいるので、指定したdevelopブランチをmasterにマージします。

git log

$ git log

commit 21d96726b17691ab7955fd4d9a3f504cf6d3aa0c (HEAD -> master, origin/develop, develop)
Author: ユーザ名 <メールアドレス>
Date:   Wed Nov 24 08:27:01 2021 +0900

    add: un-t.test

commit c22380d2bc760c1ce24bb38dd9f25ceb9aa7ea0c (origin/master)
Author: ユーザ名 <メールアドレス>
Date:   Tue Nov 23 10:47:44 2021 +0900

    add: test.html

git logで現在の状態を確認します。
リモートのmaster以外は全て最新のコミット内容と同一になっています。

git push

$ git push origin head

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ユーザ名/un-t.git
   c22380d..21d9672  head -> master

リモートのmasterにも変更を反映してあげます。

git log

$ git log

commit 21d96726b17691ab7955fd4d9a3f504cf6d3aa0c (HEAD -> origin/master,master, origin/develop, develop)
Author: ユーザ名 <メールアドレス>
Date:   Wed Nov 24 08:27:01 2021 +0900

    add: un-t.test

commit c22380d2bc760c1ce24bb38dd9f25ceb9aa7ea0c 
Author: ユーザ名 <メールアドレス>
Date:   Tue Nov 23 10:47:44 2021 +0900

    add: test.html

これでローカル、リモートともすべて同一のコミット内容となりました。

git branch -d "ブランチ名"

$ git branch -d develop

Deleted branch develop (was 21d9672).

開発ブランチは不要になったので削除します。
まず、このコマンドでローカルのdevelopブランチを削除します。


$ git push --delete origin develop

To https://github.com/ユーザ名/un-t.git
 - [deleted]         develop

まだリモートのdevelopブランチが残っているので、このコマンドでリモートdevelopも削除します。

git branch

$ git branch

* master

developブランチが削除され、ゴール到達です!

感想

  • Sourcetreeにはaddがないため、インデックスに上げるというのが理解でき、状態を把握できるようになった。
  • branch、logを確認する大事さが理解できた。
  • ブランチ数が多かったり、更新案件などはSourcetreeが向いていると思った。単純なプロジェクトを扱う場合触れてみたい。
  • 他にもコマンドはたくさんあることもわかったので、次回もやってみます!
11
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
11
1