LoginSignup
0
0

More than 1 year has passed since last update.

Gitを思い出すぞよ

Last updated at Posted at 2022-09-29

1.はじめに

Gitの使い方忘れました。なので思い出しましょうね〜って記事です。
大したことはやりませんのでご了承くださいませ!

作業環境は新調したMacです。かっこいい。
ここにGit入れて適当にリポジトリ作ってCloneしたりPushしたりの動作確認ができたらゴールとします。

ちなみに2022年10月にM2搭載したMacが出るらしいです。
ぼくは同年9月に2021年型を購入しました。
大事なことはもっと早く言おうな。

2.Homebrewのインストール

Gitを入れるためのHomebrewをインストールします。手順は公式に倣います。

インストール後にWarningが出たので調べたらパスを通さなきゃダメとのこと。
ググってこの記事通りに手を動かして解消。感謝感謝です。

3.Gitのインストール

深く考えず勢いで実行。
soshi.satoはユーザー名(本名)でして、soshi.sato: ~/ %がプロンプトですね。
以降もこの感じで書いていきます。

soshi.sato: ~/ % brew install git

なんかSuccess!!!!的なメッセージが出てないですが、インストール完了してました。
Complete云々とは書いてあった。

soshi.sato: ~/ % git version
git version 2.37.3

4.リポジトリの作成

最高に適当なリポジトリを作成。
READMEしか置いてないです。

5.Gitしてゆく

手前味噌ですが自分の記事を見直しつつ手を動かします。
ひどい内容ですが書いた当時の自分を褒めてやりたい。

5-1.リポジトリのClone

ローカルの~にCloneします。

soshi.sato: ~/ % git clone https://github.com/sosat117/remember_git

5-2.git statusとかbranchとか

remember_gitリポジトリにcdして、諸々確認。

soshi.sato: ~/ % cd remember_git
soshi.sato: remember_git/ % git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

soshi.sato: remember_git/ % git branch
* main

masterじゃなくてmainなのか…?と思いましたが気にしない。

5-3.ブランチを切る

名前はなんとなくmain_cronにしました。
この辺りからあーこんな感じだったなあと思い出してきました、手を動かすのほんと大事。

というか今思ったけどcloneじゃないか?w
おれの英語力。。

soshi.sato: remember_git/ % git branch
* main
  main_cron

5-4.ブランチの切り替え

git checkoutで先ほど作ったcronに切り替える。

soshi.sato: remember_git/ % git checkout main_cron
Switched to branch 'main_cron'
soshi.sato: remember_git/ % git branch
  main
* main_cron

ついでにstatusも確認。

soshi.sato: remember_git/ % git status
On branch main_cron
nothing to commit, working tree clean

5-5.テストファイルの追加

viで適当なファイルを追加します。

soshi.sato: remember_git/ % vi otameshi_git.txt
soshi.sato: remember_git/ % ls -lha
total 16
drwxr-xr-x   5 soshi.sato  staff   160B  9 28 17:01 .
drwxr-x---+ 22 soshi.sato  staff   704B  9 28 17:01 ..
drwxr-xr-x  12 soshi.sato  staff   384B  9 28 16:58 .git
-rw-r--r--   1 soshi.sato  staff    63B  9 28 16:41 README.md
-rw-r--r--   1 soshi.sato  staff    49B  9 28 17:01 otameshi_git.txt
soshi.sato: remember_git/ % cat otameshi_git.txt
This file is otameshi-text.
written by ssato117.

5-6.ステータスの確認

赤いです。

soshi.sato: remember_git/ % git status
On branch main_cron
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	otameshi_git.txt

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

5-7.git add

addします。今回はファイル名決め打ちで。
流れでstuatusも。緑です。

soshi.sato: remember_git/ % git add otameshi_git.txt
soshi.sato: remember_git/ % git status
On branch main_cron
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   otameshi_git.txt

5-8.commit

commitしたのはいいのですが、

