关于IDEA上Git的最佳实践方式,合并、检出、加标签,回滚等等

怼烎@ 2024-04-18 15:58 33阅读 0赞

关于IDEA上Git的最佳实践方式

    • 写在前面
    • 一、提交记录Tree
    • 二、基本操作
      • 2.1、基于远程端
      • 2.2、基于本地初始化项目
    • 三、高级操作
      • 3.1、合并两个本地库
      • 3.2、合并两个远程库
    • 3.3、加标签
    • 3.4、代码回滚
      • 3.4.1、回滚 - 对于未提交的,可直接 Revert
      • 3.4.2、回滚 - 已提交,未推送
      • 3.4.3、回滚 - 已推送
    • 3.4.1、单文件基于标签回滚
    • 3.4.1、基于提交标签回滚
    • 四、操作中的问题
      • 4.1、当我试着在B分支上合并A上没有commit的记录时,发现那个文件在B分支下(甚至任何本地分支下),也是可见的未提交的

写在前面

基于GIT的操作方式,主要种,有人习惯命令行,说是命令行专业点,有人倾向可视化的操作,可视化的东西,是最容易掌握的,习惯用命令行的,多半是顾虑GIT的可视化的操作,是否满足平时的基本命令的问题,基于个人的习惯学习吧,我还是倾向可视化的东西,学习。

一、提交记录Tree

