目录
Git使用
使用git的好处:
使用git可以方便地进行版本管理和团队协作,并且可以轻松地回退和重构代码,保证代码的可维护性和稳定性。同时,git也可以帮助我们更好地管理代码的分支和合并,提高开发效率。
使用gitlab的好处:
使用GitLab的好处包括(但不限于):
-
代码托管和版本管理:可以在GitLab上存储和管理您的代码,并轻松地跟踪代码的历史记录和更改。
-
持续集成/持续交付:GitLab提供了强大的CI / CD功能,可以自动化测试、构建和部署您的应用程序,从而提高开发效率。
-
协作和沟通:在GitLab上可以与团队成员进行协作和沟通,管理问题和任务等。
-
安全性:GitLab提供了强大的安全性功能,可以帮助您保护您的代码和应用程序。 总之,使用GitLab可以使开发和部署过程更加轻松、高效和安全。
1.git版本号
-
有40位,通过SHA-1算法进行加密。
-
其中前两位用来标识文件夹,后38位用来标识文件
-
-
怎样通过版本号确定到对应内容的文件呢?
1.在本地仓库的文件主目录下,通过Git Bash Here打开命令界面,进行操作。
-
git cat-file -p 412e68eb53477add1ae1caaca65193a8e5f893a0
2.输入tree 后面的 版本号6f9509c88bed7080d496fc5e1d87a9315e30549d
3.输入文件块对象后的版本号dfe0770424b2a19faf507a501ebfc23be8f54e7b,得到文件内容
举例总结三步:
2.git版本号(分支操作)
head文件目录:
当切换当前仓库的分支为user时,head文件存储的文件(版本号)路径也会随之发生改变。
3.git的常用命令
git中的几个区:
工作区:也就是本地文件的区域
版本库中暂存区:就是使用git add命令之后,本地工作区的文件加到暂存区
版本库当前分支:也就是使用 git commit 之后,暂存区的东西到版本库当前分支。
3.1 仓库操作
-
//查看git版本 $ git -v git version 2.40.0.windows.1
-
//初始化git仓库 $ git init Initialized empty Git repository in C:/Users/杨理/Documents/GitHub/test/local-rep-1/.git/
-
//克隆远程仓库 $ git clone https://gitee.com/phdvb/test-gitee-project.git remote-git-rep Cloning into 'remote-git-rep'... remote: Enumerating objects: 12, done. remote: Counting objects: 100% (12/12), done. remote: Compressing objects: 100% (4/4), done. remote: Total 12 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (12/12), done.
-
//配置仓库用户名 $ git config user.name yangli
-
//配置仓库用户邮箱 $ git config user.email 1260959685@qq,com
3.2 文件操作
1.//查看暂存区的状态
//查看暂存区的状态
$ git status
2.//将a.txt放入缓存区作比较
//将a.txt放入缓存区作比较
$ git add a.txt
3.//清除缓冲区的a.txt文件
//清除缓冲区的a.txt文件
$ git rm --cached a.txt
4.//将暂存区内的文件放入到存储区域
$ git commit -m 新增文件
[master 526c9fe] 新增文件
1 file changed, 1 deletion(-)
delete mode 100644 a.txt
再次查看:工作区没有任何内容
$ git status
On branch master
nothing to commit, working tree clean
//6.输出打印日志
$ git log
commit 6c9f384776ff2e8b54331fb5245b0b9feaf067b1 (HEAD -> master)
Author: yangli <1260959685@qq.com>
Date: Mon Apr 10 17:27:50 2023 +0800
新增文件b
commit d784ed438fdcd8d8729c71244396c696a15bfaed
Author: yangli <1260959685@qq.com>
Date: Mon Apr 10 17:23:22 2023 +0800
Create a.txt
commit 526c9fe347b8186ab993cc5290932a5be973645f
Author: yangli <1260959685@qq.com>
Date: Mon Apr 10 17:21:32 2023 +0800
新增文件
以一行的方式输入操作信息
总结
误删除
将工作区的abc.txt文件删除
可以通过git restore abc.txt进行恢复
git restore abc.txt
-
前提是误删以后,不能提交$ git add abc.txt 和$ git commit -m abc文件,这样的话会通过与暂存区做对比,清除掉存储区域中的文件。之后就不能通过git restore abc.txt恢复了。
//将a.txt放入缓存区作比较
$ git add a.txt
//清除缓冲区的a.txt文件
$ git rm --cached a.txt
//可以通过还原到上一个版本20eb1ef的方式恢复
$ git revert e902d5e
hint: Waiting for your editor to close the file... "C:\Desktop\安装包\Microsoft VS Code\bin\code" --wait: line 1: C:\Desktop\安装包\Microsoft VS Code\bin\code: No such file or directory
error: There was a problem with the editor '"C:\Desktop\安装包\Microsoft VS Code\bin\code" --wait'.
Please supply the message using either -m or -F option.
3.3 分支操作
1.新建仓库user分支
$ git branch user
2.查看仓库分支
$ git branch -v
* master 4e67542 aaaa //*表示当前分支是master
user 4e67542 aaaa
3.切换当前仓库分支
$ git checkout user
Switched to branch 'user'
合并与冲突
1.合并两个分支结构
$ git merge order
2.打开合并冲突文件,手动更改选择保留信息
3.再次将c.txt文件加入暂存区,接着将暂存区的c.txt放入存储区域。
3.4 标签操作
1.可以给每一次的操作标记一个别名
$ git tag upfile 2f7755d8bc0126b5397ed4ab3df62330926a596e
2.删除指定的标签文件
$ git tag -d upfile
3.5 远程仓库
修改远程仓库地址
//ssh地址(同步的时候需要安全验证功能)
$ git remote add orgin git@gitee.com:phdvb/test-gitee-project.git
相当于在config文件中修改了访问地址
将本地仓库中的文件同步到远程仓库
$ git push origin
生成rsa公钥文件
$ ssh-keygen -t rsa -C git@gitee.com:phdvb/test-gitee-project.git
公钥存放位置
在代码托管平台进行添加验证
成功同步到远程仓库:
远程仓库修改后,同步到本地仓库:
$git pull origin