You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.0 KiB
3.0 KiB
Git命令
一、仓库初始化与克隆
git init
—— 初始化本地仓库git clone <url>
—— 克隆远程仓库
二、文件操作与提交
git add <file>
—— 添加单个文件到暂存区git add .
—— 添加所有文件到暂存区git commit -m "msg"
—— 提交暂存区到本地仓库git status
—— 查看工作区状态git log
—— 查看提交历史git diff
—— 查看工作区与暂存区的差异
三、分支管理
- 查看分支:
git branch
—— 查看本地分支
- 创建分支:
git branch <name>
—— 创建新分支git checkout -b <name>
—— 创建并切换分支
- 切换分支:
git checkout <name>
—— 切换分支
- 合并分支:
git merge <name>
—— 合并指定分支到当前分支
- 删除分支:
git branch -d <name>
—— 删除已合并分支git branch -D <name>
—— 强制删除分支
四、远程仓库操作
- 查看远程信息:
git remote -v
—— 查看远程仓库信息
- 添加远程仓库:
git remote add <name> <url>
—— 添加远程仓库
- 拉取与推送:
git pull <remote> <branch>
—— 拉取远程分支并合并git push <remote> <branch>
—— 推送本地分支到远程git push -u <remote> <branch>
—— 首次推送并关联上游分支
- 获取远程分支:
git fetch <remote>
—— 仅获取远程分支(不合并)
五、版本回退与撤销
- 撤销修改:
git checkout -- <file>
—— 撤销工作区修改git reset HEAD <file>
—— 撤销暂存区修改
- 回退版本:
git reset --hard <commit>
—— 回退到指定提交git revert <commit>
—— 撤销指定提交(生成新提交)
六、临时存储与标签
- 临时存储:
git stash
—— 临时保存当前修改git stash pop
—— 恢复最近一次 stash
- 标签操作:
git tag <name>
—— 创建轻量标签git tag -a <name> -m "msg"
—— 创建带注释标签git tag
—— 查看所有标签git push <remote> <tag>
—— 推送单个标签到远程git push <remote> --tags
—— 推送所有标签
七、高级操作
- 变基与提交管理:
git rebase <branch>
—— 变基(线性化提交历史)git cherry-pick <commit>
—— 复制指定提交到当前分支
- 代码追溯:
git blame <file>
—— 查看文件每行的修改历史git bisect
—— 二分查找定位问题提交
八、配置管理
- 用户配置:
git config --global user.name "Your Name"
—— 设置用户名git config --global user.email "you@example.com"
—— 设置邮箱
- 查看配置:
git config --list
—— 查看所有配置信息