概念 | 说明 |
---|---|
仓库(Repository) | 项目所有版本数据的存储位置 |
工作目录(Working Directory) | 项目文件的当前状态 |
暂存区(Staging Area) | 准备提交的文件列表 |
提交(Commit) | 永久的项目快照 |
分支(Branch) | 独立开发线 |
标签(Tag) | 特定提交的标记 |
工作目录 → git add → 暂存区 → git commit → 本地仓库 → git push → 远程仓库
brew install git
或从官网下载# Ubuntu/Debian
sudo apt install git
# CentOS/RHEL
sudo yum install git
# 设置用户名
git config --global user.name "Your Name"
# 设置邮箱
git config --global user.email "[email protected]"
# 查看配置
git config --list
# 设置默认编辑器(可选)
git config --global core.editor "code --wait" # VS Code
# 初始化新仓库
git init
# 克隆现有仓库
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git myfolder # 指定目录名
# 检查文件状态
git status
# 添加文件到暂存区
git add filename.txt # 添加特定文件
git add . # 添加所有更改
git add *.js # 添加所有js文件
# 提交更改
git commit -m "描述信息"
# 查看提交历史
git log
git log --oneline # 简洁版
git log --graph --all # 图形化显示分支
# 删除文件
git rm filename.txt
# 重命名/移动文件
git mv oldname.txt newname.txt
# 忽略文件(.gitignore)
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
# 查看分支
git branch # 本地分支
git branch -a # 所有分支(包括远程)
# 创建分支
git branch new-feature
git checkout -b new-feature # 创建并切换
# 切换分支
git checkout main
# 合并分支
git checkout main
git merge new-feature
# 删除分支
git branch -d new-feature # 安全删除
git branch -D new-feature # 强制删除
# 普通合并(创建合并提交)
git merge feature-branch
# 变基合并(线性历史)
git checkout feature-branch
git rebase main
git checkout main
git merge feature-branch
# 解决冲突后继续
git add conflicted-file.txt
git rebase --continue
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 推送到远程
git push -u origin main # 首次推送
git push # 后续推送
# 从远程拉取
git pull origin main # 拉取并合并
git fetch origin # 仅获取不合并
# 1. 获取最新代码
git fetch origin
# 2. 基于远程分支创建本地分支
git checkout -b feature-x origin/feature-x
# 3. 开发完成后推送
git push origin feature-x
# 4. 创建Pull Request/Merge Request
# (在GitHub/GitLab界面上操作)
# 5. 同步主分支
git checkout main
git pull origin main
# 跟踪远程分支
git checkout --track origin/feature-y
# 删除远程分支
git push origin --delete old-branch
# 更新远程分支列表
git remote update origin --prune
# 撤销工作目录修改
git checkout -- filename.txt
# 撤销暂存区文件
git reset HEAD filename.txt
# 修改最后一次提交
git commit --amend
# 回退到特定提交
git reset --hard commit_hash # 彻底回退
git reset --soft commit_hash # 保留更改到暂存区
git revert commit_hash # 创建反向提交
# 储藏当前工作
git stash
git stash save "描述信息"
# 查看储藏列表
git stash list
# 恢复储藏
git stash apply stash@{0}
git stash pop # 恢复并删除
# 删除储藏
git stash drop stash@{0}
# 创建标签
git tag v1.0.0
git tag -a v1.0.1 -m "Release version 1.0.1"
# 推送标签
git push origin v1.0.0
git push origin --tags # 推送所有标签
# 删除标签
git tag -d v0.9.0
git push origin --delete v0.9.0
# 查看引用
cat .git/HEAD
cat .git/refs/heads/main
# 查看对象内容
git cat-file -p commit_hash
# 创建blob对象
echo "content" | git hash-object -w --stdin
# 创建tree对象
git update-index --add --cacheinfo 100644 blob_hash filename.txt
git write-tree
# 初始化Git Flow
git flow init
# 开始新功能
git flow feature start feature-name
# 完成功能
git flow feature finish feature-name
# 发布版本
git flow release start 1.0.0
git flow release finish 1.0.0
# 更美观的log
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# 别名配置(添加到~/.gitconfig)
[alias]
co = checkout
br = branch
ci = commit
st = status
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
git bisect start
git bisect bad # 当前版本有问题
git bisect good v1.0.0 # 标记已知好版本
# 测试后标记good/bad
git bisect reset # 结束
# 查看冲突文件
git status
# 手动解决冲突后
git add resolved-file.txt
git commit
# 查找删除的分支提交
git reflog
# 恢复分支
git branch branch-name commit-hash
# 使用git-lfs管理大文件
git lfs install
git lfs track "*.psd"
git add .gitattributes
# 交互式变基
git rebase -i HEAD~5
# 清理历史大文件
git filter-branch --tree-filter 'rm -f large-file.iso' HEAD
提交规范:
feat: 添加用户登录功能
fix: 修复首页加载错误
docs: 更新API文档
分支管理:
工作习惯:
团队协作:
通过掌握这些Git操作,您将能够高效管理代码版本,无论是个人项目还是团队协作开发。记住,Git的强大在于实践,多尝试不同的命令和工作流,找到最适合您项目的方式。