Skip to content

Instantly share code, notes, and snippets.

@daijinload
Last active July 20, 2023 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daijinload/01002c15b5308df8554920a4e2fe030d to your computer and use it in GitHub Desktop.
Save daijinload/01002c15b5308df8554920a4e2fe030d to your computer and use it in GitHub Desktop.

Gitの説明 第2弾 セットアップ編

今回はGitを使うためにインストールやセットアップなどを行います。

実際の使い方は、次の動画で説明となります。

自己紹介

サーバーサイドエンジニアのdaijinloadです。

Node.js, TypeScript, Java, Golang, PHPあたりを業務でやったことがあります。

サブ的にではありますが、ReactやFlutterなどのフロントエンドも経験があります。

環境について

Ubuntu 20.4ベースの、LinuxMint 20.3(HWE)をVirtualBoxに入れて起動しています。

OSインストール後に、下記のことを行っています。

  • homeフォルダのダウンロードなどの名称を英語化
  • vimのインストール
  • フォント:myricaをコンソールとメモ帳に設定
  • 事前にgithubにログインしておく

Gitのaptリポジトリ追加とインストール

今回は最新を入れる手順としたいため、リポジトリを追加しています。

デフォルトのもので良い場合は、リポジトリ追加は必要ないです。

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt -y update
sudo apt install git
git --version

gitのrebaseなどで使うデフォルトのエディタをvimで設定

sudo update-alternatives --config editor

git用のSSH Key作成

mkdir ~/.ssh
cd ~/.ssh
ssh-keygen -t ed25519 -C daijinload@gmail.com -f id_ed25519_daijinload
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_daijinload
cat id_ed25519_daijinload.pub

githubにSSH Keyを設定する

https://github.com/daijinload

右上アイコン - Settings - SSH and GPG keys - New SSH keyボタン

githubでリポジトリの作成

https://github.com/daijinload

git のデフォルトユーザとデフォルトsshkeyを設定する

git config --global user.name daijinload
git config --global user.email daijinload@gmail.com
git config --global core.sshCommand 'ssh -i ~/.ssh/id_ed25519_daijinload'

SSH Keyが今回のように1個しかない場合、core.sshCommandは、設定しなくても良いです。

しかし、複数SSH Keyがある場合は、メインのものを明示的に指定しておいたほうが確実です。

【補足】Githubを画面から操作したときのユーザ名とメールアドレスを合わせておいたほうが良い

特に問題が発生するわけではないですが、今ではアカウントを作るとダミーのメールアドレスがコミットログに付くようになっています。

Settings - Emails Keep my email addresses private のチェックボックスをオンにする(デフォルトでオンとなっている)

コンソールで自分のメールアドレスを設定してしまうと、画面操作時のコミットログにはダミーメールアドレスとなり、

コンソールからのコミットでは、自分のメールアドレスとなり一貫性も無いため、ダミーのほうに合わせるのを推奨します。

画面から適当にリポジトリを作成して、README.mdの作成にチェックを入れておけば、コミットログが作成されるため、

そちらのコミットログのユーザとメールアドレスに合わせるようにすると良いと思います。

一応、チェックボックスの周辺にダミーのメールアドレスも表示されるので、そちらを設定しても良いですが、SSH keyの動作確認のついででやるのが良いかと思います。

sshでclone

httpでcloneするときと記載が違うので注意です。git@から始まるのがSSH key指定です。

Githubの画面からコピー出来ますが、SSH keyのほうをコピーしてください。

git clone git@github.com:daijinload/test2.git
> SSH key作成時に設定したパスワードを入れる(パスワードを設定していなければ、入力の必要はありません)

また、clone時に特定のSSH keyを指定したい場合は下記です。

git -c core.sshCommand="ssh -i ~/.ssh/id_ed25519_daijinload" clone git@github.com:daijinload/test2.git

githubのアカウントが複数あり、cloneしたリポジトリのみ、git userを切り替える場合

cd test2
git config --local user.name daijinload
git config --local user.email daijinload@gmail.com
git config --local core.sshCommand 'ssh -i ~/.ssh/id_ed25519_daijinload'

cloneしたリポジトリのみに適用されるため、clone時のみ気をつければ良いため、おすすめです。

試しに修正をして反映してみる

vi README.md 
git commit -am 'add 1'
git log
git push origin main

実際に見に行って見ます。

https://github.com/daijinload?tab=repositories

git branch console view

cd
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

zshもあるので環境によって選んでください

@see https://github.com/git/git/tree/master/contrib/completion

setup git branch view

cat << 'EOS' >> .bashrc
source ~/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '
EOS

GUI Diff Tool meld インストール

基本的に使ってないが、VSCODEなどの環境が無くても使えるので入れています。

sudo add-apt-repository -y ppa:sicklylife/ppa
sudo apt install -y meld

git config --global diff.tool meld
git config --global difftool.meld.path "/usr/bin/meld"
git config --global difftool.prompt false
git config --global merge.tool meld
git config --global mergetool.meld.path "/usr/bin/meld"
git config --global mergetool.prompt false

# -------------------

# 個別でDiffする場合
git difftool ファイル名

# 全ファイル対象でDiffする場合
git difftool -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment