ฉันเข้าใจวิธีสร้างนามแฝงใน PowerShell สำหรับ cmdlets ได้ดี แต่ฉันต้องการสร้างนามแฝงใน PowerShell สำหรับสิ่งต่าง ๆ เช่น git status
เป็นเพียง gs
และ git pull origin master
เป็น gpm
ใครสามารถชี้ฉันไปในทิศทางที่ถูกต้อง?
การสร้างนามแฝงใน PowerShell สำหรับคำสั่ง git?
คำตอบ (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 เพื่อสร้างนามแฝงให้กับคำสั่งที่มีพารามิเตอร์ ไปป์ไลน์ ฯลฯ
Get-GitStatus
ของคุณเองตามด้านบน คุณจะต้องเขียนทับ Get-GitStatus
ของ posh-git ซึ่งใช้เพื่อรับข้อมูลสถานะที่มีโครงสร้างเพื่อสร้างพรอมต์ที่กำหนดเอง
- person Andrew Spencer; 09.02.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
s
แล้วเรียกมันโดยตรงเป็นคำสั่ง
- person Daniel Werner; 08.01.2021
ฉันไม่รู้จัก PowerShell แต่คุณสามารถตั้งค่านามแฝงได้โดยตรงใน Git
ฉันสร้าง posh-git-alias ซึ่งคุณสามารถเพิ่มลงใน PowerShell $PROFILE
ของคุณได้
คุณต้องสร้างไฟล์ profile.ps1 วางไว้ในโฟลเดอร์ชื่อ WindowsPowerShell ในเอกสารของฉัน
จากนั้นใส่ profile.ps1 ในบรรทัดดังนี้:
set-alias wit 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\witadmin.exe'