当前位置: 代码网 > it编程>编程语言>其他编程 > Git之常用命令与避坑指南分享

Git之常用命令与避坑指南分享

2026年07月30日 其他编程 我要评论
一、git 基础配置# 设置全局用户信息(必填,否则无法提交)git config --global user.name "你的用户名"git config --global user.email "

一、git 基础配置

# 设置全局用户信息(必填,否则无法提交)
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"

# 设置默认分支名(可选)
git config --global init.defaultbranch main

# 查看配置
git config --list

二、git 常用命令速查表

场景命令
初始化仓库git init
克隆远程仓库git clone <url>
查看状态git status
添加文件到暂存区git add <file> / git add .
提交代码git commit -m "提交信息"
推送到远程git push origin <branch>
拉取远程更新git pull origin <branch>
查看提交日志git log --oneline
创建分支git branch <branch>
切换分支git checkout <branch> / git switch <branch>
合并分支git merge <branch>
暂存工作区git stash
恢复暂存git stash pop

三、github / gitee 上传避坑指南

坑 1:远程仓库已有内容,本地推不上去

问题git push 被拒绝,提示 failed to push some refs

原因:远程仓库初始化时创建了 readme/license 等文件,本地没有这些文件

解决

# 方法一:先拉取合并(推荐)
git pull origin main --allow-unrelated-histories
git push origin main

# 方法二:强制推送(⚠️ 会覆盖远程)
git push -u origin main -f

坑 2:push 时提示输入用户名密码

问题:每次 push 都要输入账号密码

解决:配置凭据缓存

# 缓存 15 分钟
git config --global credential.helper cache

# 永久存储(windows)
git config --global credential.helper store

坑 3:ssh 推送报错permission denied

问题:使用 ssh 方式推送报权限错误

解决

# 1. 生成 ssh 密钥
ssh-keygen -t ed25519 -c "你的邮箱@example.com"

# 2. 查看公钥
cat ~/.ssh/id_ed25519.pub

# 3. 将公钥添加到 github/gitee 的 ssh keys 设置中

# 4. 测试连接
# github
ssh -t git@github.com
# gitee
ssh -t git@gitee.com

坑 4:文件过大无法推送

问题

remote: error: file xxx is 100.00 mb; this exceeds the file size limit

解决

# 安装 git lfs
git lfs install

# 追踪大文件
git lfs track "*.psd"
git lfs track "*.zip"

# 然后正常 add/commit/push

坑 5:误提交了敏感信息

问题:密码、密钥等被提交到仓库

紧急处理

# 1. 立即修改泄露的密码/密钥!

# 2. 从 git 历史中移除文件
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch 敏感文件路径" \
  --prune-empty --tag-name-filter cat -- --all

# 3. 推送清理后的历史
git push origin main --force --all

# 4. 添加 .gitignore 防止再次提交
echo "敏感文件名" >> .gitignore

坑 6:push 时提示detached head

问题:不在任何分支上,无法 push

解决

# 切换回主分支
git checkout main
# 或创建新分支保存当前修改
git checkout -b new-branch

坑 7:github 访问慢 / 连不上

解决

# 使用 github 镜像加速(临时)
git config --global url."https://ghproxy.com/https://github.com/".insteadof "https://github.com/"

# 或修改 hosts 文件绑定 ip

# gitee 一般无需加速

四、推荐工作流

# 1. 克隆远程仓库
git clone git@github.com:用户名/仓库名.git

# 2. 创建功能分支
git checkout -b feature/my-new-feature

# 3. 开发并提交
git add .
git commit -m "feat: 添加新功能描述"

# 4. 推送分支到远程
git push -u origin feature/my-new-feature

# 5. 在 github/gitee 上创建 pull request / merge request

五、git 提交规范(推荐)

feat:     新功能
fix:      修复 bug
docs:     文档更新
style:    代码格式(不影响逻辑)
refactor: 重构
test:     测试相关
chore:    构建/工具变动

示例:

git commit -m "feat: 添加用户登录功能"
git commit -m "fix: 修复首页加载超时问题"

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com