git
git
git reset
git reset --soft <commit>
- 撤销上一次提交,但保留所有更改在暂存区,方便你修改后重新提交。
git restore
- 撤销 工作区 的修改(文件已修改,但未执行 git add)
git restore <file-path>
1 | git restore readme.md # 放弃 readme.md 文件的所有未暂存修改 |
- 撤销 暂存区 的修改(文件已执行 git add,但未 git commit)
git restore --staged <file-path>
1 | git restore --staged readme.md # 将 readme.md 从暂存区撤出 |
- 等效旧命令:git reset HEAD <file-path>
git add
- 添加文件到暂存区
git add <file-path>
1 | git add readme.md # 添加单个文件 |
git commit
- 提交更改到本地仓库
git commit -m “message”
1 | git commit -m "更新文档" # 提交并添加提交信息 |
git push
- 推送本地提交到远程仓库
git push [remote] [branch]
1 | git push # 推送到默认远程分支 |
git pull
- 从远程仓库拉取并合并更改
git pull [remote] [branch]
1 | git pull # 拉取默认远程分支 |
git status
- 查看工作区和暂存区的状态
git status
1 | git status # 查看详细状态 |
git log
- 查看提交历史
git log [options]
1 | git log # 查看完整提交历史 |
git diff
- 查看文件差异
git diff [options]
1 | git diff # 查看工作区与暂存区的差异 |
git stash
- 暂存当前工作区的更改
git stash [options]
1 | git stash # 暂存当前更改 |
创建git
git init
- 初始化一个新的 git 仓库
git init [directory]
1 | git init # 在当前目录初始化 |
git clone
- 克隆远程仓库到本地
git clone [url] [directory]
1 | git clone https://github.com/user/repo.git |
创建分支
- 创建并切换到名为 “name” 的新分支。
git checkout -b name
git switch -c name
1 | git checkout -b feature-branch # 创建并切换到新分支(旧方式) |
git branch
- 分支管理
git branch [options]
1 | git branch # 查看所有本地分支 |
git merge
- 合并分支
git merge [branch]
1 | git merge feature-branch # 合并 feature-branch 到当前分支 |
git checkout
- 切换分支或恢复文件
git checkout [branch|file]
1 | git checkout main # 切换到 main 分支 |
git remote
- 管理远程仓库
git remote [command]
1 | git remote -v # 查看所有远程仓库 |
git fetch
- 从远程仓库获取更新但不合并
git fetch [remote]
1 | git fetch # 获取默认远程仓库的更新 |
git rebase
- 变基操作,将当前分支的提交重新应用到目标分支上
git rebase [branch]
1 | git rebase main # 将当前分支变基到 main |
git tag
- 标签管理
git tag [options]
1 | git tag # 查看所有标签 |
git revert
- 撤销指定提交,创建新的提交来反转更改
git revert [commit]
1 | git revert HEAD # 撤销最后一次提交 |
git reset 详细说明
git reset [mode] [commit]
1 | git reset --soft HEAD~1 # 撤销提交,保留更改在暂存区 |
删除远程remote
git remote -v
- 删除名为 origin 的远程仓库:
git remote remove origin
git config
- 配置 git 设置
git config [options]
1 | git config --global user.name "Your Name" # 设置全局用户名 |
git show
- 显示提交的详细信息
git show [commit]
1 | git show # 显示最后一次提交 |
git submodule
- 子模块管理
git submodule [command]
1 | git submodule status # 显示子模块状态 |
常用组合命令
1 | # 查看状态并查看差异 |
问题
? 添加子module
git submodule stats # 显示submodules
git submodule add -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly # 添加子模块
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 刘骞的博客!