การสร้างนามแฝงใน PowerShell สำหรับคำสั่ง git?

ฉันเข้าใจวิธีสร้างนามแฝงใน PowerShell สำหรับ cmdlets ได้ดี แต่ฉันต้องการสร้างนามแฝงใน PowerShell สำหรับสิ่งต่าง ๆ เช่น git status เป็นเพียง gs และ git pull origin master เป็น gpm ใครสามารถชี้ฉันไปในทิศทางที่ถูกต้อง?


person Richard    schedule 18.05.2010    source แหล่งที่มา


คำตอบ (5)


คุณจะต้องสร้างฟังก์ชันก่อนซึ่งมีคำสั่งของคุณอยู่ในนั้น จากนั้นสร้างนามแฝงให้กับฟังก์ชันนั้น

PS C:\Users\jpogran\code\git\scripts> function get-gitstatus { git status }

PS C:\Users\jpogran\code\git\scripts> get-gitstatus
# On branch master
nothing to commit (working directory clean)

PS C:\Users\jpogran\code\git\scripts> Set-Alias -Name gs -Value get-gitstatus

PS C:\Users\jpogran\code\git\scripts> gs
# On branch master
nothing to commit (working directory clean)

คุณอาจสนใจโปรเจ็กต์ระบบปฏิบัติการชื่อ posh-git ซึ่งมีจุดมุ่งหมายเพื่อจัดเตรียมสภาพแวดล้อม PowerShell สำหรับ คำสั่งคอมไพล์ ล้อมคำสั่ง git ด้วยฟังก์ชันประเภท PS และยังมีพร้อมต์ใหม่ที่แสดงสถานะและสาขาในพร้อมท์ของคุณ

แก้ไข: ลืมเพิ่มวิธีค้นหาวิธีการทำเช่นนี้โดยใช้ Powershell

PS C:\Users\jpogran\code\git\scripts> get-help set-alias -examples

นี่จะแสดงตัวอย่าง (อันสุดท้ายใช้ที่นี่) ของวิธีใช้ set-alias เพื่อสร้างนามแฝงให้กับคำสั่งที่มีพารามิเตอร์ ไปป์ไลน์ ฯลฯ

person James Pogran    schedule 18.05.2010
comment
คะแนนโบนัสสำหรับการเพิ่มฟังก์ชัน git+นามแฝงในสคริปต์แบบสแตนด์อโลน และให้มาจากโปรไฟล์ของคุณ - person Goyuix; 18.05.2010
comment
technet.microsoft.com/library/hh849938.aspx#sectionSection8 สำหรับออนไลน์ ตัวอย่าง :) - person Chris Pfohl; 29.08.2014
comment
หากคุณกำหนดฟังก์ชัน Get-GitStatus ของคุณเองตามด้านบน คุณจะต้องเขียนทับ Get-GitStatus ของ posh-git ซึ่งใช้เพื่อรับข้อมูลสถานะที่มีโครงสร้างเพื่อสร้างพรอมต์ที่กำหนดเอง - person Andrew Spencer; 09.02.2020
comment
ทำไมต้องมีนามแฝงอีกเมื่อคุณมีฟังก์ชั่น? เพียงใส่ชื่อฟังก์ชั่นสั้น ๆ เหมือนที่ @alex ทำกับเด็กหรู ähh git - person Timo; 23.10.2020

เพิ่งสร้างทางลัดสำหรับตัวเองและต้องการแบ่งปัน:

สร้างโปรไฟล์ PowerShell (หากคุณยังไม่มี):

New-Item -Type file -Path $PROFILE -Force

เปิดเพื่อแก้ไข:

notepad $PROFILE

เพิ่มฟังก์ชันและนามแฝงต่อไปนี้:

function Get-GitStatus { & git status $args }
New-Alias -Name s -Value Get-GitStatus
function Set-GitCommit { & git commit -am $args }
New-Alias -Name c -Value Set-GitCommit

