写在前面

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

一、提交记录Tree

刚new的初始化项目 master,后来又在本地的不同分支,new Branch出V1,V2,V3分支,以及相关操作命令日志
master
V1 从master new Branch
V2 从 V1 new Branch
V3 从 master new Branch

以及对应所有的命令

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分支,其实还有几个分支,可切换,判断当前情况下合适的分支检出,开发

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命令控制台如下

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 的回滚操作

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

3.4.1、单文件基于标签回滚

3.4.1、基于提交标签回滚

四、操作中的问题

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