Git的基本操作是每位开发者在使用Git进行版本控制时必须掌握的核心技能。以下是一些最常用的Git命令及其简要说明,涵盖了从设置到日常使用的各个方面:
1. 安装与配置
- 检查Git版本:
git --version
- 配置用户信息:
git config --global user.name "Your Name" git config --global user.email "you@example.com"
2. 初始化仓库
- 在当前目录创建新仓库:
git init
- 克隆现有仓库:
git clone <repository-url>
3. 工作流程
- 查看状态:
git status
- 添加文件到暂存区:
- 单个文件:
git add <file>
- 所有更改:
git add .
- 单个文件:
- 提交更改:
git commit -m "Commit message"
4. 查看提交历史
- 查看提交历史:
git log
- 简化查看:
git log --oneline
- 查看最近一次提交:
git log -1
5. 分支管理
- 查看分支:
git branch
- 创建并切换到新分支:
git checkout -b <branch-name>
- 切换分支:
git checkout <branch-name>
- 合并分支:
git merge <branch-to-merge>
- 删除分支:
git branch -D <branch-name>
6. 远程操作
- 添加远程仓库:
git remote add origin <repository-url>
- 推送更改:
git push origin <branch-name>
- 拉取远程更改:
git pull origin <branch-name>
7. 解决冲突
- 查看冲突文件:通过
git status
找到冲突文件 - 手动编辑解决冲突
- 添加解决后的文件:
git add <conflict-file>
- 提交解决:
git commit -m "Resolved merge conflict"
8. 标签
- 创建标签:
git tag <tag-name>
- 查看所有标签:
git tag
- 推送标签到远程:
git push origin <tag-name>
9. 恢复操作
- 撤销未提交的更改:
git restore <file>
或git checkout -- <file>
- 撤销最近一次提交(不改变工作目录):
git reset HEAD~1
- 撤销最近一次提交并删除更改:
git reset --hard HEAD~1
这些基本操作构成了使用Git进行日常版本控制的基础,随着实践经验的积累,还可以探索更多高级特性和命令,以提高工作效率和团队协作能力。