在 Windows 的 PowerShell 中使用 posh-git 命令来操作 git

首先

我重新整理了在Windows的PowerShell (posh-git) 上使用git方便的步骤。我从过去的帖子中提取出必要的部分并进行了更新。

前提 tí)

    • 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是一个PowerShell模块,将Git和PowerShell集成在一起,提供Git状态摘要信息,可以在PowerShell提示中显示,例如:

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

posh-git还为常见的git命令、分支名称、路径等提供了制表符补全支持。例如,使用posh-git,PowerShell可以通过输入git ch并按下制表键来制表符补全git命令,如checkout。它会补全为git checkout,如果继续按下制表键,它会循环显示其他命令匹配,如cherry和cherry-pick。你还可以制表符补全远程名称和分支名称,例如:git pull或 ma会补全为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 .
广告
将在 10 秒后关闭
bannerAds