主题
Git
基础操作
配置管理
bash
# 查看全局配置
git config --global --list
# 查看本地配置
git config --local --list
# 设置用户名
git config --global user.name "Your Name"
# 设置邮箱
git config --global user.email "your.email@example.com"
# 设置默认编辑器
git config --global core.editor "vim"
# 设置默认分支
git config --global init.defaultBranch main仓库管理
bash
# 初始化仓库
git init
# 克隆仓库
git clone <repository_url>
# 关联远程仓库
git remote add origin <repository_url>
# 查看远程仓库
git remote -v
# 修改远程仓库地址
git remote set-url origin <new_url>
# 删除远程仓库
git remote remove <remote_name>
# 拉取远程更新
git pull origin <branch_name>
# 获取远程更新(不合并)
git fetch origin
# 获取所有远程仓库的更新
git fetch --all
# 获取所有远程仓库的更新并清理已删除的远程分支
git fetch --all --prune分支操作
bash
# 查看分支
git branch -a
# 查看本地分支
git branch
# 查看远程分支
git branch -r
# 切换分支
git checkout <branch_name>
# 创建并切换分支
git checkout -b <new_branch>
# 拉取远程分支
git fetch origin <branch_name>
# 删除本地分支
git branch -d <branch_name>
# 强制删除分支
git branch -D <branch_name>
# 删除远程分支
git push origin --delete <branch_name>
# 重命名分支
git branch -m <old_name> <new_name>代码提交
暂存修改:是优先于本地提交的文件,先commit
bash
# 查看状态
git status
# 查看修改内容
git diff
# 暂存修改
git add .
# 暂存指定文件
git add <file_name>
# 暂存部分修改
git add -p
# 取消暂存
git reset <file_name>
# 提交修改
git commit -m "<type>: <description>"
# 修改最后一次提交
git commit --amend
# 取消最后一次提交(保留修改)
git reset --soft HEAD~1
# 取消最后一次提交(不保留修改)
git reset --hard HEAD~1
# 取消指定次数的提交(保留修改)
git reset --soft HEAD~<n>
# 取消指定次数的提交(不保留修改)
git reset --hard HEAD~<n>
# 推送到远程
git push origin <branch_name>
# 强制推送
git push -f origin <branch_name>
# 撤销本地修改
git checkout -- <file_name>
# 撤销所有本地修改
git checkout -- .标签管理
bash
# 查看标签
git tag
# 创建标签
git tag -a <tag_name> -m "<tag_message>"
# 在指定提交上创建标签
git tag -a <tag_name> <commit_id> -m "<tag_message>"
# 推送标签
git push origin <tag_name>
# 推送所有标签
git push origin --tags
# 删除本地标签
git tag -d <tag_name>
# 删除远程标签
git push origin :refs/tags/<tag_name>日志查看
bash
# 查看提交历史
git log
# 查看简洁提交历史
git log --oneline
# 查看指定文件的修改历史
git log -p <file_name>
# 查看指定作者的提交
git log --author="<author_name>"
# 查看指定时间段的提交
git log --since="2 weeks ago"
# 查看分支图
git log --graph --oneline --all高级操作
提交历史管理
bash
# 查看提交历史
git log --oneline
# 合并多个提交
git rebase -i HEAD~<n>
# 示例:合并最近3次提交
git rebase -i HEAD~3
# 在编辑器中保留第一个pick,其他改为squash多个提交合并
shell
# 例如有 5 个提交想合并成一个提交
a1
b2
c3
d4
e5
# 切换分支
git switch feature/xxx
# 进入交互式命令
git rebase -i main
# 正常界面如下
pick a1 First commit
pick b2 Add logic
pick c3 Fix bug
pick d4 Refactor
pick e5 Update code
# 保留第一个commit
pick a1 First commit
squash b2 Add logic
squash c3 Fix bug
squash d4 Refactor
squash e5 Update code
# 退出:wq,重新填写合并后的提交信息
feat: xxx
# 如果已经 push 远端则
git push -f代码合并
- 合并与变基的区别
变基确保你的提交会在最新的主分支上提交
shell
# merge
A---B---C---D (main)
\ \
E---F---G (feature)
# rebase
A---B---C---D (main)
\
E'---F'---G' (feature)清理仓库
bash
# 清除历史记录(创建新分支)
git checkout --orphan latest_branch
git add -A
git commit -am "Initial commit"
git branch -D master
git branch -m master
git push -f origin master常见问题
大文件上传
bash
# 设置postBuffer大小
git config --global http.postBuffer 157286400分支同步问题
bash
# 解决本地和远程分支不一致
rm -rf .git/refs/remotes/origin/*
git fetch origin错误处理
- RPC failed; HTTP 500: 检查网络连接,增加postBuffer
- bad object refs/remotes/origin/master: 清理远程分支引用后重新拉取
取消跟踪文件
保留本地文件,不再跟踪
shell
# 强制停止跟踪(不会删除本地文件)
git rm -f --cached 'file path'txt
src/deploy/deploy.config.json