programming_git_useful.rst 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. .. eyefall post example, created by `ablog start` on Jan 25, 2021.
  2. Git in real world
  3. ===================
  4. .. post:: Jan 30, 2021
  5. :tags: git
  6. :category: programming
  7. Fetch tags from remote repository
  8. ----------------------------------
  9. .. code-block:: shell
  10. $git fetch --all --tags
  11. Checkout a tag as a branch
  12. ----------------------------
  13. .. code-block:: shell
  14. $git checkout tags/<tag_name> -b <new_branch_name>
  15. Ignore ^M as a newline when execute 'git diff'
  16. -----------------------------------------------
  17. .. code-block:: shell
  18. $git config --global core.autocrlf true
  19. 查看指定文件修改历史
  20. -------------------------
  21. .. code-block:: shell
  22. $git log --pretty=oneline [--oneline] <filename>
  23. 查看单次提交详情
  24. ----------------------------
  25. .. code-block:: shell
  26. $git show <commit>
  27. 撤销 git add
  28. ------------------------
  29. .. code-block:: shell
  30. $git reset HEAD
  31. $git reset HEAD <filename>
  32. $git reset HEAD <path>
  33. $git rm --cached <filename>
  34. 撤销 commit (没 push)
  35. -----------------------------
  36. .. code-block:: shell
  37. $git reset --soft HEAD^
  38. # 如果撤销 2 次 commit
  39. $git reset --soft HEAD~2
  40. # 其他参数
  41. --mixed 为默认参数,不删除工作空间代码改动,撤销 commit,并撤销 add
  42. # $git reset --mixed HEAD~2 == $git reset HEAD~2
  43. --soft 不删除工作空间代码改动,撤销 commit, 不撤销 add
  44. --hard 删除工作空间代码改动,撤销 commit,撤销 add
  45. rebase 远程分支
  46. ---------------------------
  47. 假设情景如下: 有一些本地分支,其中 master 是用来同步的主分支
  48. 对应自己有一个远程仓库 origin, origin 是 clone 自另一个仓库
  49. 假设地址在 https://github.com/a/b.git. 现在需要拉取原始仓库的更新
  50. .. code-block:: shell
  51. $git remote add upstream https://github.com/a/b.git
  52. $git fetch upstream
  53. $git checkout master
  54. $git rebase upstream/master
  55. $git push -f origin master