参考: Git 原理入门 - 阮一峰的网络日志 (ruanyifeng.com)
示例一:创建仓库 初始化仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 PS ~> cd .\aliceWorkspace\PS ~\aliceWorkspace> git initInitialized empty Git repository in E:/个人文件归档/笔记-博客文档/软件学习/git/demoforgit/aliceWorkspace/.git/ PS ~\aliceWorkspace> tree .\.git\├─hooks ├─info ├─objects │ ├─info │ └─pack └─refs ├─heads └─tags PS ~\aliceWorkspace> git statusOn branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
配置仓库参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [core ] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true PS ~\aliceWorkspace> git remote add origin git@github.com:WilsonGoGo/gitLearning.git [remote "origin" ] url = git@github.com:WilsonGoGo/gitLearning.git fetch = +refs/heads/*:refs/remotes/origin/* PS ~\aliceWorkspace> git config user.name "WilsonGoGo" PS ~\aliceWorkspace> git config user.email "wilson6174wujunqi@outlook.com" [user ] name = WilsonGoGo email = wilson6174wujunqi@outlook.com
示例二:简单提交 修改工作区
1 2 3 PS ~\aliceWorkspace> echo "repo inited" > README.md
1 2 3 4 5 6 7 8 9 10 11 12 PS ~\aliceWorkspace> git statusOn branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) README.md ~\aliceWorkspace> tree .\.git\objects\ ├─info └─pack
提交暂存区
1 2 3 PS ~\aliceWorkspace> git add .\README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 PS ~\aliceWorkspace> git statusOn branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─info └─pack PS ~\aliceWorkspace> git cat -file -t 01 cc blob PS ~\aliceWorkspace> git cat -file -p 01 cc ÿþrepo inited
移除暂存区
1 2 3 4 5 6 7 8 9 PS ~\aliceWorkspace> git rm .\README.md error: the following file has changes staged in the index: README.md (use --cached to keep the file, or -f to force removal) PS ~\aliceWorkspace> git rm --cached .\README.mdrm 'README.md'
1 2 3 4 5 6 7 8 9 10 11 12 13 PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─info └─pack PS ~\aliceWorkspace> git statusOn branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) README.md
再次修改
1 2 3 4 5 PS ~\aliceWorkspace> echo "repo inited --wilson" > .\README.mdPS ~\aliceWorkspace> git add .\README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 PS ~\aliceWorkspace> git statusOn branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─74 /70 c63fd32dc148b1c9cf317c5fbd2a9616b492 ├─info └─pack PS ~\aliceWorkspace> git cat -file -p 7470 ÿþrepo inited --wilson PS ~\aliceWorkspace> git gc Enumerating objects: 1 , done. Counting objects: 100 % (1 /1 ), done. Writing objects: 100 % (1 /1 ), done. Total 1 (delta 0 ), reused 0 (delta 0 ), pack-reused 0 PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─info └─pack
使用commit进行提交
1 2 3 4 5 6 PS ~\aliceWorkspace> git commit -m "first commit : init README.md" [master (root -commit ) becd906 ] first commit : init README.md 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 PS ~\aliceWorkspace> git statusOn branch master nothing to commit, working tree clean PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─9 d/7 e540963b86926d49c605df45e40ee64c3aca5 ├─be/cd90604bc3786383cc3dec2b58befc221743ef ├─info └─pack PS ~\aliceWorkspace> git cat -file -t 9 d7e tree PS ~\aliceWorkspace> git cat -file -p 9 d7e 100644 blob 7470 c63fd32dc148b1c9cf317c5fbd2a9616b492 README.mdPS ~\aliceWorkspace> git cat -file -t becd commit PS ~\aliceWorkspace> git cat -file -p becd tree 9 d7e540963b86926d49c605df45e40ee64c3aca5 author WilsonGoGo <wilson6174wujunqi@outlook.com> 1663302147 +0800 committer WilsonGoGo <wilson6174wujunqi@outlook.com> 1663302147 +0800 first commit : init README.md
示例三:分支管理 一般情况下,开发一个项目时,我们需要保证主分支上的版本都是可用的,然后基于可用的主分支去新建一个dev分支用于测试开发版本
1 2 3 4 5 6 7 8 9 10 11 12 13 PS ~\aliceWorkspace> git branch dev PS ~\aliceWorkspace> git branch dev * master PS ~\aliceWorkspace> git switch dev Switched to branch 'dev' PS ~\aliceWorkspace> git branch* dev master
进行开发,这里模拟创建task.doc文件记录开发要求(即记录各开发员信息)
1 2 PS ~\aliceWorkspace> new-item task.docPS ~\aliceWorkspace> echo "record everyone's name" > .\task.doc
开发结束,进行commit来更新dev分支指向的快照
1 2 3 4 5 6 7 8 PS ~\aliceWorkspace> git add .PS ~\aliceWorkspace> git commit -m "dev: init task.doc" [dev f712190 ] dev: init task.doc 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 task.doc
通过可视化界面(来自VsCode的gitLens插件),检查分支图:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 PS ~\aliceWorkspace> tree .\.git\objects\ ├─01 /ccc646d113420d128301639686e16799ece787 ├─78 /c2277b22f2c1e22a4d29fbe4cc443dd6f267ad ├─8 a/ca54ef6e8b187c21516723fe4e2c16f4104e1b ├─9 d/7 e540963b86926d49c605df45e40ee64c3aca5 ├─be/cd90604bc3786383cc3dec2b58befc221743ef ├─f7/12190 f68a943af3cdf3a911327a51d188f7d9a ├─info └─pack PS ~\aliceWorkspace> git cat -file -t 78 c2 blob PS ~\aliceWorkspace> git cat -file -p 78 c2 ÿþrecord everyone's name # 8aca... tree类型 是包含task.doc的快照根目录 PS ~\aliceWorkspace> git cat-file -t 8aca tree PS ~\aliceWorkspace> git cat-file -p 8aca 100644 blob 7470c63fd32dc148b1c9cf317c5fbd2a9616b492 README.md 100644 blob 78c2277b22f2c1e22a4d29fbe4cc443dd6f267ad task.doc # f712... commit类型 对应dev分支上刚刚的commit操作 指向8aca...tree类型 PS ~\aliceWorkspace> git cat-file -t f712 commit PS ~\aliceWorkspace> git cat-file -p f712 tree 8aca54ef6e8b187c21516723fe4e2c16f4104e1b parent becd90604bc3786383cc3dec2b58befc221743ef author WilsonGoGo <wilson6174wujunqi@outlook.com> 1663304672 +0800 committer WilsonGoGo <wilson6174wujunqi@outlook.com> 1663304672 +0800 dev: init task.doc ## 检查指针 ## # 检查HEAD指针 # # HEAD指针当前值:HEAD指针目前指向dev分支,与dev分支指针同步 PS ~\aliceWorkspace> cat .\.git\HEAD ref: refs/heads/dev # HEAD指针历史记录 PS ~\aliceWorkspace> cat .\.git\logs\HEAD 0000000000000000000000000000000000000000 becd90604bc3786383cc3dec2b58befc221743ef WilsonGoGo <wilson6174wujunqi@outlook.com> 1663302147 +0800 commit (initial): first commit : init README.md # 这条记录表示HEAD指针从git初始化时的00...变动至第一次commit(提交了README.md)的becd...快照 becd90604bc3786383cc3dec2b58befc221743ef becd90604bc3786383cc3dec2b58befc221743ef WilsonGoGo <wilson6174wujunqi@outlook.com> 1663303934 +0800 checkout: moving from master to dev # 这条记录表示HEAD指针从master分支移动到dev分支,但是这两个分支目前指向的同一个commit对象becd...,即指向同一个项目快照 becd90604bc3786383cc3dec2b58befc221743ef f712190f68a943af3cdf3a911327a51d188f7d9a WilsonGoGo <wilson6174wujunqi@outlook.com> 1663304672 +0800 commit: dev: init task.doc # 这条记录表示HEAD指针从第一次commit的快照becd...指向了第二次commit(提交了task.doc)的快照f714... # 检查各分支指针 # # master分支 # master指针值仍指向刚提交完README.md时提交的commit类型 PS ~\aliceWorkspace> cat .\.git\refs\heads\master becd90604bc3786383cc3dec2b58befc221743ef # master指针历史记录 PS ~\aliceWorkspace> cat .\.git\logs\refs\heads\master 0000000000000000000000000000000000000000 becd90604bc3786383cc3dec2b58befc221743ef WilsonGoGo <wilson6174wujunqi@outlook.com> 1663302147 +0800 commit (initial): first commit : init README.md # 该条表示master指针从初始化0000...指向了becd...(首次commit,即README.md的提交) # dev分支 # dev指针值已经指向刚刚提交了task.doc后的commit类型 PS ~\aliceWorkspace> cat .\.git\refs\heads\dev f712190f68a943af3cdf3a911327a51d188f7d9a # dev指针历史记录 PS ~\aliceWorkspace> cat .\.git\logs\refs\heads\dev 0000000000000000000000000000000000000000 becd90604bc3786383cc3dec2b58befc221743ef WilsonGoGo <wilson6174wujunqi@outlook.com> 1663303857 +0800 branch: Created from master # 这条记录表示dev分支从master分支上创建而来 becd90604bc3786383cc3dec2b58befc221743ef f712190f68a943af3cdf3a911327a51d188f7d9a WilsonGoGo <wilson6174wujunqi@outlook.com> 1663304672 +0800 commit: dev: init task.doc # 这条记录表示dev指针从becd...(首次commit,即README.md的提交)指向了f712...(第二次commit,即task.doc的提交)
由于此时,master分支与main分支指向的快照上有所不同,尝试切换分支观察工作区对应文件的变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 PS ~\aliceWorkspace> git switch master Switched to branch 'main' PS ~\aliceWorkspace> ls Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2022 /9 /16 12 :13 46 README.mdPS ~\aliceWorkspace> git switch dev Switched to branch 'dev' PS ~\aliceWorkspace> ls Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2022 /9 /16 12 :13 46 README.md-a---- 2022 /9 /16 14 :43 50 task.doc
可以看出,当分支切换时,工作区对应的文件也会被更改至快照状态
示例三:简单提交 完成前两个示例后,alice希望将仓库同步到远程仓库,这样其他开发员就可以一起对项目进行开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 PS ~\aliceWorkspace> git switch masterPS ~\aliceWorkspace> git push origin master:main Enumerating objects: 3 , done. Counting objects: 100 % (3 /3 ), done. Writing objects: 100 % (3 /3 ), 275 bytes | 275.00 KiB/s, done. Total 3 (delta 0 ), reused 1 (delta 0 ), pack-reused 0 To github.com:WilsonGoGo/gitLearning.git * [new branch ] master -> main PS ~\aliceWorkspace> git push --all origin Enumerating objects: 4 , done. Counting objects: 100 % (4 /4 ), done. Delta compression using up to 8 threads Compressing objects: 100 % (3 /3 ), done. Writing objects: 100 % (3 /3 ), 333 bytes | 333.00 KiB/s, done. Total 3 (delta 0 ), reused 0 (delta 0 ), pack-reused 0 To github.com:WilsonGoGo/gitLearning.git * [new branch ] dev -> dev * [new branch ] master -> master PS ~\aliceWorkspace> git branch -m master mainPS ~\aliceWorkspace> git branch dev * main PS ~\aliceWorkspace> git push origin :masterTo github.com:WilsonGoGo/gitLearning.git - [deleted ] master
注:这里出现一个问题,就是本地git init的默认分支是master,github建立仓库的默认分支现在已经改为main,在上述操作中因为默认分支名不同,导致了github上有master和main的两个重复分支,以下给出几种方案来避免这个问题:
在本地初始化仓库时,使用git config --global init.defaultBranch main将本地git仓库默认分支命名为main
在github创建仓库页面,通过setting选项修改仓库默认分支为master
示例四:拉取仓库 现在程序员bob加入项目的开发,他需要把仓库clone至本地来加入开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 PS ~\bobWorkspace> git clone git@github.com:WilsonGoGo/gitLearning.gitCloning into 'gitLearning' ... remote: Enumerating objects: 6 , done. remote: Counting objects: 100 % (6 /6 ), done. remote: Compressing objects: 100 % (4 /4 ), done. remote: Total 6 (delta 0 ), reused 6 (delta 0 ), pack-reused 0 Receiving objects: 100 % (6 /6 ), done. PS ~\bobWorkspace\gitLearning> git branch* main PS ~\bobWorkspace\gitLearning> git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/dev remotes/origin/main PS ~\bobWorkspace\gitLearning> git checkout -t origin/devSwitched to a new branch 'dev' branch 'dev' set up to track 'origin/dev' .
现在bob程序员可以在本地对dev进行开发了
示例五:差异比较 切回alice视角,继续对dev分支进行开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 PS ~\aliceWorkspace> new-item nameList.txtPS ~\aliceWorkspace> mkdir aliceInfoPS ~\aliceWorkspace> cd .\aliceInfo\PS ~\aliceWorkspace\aliceInfo> new-item workList.txtPS ~\aliceWorkspace> git add nameList.txtPS ~\aliceWorkspace> git branch alicePS ~\aliceWorkspace> git switch alicePS ~\aliceWorkspace> git statusChanges to be committed: (use "git restore --staged <file>..." to unstage) new file: nameList.txt Untracked files: (use "git add <file>..." to include in what will be committed) aliceInfo/ PS ~\aliceWorkspace> git switch devPS ~\aliceWorkspace> git statusChanges to be committed: (use "git restore --staged <file>..." to unstage) new file: nameList.txt Untracked files: (use "git add <file>..." to include in what will be committed) aliceInfo/
继续开发
1 2 PS ~\aliceWorkspace> echo "alice" > nameList.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 PS ~\aliceWorkspace> git diff diff --git a/nameList.txt b/nameList.txtindex e69de29..12381 d8 100644 Binary files a/nameList.txt and b/nameList.txt differ PS ~\aliceWorkspace> git add nameList.txtPS ~\aliceWorkspace> git diff PS ~\aliceWorkspace> git diff HEAD diff --git a/nameList.txt b/nameList.txtnew file mode 100644 index 0000000 ..12381 d8 Binary files /dev/null and b/nameList.txt differ
提交对nameList.txt的更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 PS ~\aliceWorkspace> git commit -m "init: nameList.txt" [alice 6 dffd65 ] init: nameList.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 nameList.txt PS ~\aliceWorkspace> git push origin --all Enumerating objects: 8 , done. Counting objects: 100 % (8 /8 ), done. Delta compression using up to 8 threads Compressing objects: 100 % (5 /5 ), done. Writing objects: 100 % (7 /7 ), 691 bytes | 691.00 KiB/s, done. Total 7 (delta 1 ), reused 0 (delta 0 ), pack-reused 0 remote: Resolving deltas: 100 % (1 /1 ), done. remote: remote: Create a pull request for 'alice' on GitHub by visiting: remote: https://github.com/WilsonGoGo/gitLearning/pull/new/alice remote: To github.com:WilsonGoGo/gitLearning.git * [new branch ] alice -> alice