深入浅出git

241 阅读2分钟

git outside the repository

windows下 重新安装 git for windows覆盖当前版本,2.9.0会出现该问题,升级至2.22.0以上,该问题即可解决!

谈谈.gitignore文件参考资料

PatternExample matches
**/logslogs/debug.log

git更换远程仓库指令

git remote set-url origin [url]

当我们切换了远程仓库之后,git pull 或者 git push 的时候发生了 fatal:refusing to merge unrelated histories

我们可以使用 allow-unrelated-histories

当多人在同一个branch上进行开发的时候,经常会遇到你在push的时候

****To https://git-wz.nie.netease.com/md-lib/gittutorial.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://git-wz.nie.netease.com/md-lib/gittutorial.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这里我们的push命令被拒绝了,因为我们看到origin(即远程的引用是有更新的),我们可以当然暴力的解决方案是 git push remote名 branch名 -f 暴力上传 , 但是会最近几次他人提交的commit (即在你本地的git log 看不到的commit们)。 如果这个时候 我们简单的使用 git pull命令的话,然后本地解决了冲突,会导致多提交一次 commit 记录 如图所示, 即该无意义的commit,这对将来要回滚代码或者查看commit记录来说不仅增加了commit的记录并且这种记录是无意义的。 但是为什么会产生这个commit是因为 git pull = git fetch + git merge. 这里面缺少了一次rebase的操作。 可以看到我们这个图谱里面,自己目前的代码是基于 fc54b03 ,但是origin/master的也是基于fc54b03 这个时候我们需要将自己的git的Reapply commits on top of another base tip. 因此我们在git rebase之后,会将当前基于的git 指向 184bd55 这个commit记录.从而就不会有那次无意义的提交。 所以正确操作应该是

  1. git fetch
  2. git rebase
  3. 解决冲突
  4. git add 冲突文件
  5. git rebase –continue
  6. git push

更换 git log

bg:当我们在输入的时候回车敲快了导致git log出错,重置git log.

git commit --amend

如果仅仅是改当前这条的还未push的历史,可以在编辑器中进行修改。


更改 分支的名称

git branch -m oldName newName

--- 截止时间为 2020年9月13日10:17:11 ---