Git使用心得体会
本博客参考文献 https://mp.weixin.qq.com/s/Km5KuXPETvG0wCGHrvj9Vg
一、Git的简单介绍:
Git是一种分布式版本控制系统。Git的数据不止保存在服务器上,同时也完整的保存在本地计算机上,这种特性带来许多便利,比如你可以在完全离线的情况下使用Git,随时随地提交项目更新,而且你不必为单点故障过分担心,即使服务器宕机或数据损毁,也可以用任何一个节点上的数据恢复项目,因为每一个开发节点都保存着完整的项目文件镜像。
二、Git的基本模型与操作:

在git中有一个本地的存储库Repository以及一个远程的存储库Remote,用户可以将代码从本地的WorkSpace保存到Repository或者Remote以达到版本控制或团队协作开发得目的。
git常用操作及说明如下:
1 PS D:\WorkSpace\git> git --help 2 usage: git [--version] [--help] [-C <path>] [-c <name>=<value>] 3 [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] 4 [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare] 5 [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] 6 <command> [<args>] 7 8 These are common Git commands used in various situations: 9 10 start a working area (see also: git help tutorial) 11 clone Clone a repository into a new directory 12 init Create an empty Git repository or reinitialize an existing one 13 14 work on the current change (see also: git help everyday) 15 add Add file contents to the index 16 mv Move or rename a file, a directory, or a symlink 17 restore Restore working tree files 18 rm Remove files from the working tree and from the index 19 sparse-checkout Initialize and modify the sparse-checkout 20 21 examine the history and state (see also: git help revisions) 22 bisect Use binary search to find the commit that introduced a bug 23 diff Show changes between commits, commit and working tree, etc 24 grep Print lines matching a pattern 25 log Show commit logs 26 show Show various types of objects 27 status Show the working tree status 28 29 grow, mark and tweak your common history 30 branch List, create, or delete branches 31 commit Record changes to the repository 32 merge Join two or more development histories together 33 rebase Reapply commits on top of another base tip 34 reset Reset current HEAD to the specified state 35 switch Switch branches 36 tag Create, list, delete or verify a tag object signed with GPG 37 38 collaborate (see also: git help workflows) 39 fetch Download objects and refs from another repository 40 pull Fetch from and integrate with another repository or a local branch 41 push Update remote refs along with associated objects
三、Git实际操作
1、Git的安装及初始化本地库
在有GUI的操作系统下可以前往Git官网下载git , 官网链接 :https://git-scm.com/downloads 。
Linux/Unix可以在命令行下可以使用一下命令进行下载:
1 sudo apt install git
安装完成后,可以使用git init命令新建一个本地库或者git clone https://DOMAIN_NAME/YOUR_NAME/REPO_NAME.git 命令将一个远程库克隆到本地。
使用git status命令查看本地库状态。

说明本地库已建立完毕,可以开始后续的操作。
2、场景一:Git本地库的基本用法
目的:在本地库进行对代码的版本控制。
首先使用上述的方法创建一个本地库。
然后使用 git add [FILES] 命令将gitTest.txt文件添加到暂存区(Index)。
再使用 git commit -m "this is a test” 命令将暂存区中的文件添加到本地库(Repository)。
使用 git log指令查看提交的状态。

再使用上述方法提交一个。

当需要版本切换时可以使用 git reset --hard HEAD^^/HEAD~100/commit-id/commit-id的头几个字符 命令进行切换。
HEAD类似于一个指针,指向当前版本。 HEAD^^表示回退两个版本,依次类推。HEAD~100表示回退一百个版本。commit-id表示直接切换到那个版本。

可以使用 git reflog指令查看当前HEAD之后的提交记录,便于回到未来。再配合上述的 git reset --hard commit-id指令就可以切换到之后的版本。

3、场景二:Git远程版本库的基本用法
我们首先在github上创建了一个Test项目 , 并生成一个README.md文件。

之后使用git clone https://DOMAIN_NAME/YOUR_NAME/REPO_NAME.git命令将项目拷贝下来。

在VS CODE中打开README.md文件。

加入一点新内容然后保存。

使用之前的命令添加到本地库,然后使用git push命令保存到远程库。


在登录到github之后,查看README.md文件。

远程修改完成。
使用git remote -v指令可以看到详细的远程库信息以及fetch与push的URL。

一些其他可能用到的指令:
git clone命令官方的解释是“Clone a repository into a new directory”,即克隆一个存储库到一个新的目录下。
git fetch命令官方的解释是“Download objects and refs from another repository”,即下载一个远程存储库数据对象等信息到本地存储库。
git push命令官方的解释是“Update remote refs along with associated objects”,即将本地存储库的相关数据对象更新到远程存储库。
git merge命令官方的解释是“Join two or more development histories together”,即合并两个或多个开发历史记录。
git pull命令官方的解释是“Fetch from and integrate with another repository or a local branch”,即从其他存储库或分支抓取并合并到当前存储库的当前分支。
4、场景三:团队项目中的分叉合并。
使用git checkout -b newbranch指令创建一个新的分支。

使用git branch指令查看分支。

*表示当前工作的分支。
使用git checkout指令可以切换分支。

假如要将newbranch合并到main,那么首先确保当期工作区处于main分支,可以使用git checkout main命令切换到main分支。
然后使用git merge newbranch指令进行合并,默认的是快进式合并。
也可使用git merge --no-ff newbranch指令进行非快进式合并。
快进式合并会将两个分支合并到同一个时间线上,非快进式合并不会合并到同一个时间线上,而是在合并分支上生成一个新节点。


上图是快进式,下图是非快进式。
切换到newbranch分支。

在该分支进行多次修改README.md并进行提交。

最后,先切换回main分支,将远程origin/main同步最新到本地存储库,再合并newbranch到main分支,推送到远程origin/main之后即完成了一项开发工作。

查看network graph

5、场景四:git rebase
开发者要把自己的代码修改按照功能拆分成一个个相对独立的提交,一个提交对应一个功能点,而且要在对应的 commit log message 里面描述清楚。因此在合并和 push 之前检查修改一下 commit 记录时常需要。这时我们就需要git rebase指令。
git rebase指令如下:git rebase -i [startpoint] [endpoint] 。其中[endpoint]可以缺省 , 默认值是当前HEAD指向的记录。
可以使用git rebase --abort 指令撤销当前的rebase指令。
分别三次修改文件然后进行提交。


之后rebase成一个commit。使用git rebase -i HEAD^^^命令。

如果我们编辑删除了B版本,可以用VS Code打开冲突文件,根据提示选择保留哪个更改,也可以直接编辑文件去掉提示信息。


解决冲突后需要将修改后的文件存入暂存区(git add),最后执行git rebase --continue命令完成git rebase。

这时查看提交日志可以发现B版本已经不存在了。替代的是The Final版本。
最后切换回main分支进行合并即可。

最后结果


浙公网安备 33010602011771号