使用别名加速你的 Git 工作流程

2025-05-26

使用别名加速你的 Git 工作流程

使用 Git 时使用别名可以提高你使用这个流行版本控制系统的效率。下面列出了我在使用 Git 的项目中最喜欢和最常用的一些别名。

需要注意的是,当我谈论别名时,我指的是bash/zsh 别名而不是git 别名

有什么区别?Bash/Zsh 别名设置在.bashrc.zshrc文件中,允许您将命令设置为简写版本。例如,您可以将别名设置git loggl,这样可以节省一些输入字符的时间。Git 别名允许您以类似的方式将 git 命令设置为简写版本,但您仍然需要git在简写之前键入 。例如,您可以将命令设置git logl,然后通过键入 来调用该命令git l。所有 git 别名都设置在.gitconfig通常位于计算机HOME目录中的文件中。


添加和修改提交

gaa

git add -A
Enter fullscreen mode Exit fullscreen mode

将所有更改添加到暂存。

gcm

git commit -m
Enter fullscreen mode Exit fullscreen mode

创建包含所有暂存文件的新提交,并使用给定的消息作为提交的消息。

例子:

gcm "This is my commit message describing the changes"
Enter fullscreen mode Exit fullscreen mode

gcma

git commit -a -m
Enter fullscreen mode Exit fullscreen mode

将所有文件添加到暂存区并使用给定的消息作为提交消息进行提交。

例子:

gcam "This is another commit message!"
Enter fullscreen mode Exit fullscreen mode

gca

git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

将当前保存的文件移动到上一个提交信息中。--no-edit传递该标志是git commit --amend为了保留上一个提交信息(通常情况下是这样)。

如果需要更改上一个提交消息,我已将其别名git commit --amend --no-editgcae。它的作用与 相同gca,但会打开 Git 编辑器来编辑上一个提交的消息。就我而言,VS Code由于我不熟悉 VIM,因此我将其设置为默认的 Git 编辑器。

VS Code 中显示的先前提交消息以及有关如何更改提交消息的说明。

gcaa

git add --all && git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

如果某些更改需要包含在上一次提交中,则此命令非常有用。它会将所有新修改的文​​件添加到暂存区,然后将amend这些更改添加到上一次提交中。

gnope

git checkout .
Enter fullscreen mode Exit fullscreen mode

删除 Git 检测到的所有更改。

gwait

git reset HEAD
Enter fullscreen mode Exit fullscreen mode

取消暂存所有内容。

gundo

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

撤消最后一次提交并将提交中的文件移至暂存区。

阅读历史

gl

git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

以简洁的方式记录之前的提交。传入的参数--pretty定义了每次提交应显示的信息(例如提交哈希值、分支名称、提交消息、提交日期和作者)。绝对不想每次都输入这些信息!

gco

git checkout
Enter fullscreen mode Exit fullscreen mode

允许在分支之间切换。

例子:

gco other-branch-name
Enter fullscreen mode Exit fullscreen mode

远程推送和拉取

gps

git push
Enter fullscreen mode Exit fullscreen mode

更新遥控器。

gpsf

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

如果没有其他人提交到远程分支,则用本地分支覆盖远程分支。这被认为是一种比使用 更安全的方法git push --force

gpl

git pull --rebase
Enter fullscreen mode Exit fullscreen mode

从远程获取更新,并将本地分支与上游分支变基。这样可以避免使用 时可能发生的任何合并提交git pull

变基

grb

git rebase
Enter fullscreen mode Exit fullscreen mode

将当前分支重新定位到另一个分支。

例子:

grb origin/master
Enter fullscreen mode Exit fullscreen mode

grn (shell 命令)

grn() { git rebase -i HEAD~"$1"; }
Enter fullscreen mode Exit fullscreen mode

这实际上是一个shell 命令,允许将变量作为参数传递。$1是传递给grn函数的参数的占位符。该函数接受N一个参数, 其中N是要执行交互式 rebase 的提交次数。

例子:

grn shell 命令的示例用法

grbic (shell 命令)

grbic() { git rebase -i "$1" ; }
Enter fullscreen mode Exit fullscreen mode

接受提交哈希作为参数并执行交互式变基回到传递的提交哈希。

例子:

grbic shell 命令的使用示例

grba

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

git rebase中止交互式变基并将 git 状态恢复到运行命令时的状态。

grbc

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

解决提交冲突后继续进行交互式变基。


好的,我知道上面说了这么多别名,很抱歉一直在说这些。我鼓励大家多使用别名,这样可以加快开发速度。如果这些别名对你有帮助,或者你有什么喜欢的别名想分享,请告诉我。


如果您发现这篇文章,请考虑在TwitterGithubLinkedIn上关注我。

文章来源:https://dev.to/robertcoopercode/using-aliases-to-speed-up-your-git-workflow-2f5a
PREV
一次 Rust 并与 Android、iOS 和 Flutter 共享
NEXT
2019 年开始使用 TypeScript 2019 年开始使用 TypeScript