天天看點

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

關于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

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

以及對應所有的指令

10:29:16.858: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/v1.js
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
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 --
[v1 df30d49] V1版本送出
 1 file changed, 3 insertions(+)
 create mode 100644 js/v1.js
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
Enumerating objects: 6, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'v1' on GitHub by visiting:        
remote:      https://github.com/tonels/GitDemo/pull/new/v1        
remote: 
To https://github.com/tonels/GitDemo.git
* refs/heads/v1:refs/heads/v1 [new branch]
Branch 'v1' set up to track remote branch 'v1' from 'origin'.
Done
10:33:43.386: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout -b V2
Switched to a new branch 'V2'
10:35:46.980: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/v2.js
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
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 --
[V2 04f36ca] V2版本送出
 1 file changed, 3 insertions(+)
 create mode 100644 js/v2.js
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
Enumerating objects: 6, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'V2' on GitHub by visiting:        
remote:      https://github.com/tonels/GitDemo/pull/new/V2        
remote: 
To https://github.com/tonels/GitDemo.git
* refs/heads/V2:refs/heads/V2 [new branch]
Branch 'V2' set up to track remote branch 'V2' from 'origin'.
Done
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
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 --
[V2 ebc98fc] V2版本,第二次送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V2:refs/heads/V2 04f36ca..ebc98fc
Done
10:51:29.957: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false -c core.commentChar= rebase origin/master
10:57:46.724: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
10:59:57.471: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/mt.js
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
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 --
[master 1af17eb] master 送出
 1 file changed, 3 insertions(+)
 create mode 100644 js/mt.js
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
Enumerating objects: 6, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/master:refs/heads/master 028955b..1af17eb
Done
11:01:22.987: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V2 --
Switched to branch 'V2'
Your branch is up to date with 'origin/V2'.
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
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 --
[V2 1b92782] V2版本第三次 送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V2:refs/heads/V2 ebc98fc..1b92782
Done
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
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 --
[V2 c9b931b] V2版本第四次 送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V2:refs/heads/V2 1b92782..c9b931b
Done
11:10:28.959: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
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
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 --
[master 435fa50] master,第二次送出
 1 file changed, 2 insertions(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/master:refs/heads/master 1af17eb..435fa50
Done
11:18:25.832: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout v1 --
Switched to branch 'v1'
Your branch is up to date with 'origin/v1'.
11:18:42.635: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
11:21:33.365: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout -b V3
Switched to a new branch 'V3'
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
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 --
[V3 fd68c04] 從master new Branch V3 分支,第一次送出,會複制目前到V3分支
 1 file changed, 1 deletion(-)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'V3' on GitHub by visiting:        
remote:      https://github.com/tonels/GitDemo/pull/new/V3        
remote: 
To https://github.com/tonels/GitDemo.git
* refs/heads/V3:refs/heads/V3 [new branch]
Branch 'V3' set up to track remote branch 'V3' from 'origin'.
Done
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
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 --
[V3 90c21fb] V3 分支,第二次送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V3:refs/heads/V3 fd68c04..90c21fb
Done
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
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 --
[V3 57af067] V3 分支,第二次送出 +1
 1 file changed, 1 insertion(+), 1 deletion(-)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V3:refs/heads/V3 90c21fb..57af067
Done
12:14:47.896: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout v1 --
Your branch is up to date with 'origin/v1'.
Switched to branch 'v1'
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
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 --
[v1 6834d65] V1版本第二次送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/v1:refs/heads/v1 df30d49..6834d65
Done
12:17:06.349: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V2 --
Your branch is up to date with 'origin/V2'.
Switched to branch 'V2'
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
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 --
[V2 49fab0d] V2版本第五次送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V2:refs/heads/V2 c9b931b..49fab0d
Done
12:18:23.563: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout V3 --
Switched to branch 'V3'
Your branch is up to date with 'origin/V3'.
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
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 --
[V3 99ae4e8] V3版本第三次送出
 1 file changed, 1 insertion(+)
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
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 --
[V3 f84cede] V3版本第四次送出
 1 file changed, 1 insertion(+)
12:27:43.810: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false cherry-pick -n 99ae4e8afc6463afcc8ab9ff5a2c0371e7fcc6a4
error: could not apply 99ae4e8... V3版本第三次送出
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
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
Enumerating objects: 11, done.
Delta compression using up to 8 threads
Total 8 (delta 3), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/V3:refs/heads/V3 57af067..f84cede
Done
12:32:52.743: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
error: you need to resolve your current index first
js/mt.js: needs merge
12:33:25.984: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout --ours -- js/mt.js
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
12:33:51.868: [GitDemo] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
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
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 --
[master 7ac9c02] master版本第三次送出
 1 file changed, 1 insertion(+)
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
Enumerating objects: 7, done.
Delta compression using up to 8 threads
Total 4 (delta 2), reused 0 (delta 0)
To https://github.com/tonels/GitDemo.git
  refs/heads/master:refs/heads/master 435fa50..7ac9c02
Done      

二、基本操作

首先明确,項目的初始化,基于何處,Github遠端端,還是基于本地IDEA初始化項目的

2.1、基于遠端端

這種情況,我們隻需git pull 即可,如果已存在多個分支,通過IDEA導入的時候,預設初始視窗加載的是master分支,其實還有幾個分支,可切換,判斷目前情況下合适的分支檢出,開發

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

2.2、基于本地初始化項目

這種情況是,我們隻需執行 git commit ,還有push指令

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

我們還要根據需要,檢出幾個開發的分支,最後開發完成時,還要合并分支

三、進階操作

應該注意,所有操作都要盡量基于commit,甚至push的,我這裡遇到一些問題,記錄參考第四條

3.1、合并兩個本地庫

這個操作,并不多…将本地 master-a 分支合并到本地 master 上,先切換到master 分支,然後操作,如果在master-a上已送出,合并時是不需要重新commit的,直接push 到遠端master即可

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等
關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

3.2、合并兩個遠端庫

這個操作可能比較多,最後開發完成後,最終所有的Dev分支都要合并到主分支上

首先先切換到目标分支上,執行以下的操作

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

因為基于遠端庫的合并,是以master分支隻是合并了 master-a 上已push的部分

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

3.3、加标簽

我們在本地 Commit 後,檢視VC Log中看到如下

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

标簽建好後,push 到遠端端,登入遠端端,即可看到标簽已推送

3.4、代碼復原

Git 的復原操作是指将代碼回退到某一曆史時刻

3.4.1、復原 - 對于未送出的,可直接 Revert

右鍵Revert,IDEA封裝Git指令控制台如下

git -c credential.helper= -c core.quotepath=false -c log.showSignature=false rm --cached -f -- queryDsl/DslDemo1/src/main/java/querydsl/controller/CityController.java
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 的復原操作

關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等
關于IDEA上Git的最佳實踐方式,合并、檢出、加标簽,復原等等

3.4.1、單檔案基于标簽復原

3.4.1、基于送出标簽復原

四、操作中的問題

4.1、當我試着在B分支上合并A上沒有commit的記錄時,發現那個檔案在B分支下(甚至任何本地分支下),也是可見的未送出的