2
3

More than 1 year has passed since last update.

Windows の PowerShell(posh-git) で git を利用する

Last updated at Posted at 2023-08-19

はじめに

Windows の PowerShell(posh-git) で git を便利に利用する手順をあらためて整理しました。
過去の投稿から最低限必要な個所のみ抜粋して更新しています。

前提

  • Windows 11 での手順となります。
  • PowerShell 7 がインストールされていること。(PowerShellでgitを操作します。)
  • Visual Studio Code がインストールされていること。(VSCodeでgitの設定を行っています。)

Git のインストール

  • Git for Windows をダウンロードします。

    Git - Downloading Package

  • ダウンロードしたセットアップ(例:Git-2.41.0.3-64-bit.exe)を実行します。

  • インストール先のパス(例:D:\Develop\Bin\Git)を指定します。

    スペースや全角文字を含まないパスが良いです。

  • 「Select Components」で、インストールするコンポーネントを選択します。

  • 「Choosing the default editor used by git」で、既定のエディタとして「Use Visual Studio Code as Git's default editor」を選択します。

    既定のエディタは後で変更可能です。

  • 「Adjusting the name of the initial branch in new repositories」で、新しいリポジトリで最初に作成するブランチ名を指定する場合は「Override the default branch name for new repositories」を選択して、初期ブランチ名を指定します。

    github の初期ブランチ名に合わせて main にするのも良いかもです。

  • 「Adjusting your PATH environment」で、コマンドラインやPowerShellなどからGitのコマンドを利用するために「Git from the command line and also from 3rd-party software」を選択します。

  • 「Choosing the SSH executable」で、「Use bundled OpenSSH」を選択します。

  • 「Choosing HTTPS transport backend」で、「Use the OpenSSL Library」を選択します。

  • 「Configuring the line ending conversions」で、「Checkout as-is, commit as-is」を選択します。この場合、チェックアウト時とコミット時に改行コードを変更しません。

    改行コードを統一したい場合は「Checkout Windows-style, commit Unix-style line endings」もありです。

  • 「Configuring the terminal emulator to use with Git Bash」で、「Use Windows’s default console window」を選択します。

  • 「Choose the default behavior of 'git pull'」で、「Default (fast-forward or merge)」を選択します。'git pull'を行った際の既定の動作を選択できます。

  • 「Choose a credential helper」で、「Git Credential Manager」を選択します。

  • 「Configuring extra options」で、「Enable file system cashing」のみチェックします。

  • 「Configuring experimental options」の試験的オプションはとりあえずチェックなし。

Git のアップデート

  • 基本的には新しいバージョンのセットアップを実行することで更新できます。

Git のグローバル設定

  • インストールした Git の「Git bash」を起動します。

  • 下記コマンドを入力してユーザーや日本語環境の設定を行います。

Git bash
$ git config --global user.name "Mei Sei"            # 氏名の指定
$ git config --global user.email "hoge@example.com"  # メールアドレスの指定
$ git config --global color.ui auto                  # 文字の色付け
$ git config --global core.quotepath false           # git status の文字化け回避

Git の初期化とコミットとリモート接続とプッシュとプル

PowerShell
> git init (git 初期化)
> git add -A
> git commit -m "first commit"
> git remote add origin (url)
> git branch (ブランチ確認)
> git push --set-upstream origin master
> git pull origin master

Git の他ヒストリからプッシュしている場合はマージやリベース

PowerShell
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/hoge/fuga.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

> git fetch origin master
> git merge --allow-unrelated-histories origin master

> git fetch origin master
> git rebase origin master

PowerShell で git diff が文字化けする場合

LANG 環境変数を設定します。

PowerShell
> SETX LANG ja_JP.UTF-8

posh-git のインストール

PowerShellでgitの操作を便利に行うために、posh-gitを導入します。

posh-git is a PowerShell module that integrates Git and PowerShell by providing Git status summary information that can be displayed in the PowerShell prompt, e.g.:

C:\Users\Keith\GitHub\posh-git [main  +0 ~1 -0 | +0 ~1 -0 !]>

posh-git also provides tab completion support for common git commands, branch names, paths and more. For example, with posh-git, PowerShell can tab complete git commands like checkout by typing git ch and pressing the tab key. That will tab complete to git checkout and if you keep pressing tab, it will cycle through other command matches such as cherry and cherry-pick. You can also tab complete remote names and branch names e.g.: git pull or<tab> ma<tab> tab completes to git pull origin main.

  • 管理者権限でPowerShellのコンソールを起動して以下のコマンドを入力します。
PowerShell
> Get-ExecutionPolicy
  • コマンドの結果がRestrictedの場合は、署名付きのコマンドレットであれば使用できるようにセキュリティ設定を変更します。
PowerShell
> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
  • posh-gitをインストールします。
PowerShell
> 初めてインストールする場合
> PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
> アップデートなどする場合
> PowerShellGet\Install-Module posh-git -Scope CurrentUser
  • posh-gitのモジュールをインポートします。
PowerShell
> Import-Module posh-git
  • 自動でモジュールが読み込まれるようにPowerShellのプロファイルを変更します。
PowerShell
> code $profile
Import-Module posh-git
  • PowerShellのプロファイルを再読み込みします。
PowerShell
> . $profile
  • posh-git は PowerShell で git コマンドを利用し、Git ステータスの概要情報を表示できるため大変便利です。
PowerShell
> git clone https://github.com/kerobot/holocrawler.git
Cloning into 'holocrawler'...
remote: Enumerating objects: 115, done.
remote: Counting objects: 100% (115/115), done.
Receiving objects: 100% (115/115), 88.16 KiB | 9.79 MiB/s, done.
remote: Compressing objects: 100% (74/74), done.
remote: Total 115 (delta 66), reused 85 (delta 36), pack-reused 0
Resolving deltas: 100% (66/66), done.
> git status
fatal: not a git repository (or any of the parent directories): .git
> cd .\holocrawler\
holocrawler [main ]> git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
holocrawler [main ]> ls

    Directory: holocrawler

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2023/08/20     1:19                .vscode
d----          2023/08/20     1:19                app
d----          2023/08/20     1:19                config
d----          2023/08/20     1:19                tests
-a---          2023/08/20     1:19            260 .env.sample
-a---          2023/08/20     1:19            102 .gitignore
-a---          2023/08/20     1:19           2386 main.py
-a---          2023/08/20     1:19          86822 poetry.lock
-a---          2023/08/20     1:19            544 pyproject.toml
-a---          2023/08/20     1:19           2260 README.md

holocrawler [main ]> git push origin main
info: please complete authentication in your browser...
Everything up-to-date
holocrawler [main ]>

Git bash のエディタの設定と利用

  • 「Git bash」を起動します。

  • コミットメッセージのエディタを変更する場合は、~/.gitconfigを編集します。

Git bash
$ vi ~/.gitconfig
  • 以下のように設定します。(Visual Studio Codeを利用する場合です。)
[core]
    editor = code --wait
  • (参考)グローバル設定でも可能です。
Git bash
$ git config --global core.editor "code --wait"
  • (参考)プロジェクトのディレクトリを起点としてVisual Studio Codeを起動すると、Visual Studio Codeからgitを操作できます。
PowerShell
$ code .
2
3
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
2
3