配置信息
命令 | 说明 |
---|---|
git config --global user.name "Your Name" | 设置全局用户名 |
git config --global user.email "email@example.com" | 设置全局邮箱 |
git config --list | 查看当前配置 |
仓库操作
命令 | 说明 |
---|---|
git init | 初始化新仓库 |
git clone <repo_url> | 克隆远程仓库 |
git remote add origin <repo_url> | 关联远程仓库 |
git remote -v | 查看远程仓库地址 |
提交更改
命令 | 说明 |
---|---|
git status | 查看文件状态 |
git add <file> | 添加文件到暂存区 |
git add . | 添加所有修改到暂存区 |
git commit -m "Commit message" | 提交更改 |
git commit --amend | 修改最后一次提交 |
分支管理
命令 | 说明 |
---|---|
git branch | 查看本地分支 |
git branch <branch_name> | 创建新分支 |
git checkout <branch_name> | 切换到分支 |
git checkout -b <branch_name> | 创建并切换分支 |
git merge <branch_name> | 合并分支到当前分支 |
git branch -d <branch_name> | 删除本地分支 |
git push origin --delete <branch_name> | 删除远程分支 |
同步与推送
命令 | 说明 |
---|---|
git fetch | 下载远程变更(不合并) |
git pull | 拉取远程变更并合并(git fetch + git merge ) |
git pull --rebase | 拉取远程变更并变基 |
git push origin <branch_name> | 推送分支到远程 |
git push -u origin <branch_name> | 推送并关联远程分支(首次推送) |
撤销操作
命令 | 说明 |
---|---|
git restore <file> | 撤销工作区修改 |
git restore --staged <file> | 撤销暂存区修改(保留工作区更改) |
git reset HEAD~1 | 回退最后一次提交(保留修改) |
git reset --hard HEAD~1 | 彻底回退最后一次提交(丢弃修改) |
git revert <commit_id> | 创建新提交来撤销指定提交 |
查看信息
命令 | 说明 |
---|---|
git log | 查看提交历史 |
git log --oneline | 简洁版提交历史 |
git diff | 查看工作区与暂存区的差异 |
git diff --staged | 查看暂存区与最新提交的差异 |
git show <commit_id> | 查看某次提交的详细信息 |
储藏与标签
命令 | 说明 |
---|---|
git stash | 临时储藏当前修改 |
git stash pop | 恢复储藏的修改 |
git tag v1.0 | 创建轻量标签 |
git tag -a v1.0 -m "Release" | 创建含注释的标签 |
git push origin --tags | 推送所有标签到远程 |
高级操作
命令 | 说明 |
---|---|
git rebase <branch> | 将当前分支变基到目标分支 |
git cherry-pick <commit_id> | 复制指定提交到当前分支 |
git reflog | 查看所有操作记录(用于恢复误删提交) |
提示:
- 使用
git <command> -h
查看命令帮助(如git commit -h
)- 谨慎使用
git reset --hard
和git push --force
(会覆盖历史)