Git 是一个分布式版本控制系统,用于管理代码的变更历史、协同开发等。以下是Git的一些常用命令:
1. 配置用户信息
git config --global user.name "Your Name" # 设置用户名
git config --global user.email "your_email@example.com" # 设置用户邮箱
2. 创建仓库
git init # 在当前目录创建新的Git仓库
git clone https://github.com/user/repo.git # 克隆远程仓库到本地
3. 文件状态操作
git status # 查看当前文件状态
git add <file> # 将文件添加到暂存区
git add . # 将所有文件添加到暂存区
git commit -m "提交信息" # 将暂存区文件提交到仓库
4. 分支管理
git branch # 查看本地分支
git branch <branch_name> # 创建新分支
git checkout <branch_name> # 切换到指定分支
git checkout -b <branch_name> # 创建并切换到新分支
git merge <branch_name> # 合并指定分支到当前分支
git rebase <branch_name> # 变基操作,将当前分支的提交应用到指定分支上
5. 远程仓库同步
git remote add origin https://github.com/user/repo.git # 添加远程仓库地址
git fetch origin # 从远程仓库获取最新数据,但不自动合并
git pull origin <branch_name> # 从远程仓库获取并合并指定分支
git push origin <branch_name> # 将本地指定分支推送到远程仓库
6. 其他常用命令
git log # 查看提交历史
git diff # 查看文件差异
git stash # 暂存当前工作,切换分支或进行其他操作后可恢复
git tag # 查看标签
git tag <tag_name> # 创建标签
git checkout <tag_name> # 切换到指定标签
以上命令涵盖了Git的日常使用场景,通过合理运用这些命令,可以高效地进行代码版本管理和团队协作开发。