创建分支
根据 commit 创建新分支
git checkout -b branch_name commit_id_xxx
回滚
还原本地修改过的文件
# 会还原本地所有修改过的文件!!
git checkout .
# 删除新增未监视的文件(untracked files)
git clean -fd
丢弃本地上一次提交
# 回退到上一个 commit 版本
git reset HEAD^
回滚到指定版本
# 还原到指定的旧版本
git reset --hard <old_commit_id>
# 还原到上一版本
git reset HEAD^
# 新修改处理...
# 强制push更新覆盖远程仓库
git push -f origin master
标签 tag
删除指定 tag
# 先删除远程tag
git push origin :refs/tags/v0.x.x
# 再删除本地tag
git tag -d v0.x.x
删除匹配的部分 tag
# 先删除远程tag,支持*模糊匹配
git push -d origin $(git tag -l "v0.1*")
# 再删除本地tag
git tag -d $(git tag -l "v0.1*")
# 先删除所有远程tag
git tag -l | xargs -n 1 git push --delete origin
# 再删除所有本地tag
git tag | xargs git tag -d
Commit 修改
清空所有历史 commit
# 创建新的master分支
git checkout --orphan new_master
git add -A
git commit -am "First commit"
# 删除旧master分支
git branch -D master
# 新master分支改名为master
git branch -m master
# 强制push更新覆盖远程仓库
git push -f origin master
合并多个 commit
# 切换回master分支,并获取最新代码
git checkout master
git pull
# 把分支的多个提交合并为一个
git merge --squash feature-branch-xxxx
git commit -m "merge commit message"
git push
多帐号
提交时提示输入帐号
git config --local credential.helper ""
# git地址要使用http协议
git remote set-url origin https://xxx.com/xxx.git