Skip to Content
文档开发Git、SSH、终端基础

Git、SSH、终端基础

本文介绍如何在 Ubuntu 26.04 上搭建 Git 版本控制环境、配置 SSH 密钥以及终端的基础使用。

安装 Git

Ubuntu 26.04 默认仓库中包含 Git,直接通过 apt 安装即可:

sudo apt update sudo apt install git -y

验证安装:

git --version

配置 Git 用户信息

首次使用 Git 前,需要设置用户名和邮箱,这些信息会记录在每次提交中:

git config --global user.name "你的用户名" git config --global user.email "your@email.com"

推荐的全局配置:

# 设置默认分支名称为 main git config --global init.defaultBranch main # 设置默认编辑器 git config --global core.editor "vim" # 启用颜色输出 git config --global color.ui auto # 查看所有配置 git config --global --list

生成 SSH 密钥

SSH 密钥用于安全地连接 GitHub、GitLab 等远程仓库,无需每次输入密码。

生成 Ed25519 密钥(推荐)

ssh-keygen -t ed25519 -C "your@email.com"

按提示操作,默认保存路径为 ~/.ssh/id_ed25519。建议设置密码短语以增强安全性。

启动 SSH Agent 并添加密钥

eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519

复制公钥

cat ~/.ssh/id_ed25519.pub

将输出的公钥内容复制到 GitHub(Settings > SSH and GPG keys)或其他平台。

测试连接

ssh -T git@github.com

如果配置正确,会看到欢迎消息。

多 SSH 密钥管理

如果你有多个 Git 账号(如工作和个人),可以通过 SSH 配置文件管理:

vim ~/.ssh/config

添加以下内容:

# 个人 GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_personal # 工作 GitLab Host gitlab.company.com HostName gitlab.company.com User git IdentityFile ~/.ssh/id_ed25519_work

终端基础:Bash

Ubuntu 26.04 默认 Shell 为 Bash。以下是常用操作:

常用快捷键

快捷键功能
Ctrl + A移动到行首
Ctrl + E移动到行尾
Ctrl + R反向搜索历史命令
Ctrl + L清屏
Ctrl + C终止当前命令
Tab自动补全

自定义 Bash 配置

编辑 ~/.bashrc 添加别名和自定义设置:

# 常用别名 alias ll='ls -alF' alias la='ls -A' alias gs='git status' alias gp='git push' # 使修改生效 source ~/.bashrc

切换到 Zsh

Zsh 提供更强大的自动补全和插件系统:

安装 Zsh

sudo apt install zsh -y

设为默认 Shell

chsh -s $(which zsh)

注销并重新登录后生效。

安装 Oh My Zsh

Oh My Zsh 是一个流行的 Zsh 配置框架:

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

推荐插件

编辑 ~/.zshrc,修改插件列表:

plugins=(git z sudo extract zsh-autosuggestions zsh-syntax-highlighting)

安装第三方插件:

# 自动建议 git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions # 语法高亮 git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

其他终端模拟器推荐

Ubuntu 26.04 默认终端已经够用,但你也可以尝试:

# 安装 Alacritty(GPU 加速终端) sudo snap install alacritty --classic # 安装 Kitty sudo apt install kitty -y # 安装 Wezterm flatpak install flathub org.wezfurlong.wezterm

Git 常用工作流

# 克隆仓库 git clone git@github.com:username/repo.git # 创建并切换分支 git checkout -b feature/new-feature # 添加、提交、推送 git add . git commit -m "feat: 添加新功能" git push origin feature/new-feature # 合并分支 git checkout main git merge feature/new-feature

掌握 Git、SSH 和终端基础后,你就为后续的开发工作做好了准备。

Last updated on