Windows의 표준 CLI 터미널인 cmd.exe 는 Git을 사용하기에 별로 좋지 않다. PowerShell을 쓸 줄 안다면 PowerShell을 사용하는 것이 낫다.
PowerShell을 Windows가 아닌 Linux나 macOS 같은 환경에서 사용한다면 이어지는 내용을 동일하게 적용해 볼 수 있다.
Posh-Git(https://github.com/dahlbyk/posh-git) 이라는 프로젝트가 있어서 Tab 자동완성과 저장소 상태를 보여주는 프롬프트도 사용할 수 있다.
PowerShell 스크립트가 실행되게 하려면 우선 'ExecutionPolicy' 정책을 'RemoteSigned' 값('Undefined' 또는 'Restricted' 제외한 허용)으로 변경해야 한다. 'RemoteSigned' 값이 아닌 'AllSigned' 값으로 설정하면 스크립트를 실행하기 위해 전자서명 과정이 필요하다. 'RemoteSigned' 값으로 설정하면 'ZoneIdentifier’가 'Internet’으로 설정된 경우(주로 웹에서 다운로드한 파일)만 전자서명이 필요하고 나머지 파일에 대해서는 전자서명이 필요하지 않다. Windows 시스템의 관리자 권한이 있다면 모든 사용자가 PowerShell을 사용할 수 있도록 "-Scope LocalMachine" 옵션을 사용할 수 있다. 일반적인 사용자라면 관리자 권한이 없기 때문에 "-Scope CurrentUser" 옵션으로 자신만이 사용가능하도록 설정할 수 있다.
PowerShell의 Scope에 대한 자세한 내용은 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes 에서 찾아볼 수 있다.
PowerShell의 ExecutionPolicy에 대한 자세한 내용은 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy 에서 찾아볼 수 있다.
모든 사용자를 대상으로 `ExecutionPolicy`의 값을 `RemoteSigned`로 변경하려면 다음 명령을 실행한다:
> Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -ForcePowerShell 버전 5 이상을 사용하거나 PowerShell 4를 PackageManagement 모듈(Cmdlets)을 설치해서 사용한다면 패키지 관리자를 통해서 posh-git을 설치할 수 있다.
자세한 정보는 ttps://docs.microsoft.com/en-us/powershell/scripting/gallery/overview[] 에서 확인할 수 있다.
> Install-Module posh-git -Scope CurrentUser -Force
> Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force # Newer beta version with PowerShell Core support사용자 모두가 사용할 수 있도록 posh-git을 설치하려면 -Scope CurrentUser 옵션을 사용하며, 관리자 권한으로 실행한 PowerShell 콘솔에서 명령을 실행해야 한다.
코드의 두 번째 명령을 실행했을 때 Module 'PowerShellGet' was not installed by using Install-Module 같은 오류 메시지가 발생한다면 다음 명령을 먼저 실행해보기 바란다.
> Install-Module PowerShellGet -Force -SkipPublisherCheck이 명령을 실행한 후에 앞서 실패한 명령을 다시 실행해본다. 이 오류는 Windows PowerShell과 함께 배포된 모듈의 인증서가 일치하지 않기 때문이다.
PowerShell 프롬프트에 Git 정보를 표시하려면 posh-git을 Import 해야 한다.
이를 자동으로 하려면 import 구문을 $profile 스크립트에 추가하면 되는데 Add-PoshGitToProfile 명령을 통해 가능하다.
이 스크립트는 PowerShell 콘솔을 새로 실행할 때 마다 실행된다.
염두해 둘 부분은 $profile 스크립트가 다수라는 점이다.
예를 들어 어떤 것은 PowerShell 콘솔을 위한 스크립트이고 어떤 것은 ISE(통합 스크립팅 환경)를 위한 스크립트이다.
> Import-Module posh-git
> Add-PoshGitToProfile -AllHostshttps://github.com/dahlbyk/posh-git/releases 에서 posh-git을 내려받아 압축을 푼다.
그리고 posh-git.psd1 파일의 절대 경로를 입력하여 Import-Module 명령으로 모듈을 불러들인다.
> Import-Module <path-to-uncompress-folder>\src\posh-git.psd1
> Add-PoshGitToProfile -AllHosts이렇게 profile.ps1 파일 내 적절한 위치에 추가한 posh-git 은 PowerShell를 다음 열 때 부터 적용된다.
프롬프트에 표시되는 Git 상태 정보에 대한 자세한 내용은 https://github.com/dahlbyk/posh-git/blob/master/README.md#git-status-summary-information 에서 찾아볼 수 있다. posh-git 프롬프트를 사용자화 하기 위한 자세한 정보는 https://github.com/dahlbyk/posh-git/blob/master/README.md#customization-variables 에서 찾아볼 수 있다.
