以下是 Git 的常用命令大全,分为几个常见类别,便于理解和使用:
1. 初始化与克隆
- 初始化本地仓库:
git init
- 克隆远程仓库到本地:
git clone <repository_url>
2. 添加与提交
- 添加指定文件到暂存区:
git add <file_name>
- 添加所有文件到暂存区:
git add .
- 提交代码并添加注释:
git commit -m "commit_message"
- 提交所有修改(跳过暂存步骤):
git commit -a -m "commit_message"
3. 查看状态与差异
- 查看当前工作区状态:
git status
- 查看具体文件的改动内容:
git diff <file_name>
- 查看已暂存的改动内容:
git diff --cached
- 查看所有提交历史记录:
git log
4. 分支管理
- 创建新分支:
git branch <branch_name>
- 切换分支:
git checkout <branch_name>
- 创建并切换分支:
git checkout -b <branch_name>
- 删除分支:
git branch -d <branch_name>
- 查看所有分支:
git branch
5. 合并与解决冲突
- 合并指定分支到当前分支:
git merge <branch_name>
- 解决冲突后标记文件为已解决:
git add <file_name>
6. 远程仓库操作
- 查看远程仓库信息:
git remote -v
- 添加远程仓库:
git remote add <remote_name> <repository_url>
- 推送本地分支到远程仓库:
git push <remote_name> <branch_name>
- 拉取远程仓库更新:
git pull <remote_name> <branch_name>
7. 撤销操作
- 撤销暂存的文件:
git reset <file_name>
- 撤销最近一次提交(保留代码修改):
git reset --soft HEAD~1
- 撤销最近一次提交(删除代码修改):
git reset --hard HEAD~1
- 恢复被删除的提交:
git reflog
+git reset --hard <commit_id>
8. 标签操作
- 创建标签:
git tag <tag_name>
- 推送标签到远程仓库:
git push <remote_name> <tag_name>
- 查看所有标签:
git tag
9. 其他实用命令
- 查看 Git 配置信息:
git config --list
- 设置用户名:
git config --global user.name "<name>"
- 设置邮箱:
git config --global user.email "<email>"
- 忽略特定文件(在
.gitignore
中配置)
以上是 Git 的常用命令列表,涵盖了版本控制、分支管理、远程协作等主要场景。建议根据实际需求结合文档或工具进一步深入学习。