Skip to content

Git Cheatsheet

Published:

分支操作

删除本地分支

git branch -d <branch-name>

删除远程分支

git push origin :<branch-name>
# 或
git push origin --delete <branch-name>

找回被误删的分支

git reflog --date=iso
git checkout -b <branch-name> <commit-id>
git push origin <branch-name>

找回被误删的本地分支

# 查看日志,找到需要找回的commit id
git log -g
git branch <branch-name>
git checkout <branch-name>
git rebase <commit-id>

分支整体覆盖

# 若使用该命令误操作将远程分支整体覆盖(一般发生在两个远程仓库相同的项目中),可在未被污染的项目的该本地分支重新执行一次该命令
git push --force

创建一个无关分支

# 在非新存储库上以类似git init的状态创建分支
git checkout --orphan <branch-name>

提交发布

撤回commit

git reset --soft HEAD^

发布tags

git push --follow-tags origin <branch-name>

提交合并

变基

# 1. 找到需要合并提交的前一个不需要合并的commit id
git log
git rebase -i <commit-id>
# 2. 将第一行以外的pick -> s
# 3. 删除无用的message,保存后强制推送
git push origin <branch-name> --force

将分支的所有变更转移到当前分支

git merge <branch-name> --squash

仓库迁移

本地项目迁移

git remote set-url origin <new-git-url>
git push -f origin

远程镜像迁移

git clone --bare https://gitee.com/mygit-demo/test.git
cd test.git/
git push --mirror https://gitee.com/mygit-demo/git-demo.git

Previous Post
Markdown Cheatsheet