Skip to content

常用 Git 操作命令

常用推送命令

.gitignore
bash
# 切换分支
$ git checkout [branch-name]

# 添加文件到暂存区
$ git add [filename]

# 添加当前更改的所有文件到暂存区
$ git add .

# 查看工作区与暂存区的当前情况
$ git status

# 将缓存区的文件提交到仓库
$ git commit -m "feat: 提交说明"

# git add . && git commit -m
$ git commit -am "feat: 提交说明"

# 绕过验证
$ git commit --no-verify -m "feat: 提交说明"

# 将服务器上的最新代码拉取到本地
$ git pull

# 推送本地仓库到远程仓库
$ git push

# 撤销本次的 commit
$ git reset --soft HEAD^

# 修改本次提交的commit(vim)
$ git commit --amend

新建

.gitignore
bash
# 初始化当前项目
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 在指定的目录<directory>创建一个空的git仓库
$ git init --bare <directory>

# 拷贝一个 Git 仓库到本地
$ git clone [url]

# 拷贝一个 Git 仓库到本地,并指定项目名
$ git clone [url] [project-name]

# 浅克隆
$ git clone --depth=1 [url]

配置

.gitignore
bash
# 配置全局用户名(去除 --global 当前项目生效)
$ git config --global user.name "wudi"

# 配置全局用户邮箱(去除 --global 当前项目生效)
$ git config --global user.email "wuchendi96@gmail.com"

# 列出当前配置
$ git config --list

# 列出Repository配置
$ git config --local --list

# 列出全局配置
$ git config --global --list

# 列出系统配置
$ git config --system --list

# 更换git 地址
# 查看现有地址
$ git remote -v

# 替换新地址
$ git remote set-url origin [new-url]

分支操作

.gitignore
bash
# 查看所有本地分支
$ git branch

# 查看所有远程分支
$ git branch -r

# 查看所有本地分支和远程分支
$ git branch -a

.gitignore
bash
# 新建一个分支,但依然停留在当前分支
$ git branch [branch-name]

.gitignore
bash
# 从当前分支,切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 创建并切换到新建分支
$ git checkout -b [branch-name]

# 切换到上一个分支
$ git checkout -

合并

.gitignore
bash
# 合并指定分支到当前分支
$ git merge [branch-name]

# 取消当前合并
$ git merge --abort

# 选择一个 commit-id,合并进当前分支
# 总结下 git cherry-pick 是本地特性,本地要有这个 commit-id 才可以被 git cherry-pick
$ git cherry-pick [commit-id]

拉取与推送

.gitignore
bash
# 拉取远程仓库最新代码
$ git pull

# 推送本地仓库到远程仓库
$ git push

# 推送到远程分支
$ git push origin [branch-name]

# 强制推送本地覆盖远程(谨慎)
$ git push -f -u origin [branch-name]

.gitignore
bash
# 删除分支
$ git branch -D [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]

# 删除本地已不存在于远程的分支
$ git remote prune origin

# 查看哪些分支会被删除
$ git remote prune origin --dry-run

回滚

.gitignore
bash
# commit
$ git revert [commit-id]

# 针对merge的 commit
$ git revert [commit-id] -m

# 退回到指定的 commit
$ git reset [commit]

# HEAD 和当前 branch 切到上一条commit 的同时,工作目录新改动和已经add到stage区的新改动也一起全都消失了
$ git reset --hard HEAD^

# 切到上一条commit同时,保留工作目录和暂存区中的内容,并把重置 HEAD 所带来的新的差异放进暂存区
$ git reset --soft HEAD^

重命名

.gitignore
bash
$ git branch -m [old-branch-name] [new-branch-name]

更新 Git 用户信息

.gitignore
bash
# 清除缓存
$ git config --system --unset credential.helper

# 重新配置 git 用户名和密码
$ git config --global credential.helper store

其他常用命令

.gitignore
bash
# 显示哪些分支已合并到当前分支
$ git branch --merged

# 显示哪些分支未合并到当前分支
$ git branch --no-merged

# 查看所有本地各个分支最后一个提交对象的信息
$ git branch -v

# 查看所有远程各个分支最后一个提交对象的信息
$ git branch -r -v

# 查看所有本地分支和远程分支各个分支最后一个提交对象的信息
$ git branch -a -v

# 重命名分支
$ git branch -m [oldbranch-name] [newbranch-name]

# 拉取远程分支并创建本地分支
$ git checkout -b [branch-name] origin/远程分支名x

标签

.gitignore
bash
# 查看所有 tag
$ git tag

# 新建一个tag在当前 commit-id
$ git tag [tag]

# 新建一个tag在指定 commit-id
$ git tag [tag] [commit-id]

# 删除本地 tag
$ git tag -d [tag]

# 删除远程 tag
$ git push origin :refs/tags/[tagName]

# 查看tag信息
$ git show [tag]

# 提交指定tag
$ git push [remote] [tag]

# 提交所有tag
$ git push [remote] --tags

# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

信息

.gitignore
bash
# 显示有变更的文件
$ git status

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
$ git blame [file]

# 显示某次提交的元数据和内容变化
$ git show [commit-id]

# 显示某次提交发生变化的文件
$ git show --name-only [commit-id]

# 显示某次提交时,某个文件的内容
$ git show [commit-id]:[filename]

# 显示当前分支的最近几次提交
$ git reflog

远程仓库管理

.gitignore
bash
# 查看远程仓库地址
$ git remote -v

# 修改远程仓库地址
$ git remote set-url origin [new-url]

diff

.gitignore
bash
# 显示暂存区和工作区的差异
$ git diff

# 显示暂存区和上一个commit的差异
$ git diff --cached [file]

# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD

# 显示两次提交之间的差异
$ git diff [first-branch]...[second-branch]

# 显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"

log

.gitignore
bash
# 显示当前分支的版本历史
$ git log

# 单行展示版本记录
$ git log --oneline

# 显示commit历史,以及每次commit发生变更的文件
$ git log --stat

# 搜索提交历史,根据关键词
$ git log -S [keyword]

# 显示某个commit之后的所有变动,每个commit占据一行
$ git log [tag] HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
$ git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示指定文件相关的每一次diff
$ git log -p [file]

# 显示过去5次提交
$ git log -5 --pretty --oneline

rebase(谨慎)

git rebase详解

.gitignore
bash
# 将 test 应用到 master 上面
# git rebase <basebranch> <topicbranch>
$ git rebase master test

Git 提交规范

提交信息格式

.gitignore
bash
<type>(<scope>): <description>

# 常用提交类型
feat:     新功能
fix:      修复 bug
docs:     只修改文档
style:    代码样式修改(不影响功能)
build:    依赖、构建工具或外部依赖修改
refactor: 代码重构(不增加新功能)
perf:     性能优化
test:     添加测试
chore:    构建过程或辅助工具的改动

示例

.gitignore
bash
# 增加新功能
$ git commit -m "feat(auth): add login feature"

# 修复bug
$ git commit -m "fix(user): resolve login issue"

# 代码重构
$ git commit -m "refactor(auth): refactor authentication logic"

分支管理规范

分支名称环境备注
master主分支生产环境稳定、可发布的版本,所有发布内容都会合并到此分支。禁止直接在此分支开发。
release预发布分支验收测试环境(同步生产数据)用于修复 feature 分支合并到 develop 后的 bug,确保稳定后合并到 developmaster,然后发布到生产环境。
hotfix紧急修复分支开发者调试与开发环境用于修复生产环境中的紧急问题,直接从 master 创建。修复完成后合并到 developmaster
develop开发主分支功能验收测试环境集中所有新功能的开发,保证最新的功能代码。该分支只能合并其他分支,不得直接开发。
feature功能开发分支开发环境develop 创建,用于开发新功能,功能完成后合并回 develop

ssh

.gitignore
bash
# 生成 SSH 密钥
$ ssh-keygen -t rsa -C "wuchendi96@gmail.com"

# 查看公钥
$ cat ~/.ssh/id_rsa.pub

# 复制粘贴到 github
# github配置ssh key: https://github.com/settings/keys

# 测试 SSH 连接
$ ssh -T git@github.com

image.png

修改commit记录

.gitignore
bash
# 比如说,我想修改距此版本之前的第2条 commit message,运行
git rebase -i HEAD~2

# 显示
pick 63e6242 feat: test
pick 1a6b2d3 fix(pages movie): slides bug fixed

# 将要修改的那条 commit message,如 pick 63e6242 feat: test,pick 改为 edit,修改完成后保存退出

# 然后运行, 修改 commit message
git commit --amend

# 最后完成修改
git rebase --continue

# 强推(或者先 pull 在 push 也可以)
git push -f -u origin main

image.png

更新git账号信息

如git信息变更或者切换账号 remote: [session-3946bdb4] Access denied

git config --system --unset credential.helper

打开命令提示符,输入:git config --system --unset credential.helper

在操作git时会提示重新输入账户名及密码。

但是有个问题:在git中输入此命令后,每次拉取代码需要重新输入用户名和密码,可以使用
git config --global credential.helper store

这样再获取代码提示输入用户名和密码,输入之后后续就不需要再次输入了。

image.png

文件名称大小

bash
# 默认为 true
git config core.ignorecase false

生成 SSH 密钥

bash
ssh-keygen -t ed25519

cat ~/.ssh/id_ed25519.pub

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.