เมื่อคุณเริ่มเซสชัน PowerShell ใหม่ คุณควรจะสามารถส่งผ่านอาร์กิวเมนต์ไปยังนามแฝงได้เช่นกัน เช่น.:

c "This is a commit message"

อัปเดต:

ต่อไปนี้คือทางลัดบางส่วนที่ใช้บ่อยของฉัน:

function Get-GitStatus { & git status -sb $args }
New-Alias -Name s -Value Get-GitStatus -Force -Option AllScope
function Get-GitCommit { & git commit -ev $args }
New-Alias -Name c -Value Get-GitCommit -Force -Option AllScope
function Get-GitAdd { & git add --all $args }
New-Alias -Name ga -Value Get-GitAdd -Force -Option AllScope
function Get-GitTree { & git log --graph --oneline --decorate $args }
New-Alias -Name t -Value Get-GitTree -Force -Option AllScope
function Get-GitPush { & git push $args }
New-Alias -Name gps -Value Get-GitPush -Force -Option AllScope
function Get-GitPull { & git pull $args }
New-Alias -Name gpl -Value Get-GitPull -Force -Option AllScope
function Get-GitFetch { & git fetch $args }
New-Alias -Name f -Value Get-GitFetch -Force -Option AllScope
function Get-GitCheckout { & git checkout $args }
New-Alias -Name co -Value Get-GitCheckout -Force -Option AllScope
function Get-GitBranch { & git branch $args }
New-Alias -Name b -Value Get-GitBranch -Force -Option AllScope
function Get-GitRemote { & git remote -v $args }
New-Alias -Name r -Value Get-GitRemote -Force -Option AllScope
person Aaron Tribou    schedule 21.04.2014
comment
& ทำอะไร? - person Matt W; 30.03.2017
comment
@MattW ตัวดำเนินการเครื่องหมายและบังคับให้ PowerShell ดำเนินการอาร์กิวเมนต์ต่อไปนี้เป็นคำสั่ง (CMD) อาจไม่จำเป็นเสมอไป แต่ฉันพบว่าสามารถหลีกเลี่ยงผลกระทบที่ไม่ได้ตั้งใจซึ่งอาจเป็นผลมาจากการแทรกอาร์กิวเมนต์ที่ส่วนท้ายของคำสั่ง ($args) ที่ได้รับการประเมินอย่างไม่ถูกต้อง - person Aaron Tribou; 01.04.2017
comment
คุณไม่จำเป็นต้องใช้นามแฝงอีกต่อไปในกรณีนี้ คุณสามารถตั้งชื่อฟังก์ชันของคุณเช่น s แล้วเรียกมันโดยตรงเป็นคำสั่ง - person Daniel Werner; 08.01.2021

ฉันไม่รู้จัก PowerShell แต่คุณสามารถตั้งค่านามแฝงได้โดยตรงใน Git

person nicoulaj    schedule 18.05.2010
comment
ลิงก์ด้านบนใช้ไม่ได้สำหรับฉัน ลิงก์นี้ให้ข้อมูลที่คล้ายกัน git.wiki.kernel.org/articles /a/l/i/Aliases.html - person Joe Simmonds; 18.04.2012

ฉันสร้าง posh-git-alias ซึ่งคุณสามารถเพิ่มลงใน PowerShell $PROFILE ของคุณได้

person Alexander Zeitler    schedule 30.05.2015
comment
ขอบคุณสำหรับสิ่งเหล่านั้น! มีประโยชน์มากสำหรับฉัน - person tomd; 22.02.2016
comment
5ปีผ่านไปก็ยังทันสมัยอยู่ คุณควรเปลี่ยนชื่อรายการเป็นฟังก์ชัน git ที่หรูหรา ... - person Timo; 23.10.2020

คุณต้องสร้างไฟล์ profile.ps1 วางไว้ในโฟลเดอร์ชื่อ WindowsPowerShell ในเอกสารของฉัน

จากนั้นใส่ profile.ps1 ในบรรทัดดังนี้:

set-alias wit 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\witadmin.exe'
person Alex    schedule 18.05.2010