常用git操作场景实践
一 提交
- 查看提交了什么
 git show  #或者git log -n1 -p  
- 更改提交信息内容
 用于已经commit但还未push
git commit --amend --only -m 'xxxxxxx'  
- 修改提交的用户名和邮箱
git commit --amend --author "New Authorname <authoremail@mydomain.com>" 
- 从提交的内容中移除一个文件
 git checkout HEAD^ myfile  git add -A  git commit --amend 
- 删除任意提交
 非必要不要这么做
 git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT  git push -f [remote] [branch]  
- 做了硬重置,找回内容
#查看日志
git reflog
#重置到你想要的地方
git reset --hard SHA1234  
二 分支
- 从错误分支拉取内容
#找到pull之前head的指向
git reflog
#重置到你所需的提交 
git reset --hard c5bc55a  
- 我需要提交到一个新分支,但错误的提交到了main
 在main下创建一个新分支,不切换到新分支,仍在main下:
git branch my-branch  
把main分支重置到前一个提交:
git reset --hard HEAD^  
- 删除的分支恢复
git reflog
#从日志中获取删除的分支
git checkout -b my-branch-help 
#重置到提交删除的地方
git reset --hard 4e3cd85
- 删除分支
 删除本地分支
git branch -D my-branch  
删除远程分支
git push origin --delete my-branch 
- 从远程分支签出一个分支
git fetch --all  
git checkout --track origin/daves 
rebasing和Merging
- 我想撤销rebase/merge
git reset --hard ORIG_HEAD
- 我以及rebase了,不想强推
git checkout my-branch  
git rebase -i main  
git checkout main  
git merge --ff-only my-branch