基础配置
1 2 3
| git config --global user.name "Your Name" git config --global user.email "your@email.com"
|
仓库操作
1 2 3 4 5
| git clone <仓库名>
git init
|
提交修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git status
git add <文件名>
git add .
git commit -m "描述"
git commit --amend
|
分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git branch
git branch -a
git branch <分支名>
git checkout <分支名>
git checkout -b <分支名>
git branch -d <分支名>
git branch -D <分支名>
|
远程协作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git remote -v
git pull origin main
git fetch origin git merge origin/main
git push origin <本地分支名>
git push origin --delete <远程分支名>
|
合并与冲突
1 2 3 4 5 6 7 8
| git merge feature
1. 执行合并/拉取后出现冲突 2. 用 IDE 或编辑器手动修改冲突文件(搜索 >>>>> 标记) 3. 添加解决后的文件:git add conflicted_file.py 4. 完成合并:git commit
|
撤销操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git checkout -- filename.js
git reset HEAD filename.js
git reset --soft commit_id
git reset --hard commit_id
git reflog git checkout -b recovered-branch commit_id
|
规范
分支
- main/master:生产环境代码
- develop:开发分支
- feat/xxx:功能分支
提交
git commit -m “fix: resolve login timeout issue”
开发流程
- git checkout -b my-feature # 本地新建分支
- 开发并提交代码
- git push origin my-feature # 推送到远程
- 在GitHub/GitLab创建Merge Request
- 代码审查后合并