序
自用的常见命令,可直接复制运行。
执行命令,建议使用 git bash
删除本地分支
# 删除当前分支外的所有本地分支
git branch | grep -v "^\*" | xargs git branch -D
# 删除所有本地分支
git branch | xargs git branch -D
Git 从远程仓库获取最新代码
# 会清理在远程仓库中已经被删除的分支
git fetch --prune
查看指定提交的代码
# 这会将你的工作目录切换到指定的提交 5d6c5f3。
$ git checkout 5d6c5f3
全部替换某分支上的内容
# 清除所有文件
git rm -r --cached .
# 将新文件添加到暂存区
git add .
# 提交变更
git commit -m "Replace all files and clear history"
# 强推到远程仓库
git push -f origin gh-pages
新建空分支
# 这将创建一个新的空分支,该分支不会包含任何历史记录。
git checkout --orphan gh-pages
# 清空当前所有文件
git rm -rf .
基于 commit 创建一个新的分支
# 'abc1234' 为 commit hash
git checkout -b my-new-branch abc1234
当前分支 push 到指定 remote 的新分支上
# `gitlab` remote 名称, `HEAD` 表示当前分支, `fix-bugs` 新分支名称
git push gitlab HEAD:fix-bugs