让你的 Linux 终端使用起来更愉快 简介 Git 别名:初学者 Git 别名:高级 致谢

2025-06-08

让你的 Linux 终端使用起来更愉快

简介

Git 别名:初学者

Git 别名:高级

致谢

在某些时候,我们作为 Linux 用户甚至 Mac 都需要使用终端,无论是安装某些东西还是执行软件工程师任务,或者其他什么。但你知道,终端很无聊,而且常常成为人们普遍害怕 Linux 的主要原因。

“哦哦,这很可怕,很难使用,我不知道这些命令和东西”

不再害怕终端或使用难以使用的无聊终端

我无法帮助执行可怕的命令,因为我认为这是我们需要一路学习的东西,但我想我可以帮助让它变得更容易忍受一些。

好的,我们走吧!

警告:当然,与你的库存相比,这东西需要相当多的资源,所以如果你的电脑是真正的土豆,你可能需要稍微退后一点,对不起

放弃 Bash,使用 ZSH

当然不要卸载你疯了吗?只需为您的计算机添加一个新的外壳,我们将安装它zsh以使我们能够添加花哨的玩具。

Mac 用户应该会很高兴,因为我记得你们默认都有这个功能

首先,安装过程与终端中的安装过程类似,我使用基于 Arch 的 Linux 发行版,因此

sudo pacman -S zsh

如果您的 shell 没有更改为 zsh,请输入,zsh然后您应该会看到类似这样的内容,标准 zsh。

标准 zsh

哦...天哪...ZSH!!

之后您将访问Oh-My-Zsh,那里将会发生奇迹。

Oh My Zsh 是一个开源的、社区驱动的框架,用于管理您的 zsh 配置。

你可以使用 curl、wget 或 fetch 来安装它。我通常使用 curl,所以很简单

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

之后,它会做一些事情,比如将你的旧 zshrc 移动到 zshrc.pre-oh-my-zsh 或其他东西,然后你就可以准备好你的 zsh,而且看起来不错。

OMZ 注入的 Zsh 示例

您可以查看文档、更改主题、将提供的插件添加到您的 zshrc 中。

自定义插件

这将是主要的酱汁,自定义插件,就我个人而言,我在我的设置上安装了 4 个。

语法高亮

它可以帮助您语法高亮显示您的 shell 命令,使其更加清晰,甚至可能有助于发现诸如缺少引号开头之类的错误。

git clone --depth 1 "https://github.com/zsh-users/zsh-syntax-highlighting.git" $HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting

自动建议

这与自动完成功能不同,因为它会根据您之前的命令提出建议,如果您想使用之前使用过的命令,它会更快

git clone --depth 1 "https://github.com/zsh-users/zsh-autosuggestions.git" $HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions

自动完成

这个插件可能还没有被充分利用,但它是最强大的插件

git clone --depth 1 "https://github.com/marlonrichert/zsh-autocomplete.git" $HOME/.oh-my-zsh/custom/plugins/zsh-autocomplete

历史子串搜索

这很难解释,我能解释的最好的是,我可以输入,git然后我按向上箭头,然后它会向我显示我的历史 git 命令,当我想制作脚本或文档时,我会广泛使用它。

git clone --depth 1 "https://github.com/zsh-users/zsh-history-substring-search" $HOME/.oh-my-zsh/custom/plugins/zsh-history-substring-search

自定义主题

如果你已经喜欢他们的主题,那么这个就不需要了,但如果你像我一样,我想要更多!!你可以使用 powerlevel10k

这个本身就像一个框架,所以我将保留 git repo https://github.com/romkatv/powerlevel10k?tab=readme-ov-file

轰隆!

图片描述

还有更多??

说实话,我们可以做更多,但有些事情需要我们经常使用终端

例如,“别名”只是一堆我们经常使用但太长的命令,所以我们创建简短易记的别名,这样我们就可以减少输入量或使其易于记忆

这些是那里的一些流行别名,由Marius Colacioiu提供

Intro

Bellow follows a list of aliases, split between beginner and advanced.

To start using them right away you should place the aliases you like most in a file like ~/.zsh/git. Now all that remains is to add the following line to your ~/.zshrc or ~/.bashrc:

. ~/.zsh/git # git aliases

Git aliases: beginner

# Most used git command should be short.
alias s='git status -sb'

alias ga='git add -A'
alias gap='ga -p'

alias gbr='git branch -v'

gc() {
  git diff --cached | grep '\btap[ph]\b' >/dev/null &&
    echo "\e[0;31;29mOops, there's a #tapp or similar in that diff.\e[0m" ||
    git commit -v "$@"
}

alias gch='git cherry-pick'

alias gcm='git commit -v --amend'

alias gco='git checkout'

alias gd='git diff -M'
alias gd.='git diff -M --color-words="."'
alias gdc='git diff --cached -M'
alias gdc.='git diff --cached -M --color-words="."'

alias gf='git fetch'

# Helper function.
git_current_branch() {
  cat "$(git rev-parse --git-dir 2>/dev/null)/HEAD" | sed -e 's/^.*refs\/heads\///'
}

alias glog='git log --date-order --pretty="format:%C(yellow)%h%Cblue%d%Creset %s %C(white) %an, %ar%Creset"'
alias gl='glog --graph'
alias gla='gl --all'

alias gm='git merge --no-ff'
alias gmf='git merge --ff-only'

alias gp='git push'
alias gpthis='gp origin $(git_current_branch)'
alias gpthis!='gp --set-upstream origin $(git_current_branch)'

alias grb='git rebase -p'
alias grba='git rebase --abort'
alias grbc='git rebase --continue'
alias grbi='git rebase -i'

