实际开发中git常用指令以及注意事项

1. 日常开发常用指令

查看状态和变化

# 查看当前状态
git status

# 查看文件变化
git diff
git diff <filename>

# 查看暂存区变化
git diff --cached

# 查看简洁状态
git status --porcelain

文件操作

# 添加所有文件
git add .

# 添加特定文件
git add <filename>

# 添加多个文件
git add file1.js file2.css

# 添加所有 .js 文件
git add *.js

# 交互式添加
git add -p <filename>

# 撤销暂存区的文件
git reset HEAD <filename>
git restore --staged <filename>

提交操作

# 基础提交
git commit -m "feat: 添加新功能"

# 添加并提交
git commit -am "feat: 添加新功能"

# 修改最后一次提交
git commit --amend -m "新的提交信息"

# 空提交(用于触发CI/CD)
git commit --allow-empty -m "chore: 触发部署"

分支操作

# 查看分支
git branch
git branch -a  # 查看所有分支

# 创建分支
git branch <branch-name>
git checkout -b <branch-name>
git switch -c <branch-name>

# 切换分支
git checkout <branch-name>
git switch <branch-name>

# 删除分支
git branch -d <branch-name>
git branch -D <branch-name>  # 强制删除

查看历史

# 查看提交历史
git log
git log --oneline
git log --graph --oneline --all

# 查看特定文件历史
git log --follow <filename>

# 查看某个提交
git show <commit-hash>

2. 多远程库管理

添加多个远程库

# 添加 GitHub 仓库
git remote add github https://github.com/username/repo.git

# 添加 Gitee 仓库
git remote add gitee https://gitee.com/username/repo.git

# 添加 GitLab 仓库
git remote add gitlab https://gitlab.com/username/repo.git

# 查看所有远程库
git remote -v

推送到多个远程库

# 推送到 GitHub
git push github main

# 推送到 Gitee
git push gitee main

# 推送到 GitLab
git push gitlab main

# 推送到所有远程库
git push github main && git push gitee main && git push gitlab main

从多个远程库拉取

# 从 GitHub 拉取
git pull github main

# 从 Gitee 拉取
git pull gitee main

# 从 GitLab 拉取
git pull gitlab main

# 拉取所有远程库更新
git fetch --all

3. 实际开发工作流

功能开发流程

# 1. 确保在主分支
git checkout main
git pull origin main

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

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

# 4. 推送到远程
git push origin feature/new-feature

# 5. 创建 Pull Request

# 6. 合并后删除分支
git checkout main
git pull origin main
git branch -d feature/new-feature
git push origin --delete feature/new-feature

多远程库同步流程

# 1. 开发功能
git checkout -b feature/new-feature
git add .
git commit -m "feat: 添加新功能"

# 2. 推送到所有远程库
git push github feature/new-feature
git push gitee feature/new-feature
git push gitlab feature/new-feature

# 3. 合并到主分支
git checkout main
git merge feature/new-feature

# 4. 同步到所有远程库
git push github main
git push gitee main
git push gitlab main

4. 注意事项和最佳实践

1. 远程库命名规范

# 推荐命名
origin      # 主要仓库
upstream    # 上游仓库
backup      # 备份仓库
staging     # 测试环境
production  # 生产环境

# 避免使用相同名称
git remote add origin https://github.com/user1/repo.git
git remote add origin https://gitee.com/user2/repo.git  # 错误!会覆盖

2. 分支管理

# 主分支保持稳定
main        # 生产环境
develop     # 开发环境

# 功能分支及时清理
feature/*   # 功能开发
hotfix/*    # 紧急修复
release/*   # 发布准备

3. 提交信息规范

# 使用规范的提交类型
feat:     新功能
fix:      修复bug
docs:     文档更新
style:    代码格式调整
refactor: 重构
perf:     性能优化
test:     测试相关
build:    构建相关
ci:       持续集成
chore:    其他

4. 冲突处理

# 拉取前先提交本地更改
git add .
git commit -m "feat: 本地更改"

# 拉取远程更新
git pull origin main

# 解决冲突
# 手动编辑冲突文件

# 提交解决结果
git add .
git commit -m "fix: 解决合并冲突"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值