常用git操作场景实践

常用git操作场景实践

一 提交

  1. 查看提交了什么
 git show  
 #或者
  git log -n1 -p  
  1. 更改提交信息内容
    用于已经commit但还未push
git commit --amend --only -m 'xxxxxxx'  
  1. 修改提交的用户名和邮箱
git commit --amend --author "New Authorname <[email protected]>" 
  1. 从提交的内容中移除一个文件
 git checkout HEAD^ myfile  
 git add -A  
 git commit --amend 
  1. 删除任意提交
    非必要不要这么做
 git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT  
 git push -f [remote] [branch]  
  1. 做了硬重置,找回内容
#查看日志
git reflog
#重置到你想要的地方
git reset --hard SHA1234  

二 分支

  1. 从错误分支拉取内容
#找到pull之前head的指向
git reflog
#重置到你所需的提交 
git reset --hard c5bc55a  
  1. 我需要提交到一个新分支,但错误的提交到了main
    在main下创建一个新分支,不切换到新分支,仍在main下:
git branch my-branch  

把main分支重置到前一个提交:

git reset --hard HEAD^  
  1. 删除的分支恢复
git reflog
#从日志中获取删除的分支
git checkout -b my-branch-help 
#重置到提交删除的地方
git reset --hard 4e3cd85
  1. 删除分支
    删除本地分支
git branch -D my-branch  

删除远程分支

git push origin --delete my-branch 
  1. 从远程分支签出一个分支
git fetch --all  
git checkout --track origin/daves 

rebasing和Merging

  1. 我想撤销rebase/merge
git reset --hard ORIG_HEAD
  1. 我以及rebase了,不想强推
git checkout my-branch  
git rebase -i main  
git checkout main  
git merge --ff-only my-branch  

猜你喜欢

转载自blog.csdn.net/hcyxsh/article/details/132084795