soshi.sato: remember_git/ % git commit -m "add otameshi_git.txt"
[main_cron 1d319fc] add otameshi_git.txt
 Committer: soshi.sato <soshi.sato@soshisatonoMBP.usen.ad.jp>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
 create mode 100644 otameshi_git.txt

soshi.sato@soshisatonoMBP.usen.ad.jpこれダサすぎませんかねw
問題が1つ増えてしまった。。

あ、commitは-mのメッセージはいつもaddとかmodifyからのファイル名にしてます。
この辺はベストプラクティスがあるはず。いつかググります。

statusも確認。綺麗になってますね!

soshi.sato: remember_git/ % git status
On branch main_cron
nothing to commit, working tree clean

5-9.push

なんか出た。

soshi.sato: remember_git/ % git push origin main_cron
Username for 'https://github.com': sosat117
Password for 'https://sosat117@github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/sosat117/remember_git/'

パスワード認証は去年の8月に終わっとーわ!!!!!とのこと。
公式にも思いっきり書いてました。

軽く調べたらキーペア作って認証するっぽいですね。やってみる。

6.その前に逸脱してinit

リモートリポジトリをCloneしたり、ローカルリポジトリをinitしたり、やり方はいろいろあるかと。
ただ個人的にinitって使ったことほぼ無いのでやってみました。

6-1.git init

めっちゃ黄色。

soshi.sato: ~/ % git init remember_git2
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /Users/soshi.sato/remember_git2/.git/

.gitファイル込みのディレクトリできててグッときますねw

soshi.sato: remember_git2/ % ls -lha
total 0
drwxr-xr-x   3 soshi.sato  staff    96B  9 29 10:40 .
drwxr-x---+ 24 soshi.sato  staff   768B  9 29 10:40 ..
drwxr-xr-x   9 soshi.sato  staff   288B  9 29 10:40 .git

ヒントにmaster, mainって書かれてたので調べたら、masterとslaveだと奴隷制度が連想されるからmainに変わったとのこと。共感しかない。

誰かが不快になる可能性があるなら避けるべき、って考えがスタンダードになると良いですね!!

7.再びpush

公式を読み進めてるとGUIからトークンを作成できるとのこと。

超わかりやすい手順通りにトークンを作成。

このトークンをPasswordに入力し成功!やったぜ!!

soshi.sato: remember_git/ % git push origin main_cron
Username for 'https://github.com': sosat117
Password for 'https://sosat117@github.com':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 347 bytes | 347.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'main_cron' on GitHub by visiting:
remote:      https://github.com/sosat117/remember_git/pull/new/main_cron
remote:
To https://github.com/sosat117/remember_git
 * [new branch]      main_cron -> main_cron

GUIを見ると切ったブランチが追加されてます。バッチリ。
プルリクはなんとなく想像つくのでやりません。

8.逸脱その② DS_Storeについて

Macからpushするとよく.DS_Storeも一緒にupしちゃってたの思い出しました。
ただcloneしてきたローカルリポジトリ、initしたローカルリポジトリどちらにも存在しないんですよね。

ただホームディレクトリとかにはよっ!!て感じで普通にいるので、何かしら変わったのかな…?
今回はDS_Storeってなんじゃい&消し方の調査に留めておきます。

9.おわりに

やっぱり手を動かしてると連動して記憶も蘇ってきますね。あとGithubの進化スピードはやっぱり早い。
センシティブな箇所も改善されたりしてて、ますますこのサービスが好きになりました。

また、先人の先輩方のブログにもたくさん助けられました。この場を借りてお礼申し上げます!
やっぱりエンジニアの集合知は最高だぜ。

コンフリクトとかまだわからないことも多いので、引き続きGit道を邁進してくぞー!!

10.追記

ファイルを削除する時はrmじゃなくてgit rmを使う。
戸惑いました。。w

以上です。
この記事がどなたかのお役に立ちますように。

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