Git 本地常见快捷操作
📌 1. 基本操作
| 操作 | 命令 |
|---|---|
| 初始化 Git 仓库 | git init |
| 查看 Git 状态 | git status |
| 添加所有文件到暂存区 | git add . |
| 添加指定文件 | git add <file> |
| 提交更改 | git commit -m "提交信息" |
| 修改最后一次提交信息 | git commit --amend -m "新提交信息" |
| 显示提交历史 | git log --oneline --graph |
| 显示修改的文件 | git diff |
📌 2. 分支管理
| 操作 | 命令 |
|---|---|
| 查看当前分支 | git branch |
| 创建新分支 | git branch <branch-name> |
| 切换分支 | git checkout <branch-name> |
| 创建并切换到新分支 | git checkout -b <branch-name> |
| 删除本地分支 | git branch -d <branch-name> |
| 强制删除本地分支 | git branch -D <branch-name> |
📌 3. 远程仓库
| 操作 | 命令 |
|---|---|
| 查看远程仓库 | git remote -v |
| 添加远程仓库 | git remote add origin <repo-url> |
| 删除远程仓库 | git remote remove origin |
| 推送到远程仓库 | git push origin <branch-name> |
| 拉取远程分支 | git pull origin <branch-name> |
| 获取远程更新但不合并 | git fetch origin |
📌 4. 回退与撤销
| 操作 | 命令 |
|---|---|
撤销 git add 操作 | git reset HEAD <file> |
| 撤销最后一次提交 | git reset --soft HEAD~1 |
| 强制回退到某个提交 | git reset --hard <commit-hash> |
| 撤销对文件的修改 | git checkout -- <file> |
📌 5. 临时存储
| 操作 | 命令 |
|---|---|
| 保存未提交的更改 | git stash |
| 查看存储的更改 | git stash list |
| 恢复最近的存储 | git stash pop |
| 应用某个存储项 | git stash apply stash@{0} |
| 删除某个存储项 | git stash drop stash@{0} |
📌 6. 其他常用命令
| 操作 | 命令 |
|---|---|
| 查看某个文件的修改历史 | git log -- <file> |
| 查看某次提交的详细信息 | git show <commit-hash> |
| 配置用户名 | git config --global user.name "你的名字" |
| 配置邮箱 | git config --global user.email "你的邮箱" |
| 列出所有别名 | git config --global --list |
| 清除所有未跟踪的文件(慎用) | git clean -fd |