1
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 3 years have passed since last update.

MACでGit周りの操作まとめ【メモ】

Last updated at Posted at 2020-07-30

Gitっていろいろ操作ありますが、毎回忘れて都度ググっているので自分用のメモとしてよく使う操作とコマンドを記載していきたいと思います。
Gitlab使っているのでGitlabの操作基準で書いてます(最初のセットアップ以外はほぼ変わらないと思いますが)。
都度更新します。

Git登録

Gitlabのアカウントはある状態でPCにGitが登録されていない場合の手順

gitあるかどうかの確認

バージョン返ってくればOK
ない場合はbrew install git とかインストールできるはず(ググって)

terminal
$ git --version
git version 2.24.2 (Apple Git-127)
$

Git初設定

ユーザ名とメールアドレスを登録

terminal
$ git config --global user.name "ユーザー名"
$ git config --global user.email "メールアドレス"

SSH Key発行&登録

GitlabにSSH Key情報を登録させる(これしないとクローンとかできない)
Gitlabの右上のアイコンから「Settings」→左ペインの「SSH keys」でkeyのところにid_rsa.pubの内容をペーストして「Add key」ボタン押下(GUI画面はめんどうなので省略)

terminal
$ cd ~/.ssh
$ ssh-keygen -t rsa -C tk-shiina@kddi.com
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa):  (そのままEnter)
Enter passphrase (empty for no passphrase):  (自分で決めたパスワード入力)
Enter same passphrase again:  (パスワード確認にもう一度入力)
Your identification has been saved in ~/.ssh/id_rsa.
Your public key has been saved in ~/.ssh/id_rsa.pub.
The key fingerprint is:
************************************************** your@email.com
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+----[SHA256]-----+
$ ls
config		id_rsa		id_rsa.pub	known_hosts
(※id_rsaが秘密鍵なので管理注意)
$ cat id_rsa.pub
(中身をまるごとコピー)

Gitクローン

既存のプロジェクトの内容をクローンするとき
GitlabのレポジトリでClone→「Clone with SSH」の右のクリップボードアイコンをクリックしてURLコピー

terminal
$ cd (クローンしたいディレクトリ)
$ git clone (コピーしたURL)

Gitブランチ

現在のブランチの確認(※がついているのが今いるブランチ)
リモート含めてすべてのブランチを確認したいときは-aオプションをつける

terminal
$ git branch 
* master
  Develop
$ git branch -a
* master
  Develop
  remotes/origin/Develop
  remotes/origin/Developer/A
  remotes/origin/HEAD -> origin/master
  remotes/origin/Developer/B
$

git branchのあとに適当な名前をつけると新しいローカルブランチを作成できる

terminal
$ git branch test(新規作成するローカルブランチ名)
$ git branch
* master
  Develop
  test

ローカルブランチの削除方法

terminal
$ git branch -d test(削除するローカルブランチ名)
Deleted branch test (was 679b53b).
$ git branch
* master
  Develop

Gitチェックアウト

ブランチを切り替えたいとき

terminal
$ git branch 
* master
  Develop
$ git checkout Develop(チェックアウトするブランチ名)
Updating files: 100% (1088/1088), done.
Switched to branch 'Develop'
Your branch is up to date with 'origin/Develop'.
$ git branch 
  master
* Develop

ローカルブランチない状態でリモートブランチ切り替えたいときは-bオプション
ローカルブランチを作成した上でチェックアウトしてくれる

terminal
$ git checkout Developer/A(作成するローカルブランチ名) origin/Developer/A(リモートブランチ名)
Updating files: 100% (1088/1088), done.
Branch 'Developer/A' set up to track remote branch 'Developer/A' from 'origin'.
Switched to a new branch 'Develop/A'
$ git branch
  master
  Develop
* Developer/A
1
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
1
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?