刚new的初始化项目 master,后来又在本地的不同分支,new Branch出V1,V2,V3分支,以及相关操作命令日志
master
V1 从master new Branch
V2 从 V1 new Branch
V3 从 master new Branch
在这里插入图片描述
以及对应所有的命令

  1. 10:29:16.858: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/v1.js
  2. 10:29:36.390: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v1.js
  3. 10:29:36.985: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  4. [v1 df30d49] V1版本提交
  5. 1 file changed, 3 insertions(+)
  6. create mode 100644 js/v1.js
  7. 10:30:25.413: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/v1:v1 --set-upstream
  8. Enumerating objects: 6, done.
  9. Delta compression using up to 8 threads
  10. Total 4 (delta 1), reused 0 (delta 0)
  11. remote:
  12. remote: Create a pull request for 'v1' on GitHub by visiting:
  13. remote: https://github.com/tonels/GitDemo/pull/new/v1
  14. remote:
  15. To https://github.com/tonels/GitDemo.git
  16. * refs/heads/v1:refs/heads/v1 [new branch]
  17. Branch 'v1' set up to track remote branch 'v1' from 'origin'.
  18. Done
  19. 10:33:43.386: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout -b V2
  20. Switched to a new branch 'V2'
  21. 10:35:46.980: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/v2.js
  22. 10:35:51.804: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v2.js
  23. 10:35:52.223: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  24. [V2 04f36ca] V2版本提交
  25. 1 file changed, 3 insertions(+)
  26. create mode 100644 js/v2.js
  27. 10:36:01.903: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V2:V2 --set-upstream
  28. Enumerating objects: 6, done.
  29. Delta compression using up to 8 threads
  30. Total 4 (delta 1), reused 0 (delta 0)
  31. remote:
  32. remote: Create a pull request for 'V2' on GitHub by visiting:
  33. remote: https://github.com/tonels/GitDemo/pull/new/V2
  34. remote:
  35. To https://github.com/tonels/GitDemo.git
  36. * refs/heads/V2:refs/heads/V2 [new branch]
  37. Branch 'V2' set up to track remote branch 'V2' from 'origin'.
  38. Done
  39. 10:43:32.685: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v2.js
  40. 10:43:33.154: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  41. [V2 ebc98fc] V2版本,第二次提交
  42. 1 file changed, 1 insertion(+)
  43. 10:43:38.810: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V2:V2
  44. Enumerating objects: 7, done.
  45. Delta compression using up to 8 threads
  46. Total 4 (delta 1), reused 0 (delta 0)
  47. To https://github.com/tonels/GitDemo.git
  48. refs/heads/V2:refs/heads/V2 04f36ca..ebc98fc
  49. Done
  50. 10:51:29.957: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false -c core.commentChar= rebase origin/master
  51. 10:57:46.724: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
  52. Switched to branch 'master'
  53. Your branch is up to date with 'origin/master'.
  54. 10:59:57.471: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/mt.js
  55. 11:00:02.352: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  56. 11:00:02.771: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  57. [master 1af17eb] master 提交
  58. 1 file changed, 3 insertions(+)
  59. create mode 100644 js/mt.js
  60. 11:00:31.208: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master
  61. Enumerating objects: 6, done.
  62. Delta compression using up to 8 threads
  63. Total 4 (delta 1), reused 0 (delta 0)
  64. To https://github.com/tonels/GitDemo.git
  65. refs/heads/master:refs/heads/master 028955b..1af17eb
  66. Done
  67. 11:01:22.987: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V2 --
  68. Switched to branch 'V2'
  69. Your branch is up to date with 'origin/V2'.
  70. 11:03:08.143: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v2.js
  71. 11:03:08.589: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  72. [V2 1b92782] V2版本第三次 提交
  73. 1 file changed, 1 insertion(+)
  74. 11:03:12.467: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V2:V2
  75. Enumerating objects: 7, done.
  76. Delta compression using up to 8 threads
  77. Total 4 (delta 2), reused 0 (delta 0)
  78. To https://github.com/tonels/GitDemo.git
  79. refs/heads/V2:refs/heads/V2 ebc98fc..1b92782
  80. Done
  81. 11:09:44.300: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v2.js
  82. 11:09:44.737: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  83. [V2 c9b931b] V2版本第四次 提交
  84. 1 file changed, 1 insertion(+)
  85. 11:09:47.943: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V2:V2
  86. Enumerating objects: 7, done.
  87. Delta compression using up to 8 threads
  88. Total 4 (delta 2), reused 0 (delta 0)
  89. To https://github.com/tonels/GitDemo.git
  90. refs/heads/V2:refs/heads/V2 1b92782..c9b931b
  91. Done
  92. 11:10:28.959: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
  93. Switched to branch 'master'
  94. Your branch is up to date with 'origin/master'.
  95. 11:11:44.368: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  96. 11:11:44.818: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  97. [master 435fa50] master,第二次提交
  98. 1 file changed, 2 insertions(+)
  99. 11:11:49.467: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master
  100. Enumerating objects: 7, done.
  101. Delta compression using up to 8 threads
  102. Total 4 (delta 1), reused 0 (delta 0)
  103. To https://github.com/tonels/GitDemo.git
  104. refs/heads/master:refs/heads/master 1af17eb..435fa50
  105. Done
  106. 11:18:25.832: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout v1 --
  107. Switched to branch 'v1'
  108. Your branch is up to date with 'origin/v1'.
  109. 11:18:42.635: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
  110. Switched to branch 'master'
  111. Your branch is up to date with 'origin/master'.
  112. 11:21:33.365: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout -b V3
  113. Switched to a new branch 'V3'
  114. 12:09:08.354: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  115. 12:09:08.811: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  116. [V3 fd68c04] master new Branch V3 分支,第一次提交,会复制当前到V3分支
  117. 1 file changed, 1 deletion(-)
  118. 12:09:20.995: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V3:V3 --set-upstream
  119. Enumerating objects: 7, done.
  120. Delta compression using up to 8 threads
  121. Total 4 (delta 2), reused 0 (delta 0)
  122. remote:
  123. remote: Create a pull request for 'V3' on GitHub by visiting:
  124. remote: https://github.com/tonels/GitDemo/pull/new/V3
  125. remote:
  126. To https://github.com/tonels/GitDemo.git
  127. * refs/heads/V3:refs/heads/V3 [new branch]
  128. Branch 'V3' set up to track remote branch 'V3' from 'origin'.
  129. Done
  130. 12:12:27.401: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  131. 12:12:27.848: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  132. [V3 90c21fb] V3 分支,第二次提交
  133. 1 file changed, 1 insertion(+)
  134. 12:12:49.039: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V3:V3
  135. Enumerating objects: 7, done.
  136. Delta compression using up to 8 threads
  137. Total 4 (delta 2), reused 0 (delta 0)
  138. To https://github.com/tonels/GitDemo.git
  139. refs/heads/V3:refs/heads/V3 fd68c04..90c21fb
  140. Done
  141. 12:13:58.162: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  142. 12:13:58.626: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  143. [V3 57af067] V3 分支,第二次提交 +1
  144. 1 file changed, 1 insertion(+), 1 deletion(-)
  145. 12:14:02.321: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V3:V3
  146. Enumerating objects: 7, done.
  147. Delta compression using up to 8 threads
  148. Total 4 (delta 2), reused 0 (delta 0)
  149. To https://github.com/tonels/GitDemo.git
  150. refs/heads/V3:refs/heads/V3 90c21fb..57af067
  151. Done
  152. 12:14:47.896: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout v1 --
  153. Your branch is up to date with 'origin/v1'.
  154. Switched to branch 'v1'
  155. 12:16:14.079: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v1.js
  156. 12:16:14.513: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  157. [v1 6834d65] V1版本第二次提交
  158. 1 file changed, 1 insertion(+)
  159. 12:16:20.495: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/v1:v1
  160. Enumerating objects: 7, done.
  161. Delta compression using up to 8 threads
  162. Total 4 (delta 1), reused 0 (delta 0)
  163. To https://github.com/tonels/GitDemo.git
  164. refs/heads/v1:refs/heads/v1 df30d49..6834d65
  165. Done
  166. 12:17:06.349: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V2 --
  167. Your branch is up to date with 'origin/V2'.
  168. Switched to branch 'V2'
  169. 12:17:48.771: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/v2.js
  170. 12:17:49.226: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  171. [V2 49fab0d] V2版本第五次提交
  172. 1 file changed, 1 insertion(+)
  173. 12:17:54.211: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V2:V2
  174. Enumerating objects: 7, done.
  175. Delta compression using up to 8 threads
  176. Total 4 (delta 2), reused 0 (delta 0)
  177. To https://github.com/tonels/GitDemo.git
  178. refs/heads/V2:refs/heads/V2 c9b931b..49fab0d
  179. Done
  180. 12:18:23.563: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V3 --
  181. Switched to branch 'V3'
  182. Your branch is up to date with 'origin/V3'.
  183. 12:19:21.417: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  184. 12:19:21.857: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  185. [V3 99ae4e8] V3版本第三次提交
  186. 1 file changed, 1 insertion(+)
  187. 12:20:37.615: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  188. 12:20:38.059: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  189. [V3 f84cede] V3版本第四次提交
  190. 1 file changed, 1 insertion(+)
  191. 12:27:43.810: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false cherry-pick -n 99ae4e8afc6463afcc8ab9ff5a2c0371e7fcc6a4
  192. error: could not apply 99ae4e8... V3版本第三次提交
  193. hint: after resolving the conflicts, mark the corrected paths
  194. hint: with 'git add <paths>' or 'git rm <paths>'
  195. 12:31:17.707: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/V3:V3
  196. Enumerating objects: 11, done.
  197. Delta compression using up to 8 threads
  198. Total 8 (delta 3), reused 0 (delta 0)
  199. To https://github.com/tonels/GitDemo.git
  200. refs/heads/V3:refs/heads/V3 57af067..f84cede
  201. Done
  202. 12:32:52.743: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
  203. error: you need to resolve your current index first
  204. js/mt.js: needs merge
  205. 12:33:25.984: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout --ours -- js/mt.js
  206. 12:33:26.416: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  207. 12:33:51.868: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
  208. Switched to branch 'master'
  209. Your branch is up to date with 'origin/master'.
  210. 12:34:39.518: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- js/mt.js
  211. 12:34:39.962: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Administrator\AppData\Local\Temp\git-commit-msg-.txt --
  212. [master 7ac9c02] master版本第三次提交
  213. 1 file changed, 1 insertion(+)
  214. 12:34:43.532: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master
  215. Enumerating objects: 7, done.
  216. Delta compression using up to 8 threads
  217. Total 4 (delta 2), reused 0 (delta 0)
  218. To https://github.com/tonels/GitDemo.git
  219. refs/heads/master:refs/heads/master 435fa50..7ac9c02
  220. Done

