Chapters ▾
  1. 1. Getting Started

    1. 1.1 About Version Control
    2. 1.2 A Short History of Git
    3. 1.3 What is Git?
    4. 1.4 The Command Line
    5. 1.5 Installing Git
    6. 1.6 First-Time Git Setup
    7. 1.7 Getting Help
    8. 1.8 Summary
  2. 2. Git Basics

    1. 2.1 Getting a Git Repository
    2. 2.2 Recording Changes to the Repository
    3. 2.3 Viewing the Commit History
    4. 2.4 Undoing Things
    5. 2.5 Working with Remotes
    6. 2.6 Tagging
    7. 2.7 Git Aliases
    8. 2.8 Summary
  3. 3. Git Branching

    1. 3.1 Branches in a Nutshell
    2. 3.2 Basic Branching and Merging
    3. 3.3 Branch Management
    4. 3.4 Branching Workflows
    5. 3.5 Remote Branches
    6. 3.6 Rebasing
    7. 3.7 Summary
  4. 4. Git on the Server

    1. 4.1 The Protocols
    2. 4.2 Getting Git on a Server
    3. 4.3 Generating Your SSH Public Key
    4. 4.4 Setting Up the Server
    5. 4.5 Git Daemon
    6. 4.6 Smart HTTP
    7. 4.7 GitWeb
    8. 4.8 GitLab
    9. 4.9 Third Party Hosted Options
    10. 4.10 Summary
  5. 5. Distributed Git

    1. 5.1 Distributed Workflows
    2. 5.2 Contributing to a Project
    3. 5.3 Maintaining a Project
    4. 5.4 Summary
  1. 6. GitHub

    1. 6.1 Account Setup and Configuration
    2. 6.2 Contributing to a Project
    3. 6.3 Maintaining a Project
    4. 6.4 Managing an organization
    5. 6.5 Scripting GitHub
    6. 6.6 Summary
  2. 7. Git Tools

    1. 7.1 Revision Selection
    2. 7.2 Interactive Staging
    3. 7.3 Stashing and Cleaning
    4. 7.4 Signing Your Work
    5. 7.5 Searching
    6. 7.6 Rewriting History
    7. 7.7 Reset Demystified
    8. 7.8 Advanced Merging
    9. 7.9 Rerere
    10. 7.10 Debugging with Git
    11. 7.11 Submodules
    12. 7.12 Bundling
    13. 7.13 Replace
    14. 7.14 Credential Storage
    15. 7.15 Summary
  3. 8. Customizing Git

    1. 8.1 Git Configuration
    2. 8.2 Git Attributes
    3. 8.3 Git Hooks
    4. 8.4 An Example Git-Enforced Policy
    5. 8.5 Summary
  4. 9. Git and Other Systems

    1. 9.1 Git as a Client
    2. 9.2 Migrating to Git
    3. 9.3 Summary
  5. 10. Git Internals

    1. 10.1 Plumbing and Porcelain
    2. 10.2 Git Objects
    3. 10.3 Git References
    4. 10.4 Packfiles
    5. 10.5 The Refspec
    6. 10.6 Transfer Protocols
    7. 10.7 Maintenance and Data Recovery
    8. 10.8 Environment Variables
    9. 10.9 Summary
2nd Edition

A1.8 Appendix A: Git in Other Environments - Git in PowerShell

Git in PowerShell

The legacy command-line terminal on Windows (cmd.exe) isn’t really capable of a customized Git experience, but if you’re using PowerShell, you’re in luck. This also works if you’re running PowerShell Core on Linux or macOS. A package called posh-git (https://github.com/dahlbyk/posh-git) provides powerful tab-completion facilities, as well as an enhanced prompt to help you stay on top of your repository status. It looks like this:

PowerShell with Posh-git
Figure 187. PowerShell with Posh-git

Installation

Prerequisites (Windows only)

Before you’re able to run PowerShell scripts on your machine, you need to set your local ExecutionPolicy to RemoteSigned (basically, anything except Undefined and Restricted). If you choose AllSigned instead of RemoteSigned, also local scripts (your own) need to be digitally signed in order to be executed. With RemoteSigned, only scripts having the ZoneIdentifier set to Internet (were downloaded from the web) need to be signed, others not. If you’re an administrator and want to set it for all users on that machine, use -Scope LocalMachine. If you’re a normal user, without administrative rights, you can use -Scope CurrentUser to set it only for you.

To set the value of ExecutionPolicy to RemoteSigned for all users use the next command:

> Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force

If you have at least PowerShell 5 or PowerShell 4 with PackageManagement installed, you can use the package manager to install posh-git for you.

> Install-Module posh-git -Scope CurrentUser -Force
> Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force # Newer beta version with PowerShell Core support

If you want to install posh-git for all users, use -Scope AllUsers instead and execute the command from an elevated PowerShell console. If the second command fails with an error like Module 'PowerShellGet' was not installed by using Install-Module, you’ll need to run another command first:

> Install-Module PowerShellGet -Force -SkipPublisherCheck

Then you can go back and try again. This happens, because the modules that ship with Windows PowerShell are signed with a different publishment certificate.

Update PowerShell Prompt

To include Git information in your prompt, the posh-git module needs to be imported. To have posh-git imported every time PowerShell starts, execute the Add-PoshGitToProfile command which will add the import statement into your $profile script. This script is executed everytime you open a new PowerShell console. Keep in mind, that there are multiple $profile scripts. E.g. one for the console and a separate one for the ISE.

> Import-Module posh-git
> Add-PoshGitToProfile -AllHosts

From Source

Just download a posh-git release from https://github.com/dahlbyk/posh-git/releases, and uncompress it. Then import the module using the full path to the posh-git.psd1 file:

> Import-Module <path-to-uncompress-folder>\src\posh-git.psd1
> Add-PoshGitToProfile -AllHosts

This will add the proper line to your profile.ps1 file, and posh-git will be active the next time you open PowerShell.

For a description of the Git status summary information displayed in the prompt see: https://github.com/dahlbyk/posh-git/blob/master/README.md#git-status-summary-information For more details on how to customize your posh-git prompt see: https://github.com/dahlbyk/posh-git/blob/master/README.md#customization-variables.

AltStyle によって変換されたページ (->オリジナル) /