ฉันเห็นหน้าจอที่ใครบางคนได้รับ
git st
git ci
ไปทำงาน. เมื่อฉันทำฉันได้รับข้อผิดพลาดถามฉันว่าฉันหมายถึงอย่างอื่น
ฉันอยากรู้ว่าคุณต้องทำอะไรเพื่อให้เสร็จ
ฉันเห็นหน้าจอที่ใครบางคนได้รับ
git st
git ci
ไปทำงาน. เมื่อฉันทำฉันได้รับข้อผิดพลาดถามฉันว่าฉันหมายถึงอย่างอื่น
ฉันอยากรู้ว่าคุณต้องทำอะไรเพื่อให้เสร็จ
คำตอบ:
โดยทั่วไปคุณเพียงแค่ต้องเพิ่มบรรทัด ~/.gitconfig
[alias]
st = status
ci = commit -v
หรือคุณสามารถใช้คำสั่ง git config alias:
$ git config --global alias.st status
บนยูนิกซ์ให้ใช้เครื่องหมายคำพูดเดี่ยวหากชื่อแทนมีช่องว่าง
$ git config --global alias.ci 'commit -v'
บน windows ให้ใช้เครื่องหมายคำพูดคู่หากนามแฝงมีช่องว่างหรืออาร์กิวเมนต์บรรทัดคำสั่ง:
c:\dev> git config --global alias.ci "commit -v"
คำสั่ง alias ยังยอมรับฟังก์ชันเป็นพารามิเตอร์ ลองดูที่ชื่อแทน
git config --global
แทนนามแฝง~/.gitconfig
แทนที่.git/config
เก็บปัจจุบันของคุณ
st
เป็นstatus -s
(สถานะสั้น)
'
เช่นgit config --global alias.sr 'svn rebase'
"
แทนการใส่เครื่องหมายคำพูดเดี่ยวเมื่อเพิ่มคำสั่งด้วยช่องว่างเช่นgit config --global alias.ci "commit -v"
ดังที่คนอื่น ๆ ได้กล่าวถึงวิธีที่เหมาะสมในการเพิ่มชื่อแทน git ใน.gitconfig
ไฟล์ทั่วโลกของคุณไม่ว่าจะโดยการแก้ไข~/.gitconfig
หรือโดยการใช้git config --global alias.<alias> <git-command>
คำสั่ง
ด้านล่างนี้เป็นสำเนาของส่วนนามแฝงของ~/.gitconfig
ไฟล์ของฉัน:
[alias]
st = status
ci = commit
co = checkout
br = branch
unstage = reset HEAD --
last = log -1 HEAD
นอกจากนี้หากคุณกำลังใช้ bash ฉันขอแนะนำให้ตั้งค่า bash complete ด้วยการคัดลอกgit-completion.bash
ไปยังโฮมไดเร็กตอรี่ของคุณ~/.bashrc
แล้วทำการหาจากbash (ฉันเชื่อว่าฉันเรียนรู้เกี่ยวกับสิ่งนี้จากหนังสือออนไลน์Pro Git ) ใน Mac OS X ฉันทำได้โดยใช้คำสั่งต่อไปนี้:
# Copy git-completion.bash to home directory
cp usr/local/git/contrib/completion/git-completion.bash ~/
# Add the following lines to ~/.bashrc
if [ -x /usr/local/git/bin/git ]; then
source ~/.git-completion.bash
fi
หมายเหตุ:การทุบตีจะเสร็จสมบูรณ์ไม่เพียง แต่จะใช้กับคำสั่ง git มาตรฐานเท่านั้น แต่ยังใช้ได้กับนามแฝง git ของคุณด้วย
ในที่สุดเพื่อลดการกดแป้นจริง ๆ ฉันได้เพิ่มสิ่งต่อไปนี้ลงใน~/.bash_aliases
ไฟล์ซึ่งมาจาก~/.bashrc
:
alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcotb='git checkout --track -b'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'
~/.bashrc
ไฟล์"ได้มาจาก" หมายความว่าอะไร
~/.bashrc
: เพื่อลดการกดแป้น สิ่งที่ต้องการ
ฉันคิดว่า gitconfig ที่มีประโยชน์มากที่สุดเป็นเช่นนี้เรามักจะใช้ฟังก์ชั่น 20% ใน git คุณสามารถลอง "g ll" มันน่าทึ่งรายละเอียด:
[user]
name = my name
email = me@example.com
[core]
editor = vi
[alias]
aa = add --all
bv = branch -vv
ba = branch -ra
bd = branch -d
ca = commit --amend
cb = checkout -b
cm = commit -a --amend -C HEAD
ci = commit -a -v
co = checkout
di = diff
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
mm = merge --no-ff
st = status --short --branch
tg = tag -a
pu = push --tags
un = reset --hard HEAD
uh = reset --hard HEAD^
[color]
diff = auto
status = auto
branch = auto
[branch]
autosetuprebase = always
คุณต้องการgit config alias
คำสั่ง ดำเนินการต่อไปนี้ในที่เก็บ Git:
git config alias.ci commit
สำหรับนามแฝงทั่วโลก:
git config --global alias.ci commit
สิ่งนี้ใช้ได้กับฉัน:
bco = "!f(){ git branch ${1} && git checkout ${1}; };f"
บน:
$ git --version
git version 1.7.7.5 (Apple Git-26)
git cob
ผมชอบ ทำให้ฉันนึกถึงฤดูร้อนเช่นเดียวกับในซังข้าวโพด จริง ๆ แล้วเป็นคำที่ดีที่เราคิดไม่ถึง ... ซังนั่นคือ
สิ่งนี้จะสร้างนามแฝงst
สำหรับstatus
:
git config --add alias.st status
git st
เมื่อคุณสามารถใช้งานgit s
ให้กำจัดสิ่งนั้นs
: P
Follwing เป็นทางลัด 4 git หรือนามแฝงที่คุณใช้เพื่อประหยัดเวลา
เปิด commandline และพิมพ์คำสั่งเหล่านี้ด้านล่าง 4 และใช้ทางลัดหลังจาก
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
ลองทดสอบดูสิ!
$ git co # use git co instead of git checkout
$ git ci # use git ci instead of git commit
$ git st # use git st instead of git status
$ git br # use git br instead of git branch
สำหรับผู้ที่ต้องการรันคำสั่งเชลล์ในนามแฝง gitตัวอย่างเช่น:
$ git pof
ใน terminal ของฉันจะผลักดันสาขาปัจจุบันไปยัง repo กำเนิดของฉัน:
[alias]
pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)
ที่ไหน
$(git branch | grep \\* | cut -d ' ' -f2)
คำสั่งส่งคืนสาขาปัจจุบัน
ดังนั้นนี่คือทางลัดสำหรับพิมพ์ชื่อสาขาด้วยตนเอง:
git push origin -f <current-branch>
git push -f origin HEAD
เพื่อผลักดันสาขาปัจจุบันไปยังคู่ที่ห่างไกล นอกจากนี้ทางลัดที่จะผลักด้วยแรง? หากคุณต้องออกแรงบ่อยพอที่จะได้รับประโยชน์จากทางลัดไม่ได้มีอะไรผิดปกติในการตั้งค่าหรือเวิร์กโฟลว์ของคุณ?
!git
ด้วยคำสั่ง git ล่าสุด) แต่การแก้ไขไฟล์ config ด้วยตนเองได้หลอกลวง
คุณสามารถนามแฝงทั้งคำสั่ง git และ non-git ดูเหมือนว่าจะมีการเพิ่มในเวอร์ชัน 1.5 ตัวอย่างจากgit config --help
หน้าในเวอร์ชัน 2.5.4 บน Mac ของฉันแสดง:
หากส่วนขยาย alias ถูกเติมหน้าด้วยเครื่องหมายอัศเจรีย์จะถูกใช้เป็นคำสั่งเชลล์
ตัวอย่างเช่นใน.gitconfig
ไฟล์ส่วนกลางของคุณคุณอาจมี:
[alias]
st = status
hi = !echo 'hello'
จากนั้นเรียกใช้พวกเขา:
$ git hi
hello
$ git st
On branch master
...
เพิ่มบรรทัดต่อไปนี้ใน ~ / .gitconfig ในโฮมไดเร็กตอรี่ของคุณ
[alias]
# one-line log
l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
a = add
ap = add -p
c = commit --verbose
ca = commit -a --verbose
cm = commit -m
cam = commit -a -m
m = commit --amend --verbose
d = diff
ds = diff --stat
dc = diff --cached
s = status -s
co = checkout
cob = checkout -b
# list branches sorted by last modified
b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"
# list aliases
la = "!git config -l | grep alias | cut -c 7-"
เมื่อทำเสร็จแล้วคุณสามารถทำgit a
แทนgit add
ตัวอย่าง เช่นเดียวกับคำสั่งอื่น ๆ ภายใต้หัวข้อนามแฝง ..
$ git update git: 'update' ไม่ใช่คำสั่ง git ดู 'git --help' คุณหมายถึงสิ่งนี้หรือไม่ ปรับปรุงโทษ $ git config --global alias.update 'pull -v' $ git update จาก git: //git.kernel.org/pub/scm/git/git = [ถึงวันที่] html -> origin / html = [ถึงวันที่] ดูแล -> ที่มา / ซ่อมบำรุง = [up to date] man -> origin / man = [up to date] master -> origin / master = [ถึงวันที่] ถัดไป -> ที่มา / ถัดไป = [up to date] pu -> origin / pu = [up to date] todo -> origin / todo ทันสมัยอยู่แล้ว
ฉันสร้างนามแฝงdog
เพื่อแสดงกราฟบันทึกการทำงาน:
git config --global alias.dog "log --all --decorate --oneline --graph"
และใช้มันดังต่อไปนี้:
git dog
คุณยังสามารถเชนคำสั่งหากคุณใช้ '!' ผู้ประกอบการวางไข่เปลือก:
aa = !git add -A && git status
$ git aa
ทั้งสองนี้จะเพิ่มไฟล์ทั้งหมดและให้รายงานสถานะกับ
สำหรับวิธีที่สะดวกในการตรวจสอบชื่อแทนของคุณให้เพิ่มชื่อแทนนี้:
alias = config --get-regexp ^alias\\.
จากนั้นอย่างรวดเร็ว$ git alias
จะให้ชื่อแทนปัจจุบันของคุณและสิ่งที่พวกเขาทำ
คุณสามารถตั้งชื่อแทน git ที่กำหนดเองโดยใช้การกำหนดค่าของ git นี่คือไวยากรณ์:
git config --global alias.<aliasName> "<git command>"
ตัวอย่างเช่นหากคุณต้องการนามแฝงเพื่อแสดงรายการไฟล์ที่มีข้อขัดแย้งในการผสานให้เรียกใช้:
git config --global alias.conflicts "diff --name-only --diff-filter=U"
ตอนนี้คุณสามารถใช้คำสั่งด้านบนโดยใช้ "ขัดแย้ง" เท่านั้น:
git conflicts
# same as running: git diff --name-only --diff-filter=U
สำหรับฉัน ( ฉันใช้ mac กับเทอร์มินัล ) ทำงานเฉพาะเมื่อฉันเพิ่มใน. bash_profileและเปิดแท็บอื่นเพื่อโหลดการเปลี่ยนแปลง:
alias gst="git status"
alias gd="git diff"
alias gl="git log"
alias gco="git commit"
alias gck="git checkout"
alias gl="git pull"
alias gpom="git pull origin master"
alias gp="git push"
alias gb="git branch"
เพียงเพื่อให้นามแฝงสั้นลงกว่าวิธีการตั้งค่า git มาตรฐานที่กล่าวถึงในคำตอบอื่น ๆ ฉันได้สร้างแพคเกจ npm mingit ( npm install -g mingit
) เพื่อให้คำสั่งส่วนใหญ่กลายเป็น 2 อักขระแทนที่จะเป็น 2 คำ นี่คือตัวอย่าง:
g a . // git add .
g b other-branch // git branch other-branch
g c "made some changes" // git commit -m "made some changes"
g co master // git checkout master
g d // git diff
g f // git fetch
g i // git init
g m hotfix // git merge hotfix
g pll // git pull
g psh // git push
g s // git status
และคำสั่งอื่น ๆ จะสั้นเหมือนกัน นอกจากนี้ยังช่วยให้การทุบตีสำเร็จ แพ็คเกจเพิ่มฟังก์ชันทุบตีให้กับ dotfiles ของคุณทำงานบน osx, linux และ windows นอกจากนี้ซึ่งแตกต่างจากนามแฝงอื่น ๆ มันเป็นนามแฝงgit
-> g
เช่นเดียวกับพารามิเตอร์ที่สอง
หากคุณต้องการทางเลือกอื่นสำหรับ~/.gitconfig
ตัวเลือกและเปิดให้ทำการขุดอีกเล็กน้อยตัวเลือกอื่นคือการเขียนคำสั่ง git ที่กำหนดเองทั้งหมดโดยการห่อคำสั่งเหล่านั้นในแพ็คเกจโหนดทั่วโลก
ใน package.json ของคุณคุณจะต้องกำหนดคำสั่งรูท (ตัวอย่าง:) gt
จากนั้นกรองคำสั่งเฉพาะเพื่อดำเนินการคำสั่ง git ที่ถูกต้อง ยกตัวอย่างเช่นอาจจะgit checkout my-branch
gt co mybranch
แพ็คเกจ "christian-git" ใน NPM ใช้วิธีการนี้: https://github.com/alexmacarthur/christian-git
มันมอบให้ที่นี่นามแฝงแม้จะมีคำตอบที่ดีที่นี่ฉันเพิ่มนี้เพราะมันแตกต่างใน windows และ linux
ภาพหน้าจอ PFA ของไฟล์. gitconfig ของฉัน
ด้วยนามแฝงด้านล่าง
[alias]
cb = checkout branch
pullb = pull main branch
.gitconfig
ฉันขอแนะนำให้ใช้การ.gitconfig
รวมสำหรับนามแฝงของคุณ เมื่อคุณเริ่มสร้างนามแฝงคุณอาจท้ายด้วยจำนวนมาก พวกเขาอาจเป็นสิ่งที่คุณต้องการแบ่งปันกับผู้อื่น วางไว้ในไฟล์เฉพาะทำให้ง่ายต่อการแบ่งปัน ทีมของคุณสามารถใช้ repo คอมไพล์เพื่อถือ aliases ที่แชร์ได้ และแน่นอนว่ามีนามแฝงบางตัวที่คุณไม่ต้องการแบ่งปันดังนั้นให้เก็บไว้ในไฟล์นามแฝงส่วนตัว
[include]
path=src/dotfiles/.gitaliases
[include]
path=src/team-utils/gitaliases
[include]
path=.gitaliases.private
ความเป็นไปได้อีกประการหนึ่งสำหรับ windows คือการมีไดเรกทอรีที่เต็มไปด้วยไฟล์. bat ที่มีทางลัดของคุณ ชื่อของไฟล์เป็นทางลัดที่จะใช้ เพียงเพิ่มไดเรกทอรีไปยังตัวแปรสภาพแวดล้อม PATH ของคุณและคุณมีทางลัดทั้งหมดในการกำจัดของคุณในหน้าต่าง cmd
ตัวอย่างเช่น (gc.bat):
git commit -m %1
จากนั้นคุณสามารถดำเนินการคำสั่งต่อไปนี้ในคอนโซล:
gc "changed stuff"
เหตุผลที่ฉันเพิ่มสิ่งนี้เป็นคำตอบก็เพราะว่าเมื่อใช้สิ่งนี้คุณไม่ได้ จำกัด อยู่git ...
แค่คำสั่งเท่านั้น