二、基本操作

首先明确,项目的初始化,基于何处,Github远程端,还是基于本地IDEA初始化项目的

2.1、基于远程端

这种情况,我们只需git pull 即可,如果已存在多个分支,通过IDEA导入的时候,默认初始窗口加载的是master分支,其实还有几个分支,可切换,判断当前情况下合适的分支检出,开发
在这里插入图片描述

2.2、基于本地初始化项目

这种情况是,我们只需执行 git commit ,还有push命令

在这里插入图片描述
我们还要根据需要,检出几个开发的分支,最后开发完成时,还要合并分支

三、高级操作

应该注意,所有操作都要尽量基于commit,甚至push的,我这里遇到一些问题,记录参考第四条

3.1、合并两个本地库

这个操作,并不多…将本地 master-a 分支合并到本地 master 上,先切换到master 分支,然后操作,如果在master-a上已提交,合并时是不需要重新commit的,直接push 到远程master即可
在这里插入图片描述
在这里插入图片描述

3.2、合并两个远程库

这个操作可能比较多,最后开发完成后,最终所有的Dev分支都要合并到主分支上
首先先切换到目标分支上,执行以下的操作
在这里插入图片描述
因为基于远程库的合并,所以master分支只是合并了 master-a 上已push的部分
在这里插入图片描述

3.3、加标签

我们在本地 Commit 后,查看VC Log中看到如下

在这里插入图片描述

标签建好后,push 到远程端,登录远程端,即可看到标签已推送

3.4、代码回滚

Git 的回滚操作是指将代码回退到某一历史时刻

3.4.1、回滚 - 对于未提交的,可直接 Revert

右键Revert,IDEA封装Git命令控制台如下

  1. git -c credential.helper= -c core.quotepath=false -c log.showSignature=false rm --cached -f -- queryDsl/DslDemo1/src/main/java/querydsl/controller/CityController.java
  2. git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout HEAD -- queryDsl/DslDemo1/src/main/java/querydsl/controller/CityController.java

3.4.2、回滚 - 已提交,未推送

todo

3.4.3、回滚 - 已推送

todo

GIt 的回滚操作
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

先从需要回滚的分支上检出,另一个分支,用于回滚和修改,调整
基于提交 tree记录,找到合适的head,然后回滚到当前的点,如上图示

3.4.1、单文件基于标签回滚

3.4.1、基于提交标签回滚

四、操作中的问题

4.1、当我试着在B分支上合并A上没有commit的记录时,发现那个文件在B分支下(甚至任何本地分支下),也是可见的未提交的

发表评论

表情:
评论列表 (有 0 条评论,33人围观)

还没有评论,来说两句吧...

相关阅读