alias gr='git reset'
alias grh='git reset --hard'
alias grsh='git reset --soft HEAD~'

alias grv='git remote -v'

alias gs='git show'
alias gs.='git show --color-words="."'

alias gst='git stash'
alias gstp='git stash pop'

alias gup='git pull'

Git aliases: advanced

# Use gh instead of git (fast GitHub command line client).
#
# More info: https://github.com/jingweno/gh
#
if type gh >/dev/null; then
  alias git=gh
  if type compdef >/dev/null 2>/dev/null; then
     compdef gh=git
  fi
fi

# Redefine alias. Allow to pass more args to `s`.
s() {
  git status -sb "$@"
  return 0
}

git-new() {
  [ -d "$1" ] || mkdir "$1" &&
  cd "$1" &&
  git init &&
  touch .gitignore &&
  git add .gitignore &&
  git commit -m "Add .gitignore."
}

# Query `glog` with regex query.
gls() {
  query="$1"
  shift
  glog --pickaxe-regex "-S$query" "$@"
}

# Checkout git-smart gem for more infos.
#
# More infos: https://github.com/geelen/git-smart
#
alias gup='git smart-pull'

# Create your own git alias, reusing other git shortcuts.
alias gpstaging='export PREVIOUS_BRANCH=$(git_current_branch) ; gco -B staging ; gpthis -f ; gco $PREVIOUS_BRANCH ; gbr -D staging'

# Create pull-request from command line (uses gh gem).
alias gpr='gpthis; git pull-request'

Credits

The way I use git changed since I found and started using benhoskings's dot-files. Using all those aliases for git commands started to grow on me, day after day.

Recommended video: Ben Hoskings on Advanced Git.

view raw git-aliases.md hosted with ❤ by GitHub

Intro

Bellow follows a list of aliases, split between beginner and advanced.

To start using them right away you should place the aliases you like most in a file like ~/.zsh/git. Now all that remains is to add the following line to your ~/.zshrc or ~/.bashrc:

. ~/.zsh/git # git aliases

Git aliases: beginner

# Most used git command should be short.
alias s='git status -sb'

alias ga='git add -A'
alias gap='ga -p'

alias gbr='git branch -v'

gc() {
  git diff --cached | grep '\btap[ph]\b' >/dev/null &&
    echo "\e[0;31;29mOops, there's a #tapp or similar in that diff.\e[0m" ||
    git commit -v "$@"
}

alias gch='git cherry-pick'

alias gcm='git commit -v --amend'

alias gco='git checkout'

alias gd='git diff -M'
alias gd.='git diff -M --color-words="."'
alias gdc='git diff --cached -M'
alias gdc.='git diff --cached -M --color-words="."'

alias gf='git fetch'

# Helper function.
git_current_branch() {
  cat "$(git rev-parse --git-dir 2>/dev/null)/HEAD" | sed -e 's/^.*refs\/heads\///'
}

alias glog='git log --date-order --pretty="format:%C(yellow)%h%Cblue%d%Creset %s %C(white) %an, %ar%Creset"'
alias gl='glog --graph'
alias gla='gl --all'

alias gm='git merge --no-ff'
alias gmf='git merge --ff-only'

alias gp='git push'
alias gpthis='gp origin $(git_current_branch)'
alias gpthis!='gp --set-upstream origin $(git_current_branch)'

alias grb='git rebase -p'
alias grba='git rebase --abort'
alias grbc='git rebase --continue'
alias grbi='git rebase -i'

alias gr='git reset'
alias grh='git reset --hard'
alias grsh='git reset --soft HEAD~'

alias grv='git remote -v'

alias gs='git show'
alias gs.='git show --color-words="."'

alias gst='git stash'
alias gstp='git stash pop'

alias gup='git pull'

Git aliases: advanced

# Use gh instead of git (fast GitHub command line client).
#
# More info: https://github.com/jingweno/gh
#
if type gh >/dev/null; then
  alias git=gh
  if type compdef >/dev/null 2>/dev/null; then
     compdef gh=git
  fi
fi

# Redefine alias. Allow to pass more args to `s`.
s() {
  git status -sb "$@"
  return 0
}

git-new() {
  [ -d "$1" ] || mkdir "$1" &&
  cd "$1" &&
  git init &&
  touch .gitignore &&
  git add .gitignore &&
  git commit -m "Add .gitignore."
}

# Query `glog` with regex query.
gls() {
  query="$1"
  shift
  glog --pickaxe-regex "-S$query" "$@"
}

# Checkout git-smart gem for more infos.
#
# More infos: https://github.com/geelen/git-smart
#
alias gup='git smart-pull'

# Create your own git alias, reusing other git shortcuts.
alias gpstaging='export PREVIOUS_BRANCH=$(git_current_branch) ; gco -B staging ; gpthis -f ; gco $PREVIOUS_BRANCH ; gbr -D staging'

# Create pull-request from command line (uses gh gem).
alias gpr='gpthis; git pull-request'

Credits

The way I use git changed since I found and started using benhoskings's dot-files. Using all those aliases for git commands started to grow on me, day after day.

Recommended video: Ben Hoskings on Advanced Git.

view raw git-aliases.md hosted with ❤ by GitHub

这是我的,保存为 .aliases.sh,然后在我的 zshrc 上获取它

图片描述

还有什么??

我们还能添加什么呢?我想这取决于我们的想象力吧?请在评论区告诉我我还能添加什么。

干杯

编辑:向要点提供者添加信用

鏂囩珷鏉ユ簮锛�https://dev.to/dhupee/make-your-linux-terminal-enjoyable-to-use-3j47
PREV
SOLID 原则变得简单
NEXT
使用 Twitter APIv2 🚀🚀 在 Python 中创建 Twitter 机器人