Skip to content

Instantly share code, notes, and snippets.

@shhider
Last active May 9, 2023 07:06
Show Gist options
  • Save shhider/a2890ec0cc71ffd9c1ce1844e21b81d9 to your computer and use it in GitHub Desktop.
Save shhider/a2890ec0cc71ffd9c1ce1844e21b81d9 to your computer and use it in GitHub Desktop.
[Git Lite Docs] #git #litedoc #commands
# 只 clone 最新的代码,不要 commits/tags...
git clone https://xxx/xxx.git --depth 1
# 这称为 shallow clone
# 如果后面又需要完整的仓库
git pull --unshallow
# === 恢复数据 ===
# reflog 可以查看你每次 git 操作后的 hash
git reflog
# 以 reflog 的 hash 对应的状态,新建一个分支
git branch your-recover-branch-name xxxxxxx
# 撤销上一个 commit,commit 的修改会回到暂存区,可以添加修改后再 commit,达到修改 commit 的目的
git reset --soft HEAD^
# 也可以直接修改后,git add 修改的内容,然后
git commit --amend # 直接添加到上个 commit
# === 查看分支的差异 ===
# 有时候清理本地分支,不知道某个分支的 commits 是否都已经合到 master。
# 可以用以下命令列出 dev 分支有、而 master 分支没有的 commits:
git log dev ^master
# or
git log master..dev
# 假设有主分支 master 和开发分支 dev,
# 现在想要将 dev 分支 push 到远程,准备提 merge request,
# 在这之前要先把 master 上新的提交加到 dev 上,用 merge or rebase 分别是:
# 1. merge
# 将 master 合并到 dev 分支
git merge master
# 2. rebase
# 基于 master,对 dev 的 commits 进行变基,
# 相当于是从现在的 master 分支拉一个新的 dev 分支,照着原来 dev 分支的 commits 重新 commit 一遍
git rebase master
# 交互式 rebase
git rebase -i master
# === 合并 commits ===
# 如合并最近 3 个 commits
git rebase -i HEAD~3
# 在列出的 commits 中,将要合并的 commits 前的 pick 改成 s(squash)
# 修改后保存退出。如无异常,就已经合并了;
# 若有异常,处理异常后需要:
git add .
git rebase --continue
# 查看仓库所有分支,本地+远端
git branch -a
# 查看本地分支
git branch -v
# 查看仓库远端(origin)分支的信息,以及本地跟踪的情况
git remote show origin
# 有些分支在远程(origin)已经被删除了,而本地还在跟踪,就会提示你用以下命令清除这类分支
git remote prune origin
# prune 不会清理你本地 checkout 的分支。
# 删除本地 checkout 的分支,用:
git branch -D branch-name1
# 将其它仓库合并到当前仓库,作为一个分支的代码
# refer: https://stackoverflow.com/a/21353836/3676413
# eg. merge `bar` to `foo`, in `foo` folder:
git remote add bar ../bar
git remote update
git merge --allow-unrelated-histories bar/somebranch
# remove remote tag
git push --delete origin v1.xx.x
# remove local tag
git tag -d tagname
# push tag to remote
git push origin tagname
# only fetch tags from remote
git fetch origin 'refs/tags/*:refs/tags/*'
# check whether a tag exists on remote.
# with -exit-code, this cmd exits with code 2 when the tag doesn't exist.
git ls-remote --exit-code --tags origin xxxxx
# 手动选择要 stash 的修改
git stash push --patch
git stash -p
# 只 stash 未暂存(add)的修改
git stash push --keep-index
git stash -k
# 查找引入问题的 commit
# 开始查找
git bisect start
# 标记当前 commit 出现了问题
git bisect bad
# 标记指定 commit 没有问题
# 执行后 git 会 checkout 中间的 commit
git bisect good v1.0
# 如果此时还有问题:
git bisect bad
# 如果此时没有问题:
git bisect good
# git status 中文显示乱码,如 \123\456.js
git config --global core.quotepath false
# 列出本地 branchs 时以最新的提交时间排序
git config --global branch.sort -committerdate
# 配置一个更简洁的 log 列表
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 忽略文件 mode 的变化
git config --global core.filemode false
# 不显示 merge/pull 时文件 diff 列表
git config --global merge.stat false
# 结合 oh-my-zsh 的变量和历史记录,省去 pull/push 时每次输入分支名
git pull origin $(git_current_branch)
git push origin $(git_current_branch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment