最近公司又来一批小伙伴,对git的使用非常陌生,我就安排给大家讲了下git的基本使用,今天也总结下发到博客园上和大家分享
一、git安装
由于公司都是用windows,本屌丝也是用windows,所有这里就只讲windows的安装
windows的安装非常简单
1、下载git:https://git-scm.com/download/win
2、双击后一路下一步即可
3、检查是否安装成功,鼠标右键,有下图所示证明安装成功

4、点击 git bash,查看当前安装的版本
$ git --version
git version 2.30.0.windows.2
到此为止git我们就安装完成
二、基本使用场景及命令
1、创建一个空的文件夹,然后进入该文件,打开 git bash

2、初始化git,输入命令:git init,此时我们就可以使用git 管理该文件夹

3、此时我们就需要介绍下git本地管理,一共有三大逻辑区域

我们一般就在工作区
a、git status:查看当前工作区的状态

此时我们新建一个文件,在执行git status命令,可以看到有个test1.txt的文件待提交

b、此时我们执行 git add命令,将工作区 的文件提交到暂存区

c、此时我们执行git commit -m "xxxxx",将暂存区 的文件提交到版本库区
git commit -m "git第一次提交"

然后我们可以执行 git log查看提交记录
git log

然后我们一般会给这次提交打一个tag,执行 git tag -a 版本号 -m 版本描述
git tags -a v0.1 -m "第一个版本"

此时我们就把最主流的一条路走完

4、前面我们讲了文件如何从左向右移动,这一节我们讲下文件如何从右向左移动
为了让大家看的i清楚,我先修改下文件,然后多提交几次变更到版本库区域

当前的文件处于第四个版本的状态

a、首先从第四个版本直接回到第一个版本的工作区已经管理的状态
git reset --hard 745cf9a2ec997cf9fca7d9d0f0f655807438eee2

b、下面我们演示git checkout功能,从变更区 回到已管理里区
$ git checkout test1.txt
此时我们在修改文件


此时要注意,我们git log会查不到所有的提交记录,此时查看记录我们需要用git reflog命令
$ git reflog

大家观察下,发现这2个命令的区别么
git log是查看当前提交记录之前的提交记录
git reflog是查看当前提交记录之后的提交记录
从右往左的文件流转最常用的命令就讲完了,我们更新下我们的逻辑图

c、下面我们讲下从右往左一些不常用的命令
从版本库区域 回到暂存区
git reset --soft 745cf9a2ec997cf9fca7d9d0f0f655807438eee2
从暂存区 回到修改过的文件或者新增文件的状态
git reset HEAD test1.txt


此时在执行git checkout 回到已经管理的 状态

从版本库回到新增文件或者修改文件文件状态,同样在执行git checkout 可以回到已管理文件区域
git reset --mix 745cf9a2ec997cf9fca7d9d0f0f655807438eee2

最终,我们把整个文件流转的图更新下

5、讲下git分支
正常情况下,master分支是我们的线上分支,我们实际开发需要在dev分支上开发
a、查看当前分支: git branch

b、创建分支、切换分支、删除分支
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (master)
$ git branch dev
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (master)
$ git checkout dev
Switched to branch 'dev'
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git branch
* dev
master
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git branch -d dev
error: Cannot delete branch 'dev' checked out at 'D:/test'
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git branch
* dev
master

c、在分支上提交代码
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git branch
* dev
master
//切换到dev分支
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git status
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test1.txt
//修改文件
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git add .
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git commit -m "dev分支第一次提交"
[dev be36c1a] dev分支第一次提交
1 file changed, 3 insertions(+), 1 deletion(-)
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git tag -a v0.0.1 -m "dev第一个版本"
admin@LAPTOP-SDS5GNTD MINGW64 /d/test (dev)
$ git log
commit be36c1aba0d7cbaf7d962f54d24a05ab91cf6889 (HEAD -> dev, tag: v0.0.1)
Author: cuiyuerong <cuiyuerong@126.com>
Date: Thu Oct 12 11:52:18 2023 +0800
dev分支第一次提交
//在dev分支提交代码

d、此时我们切换回master分支,查看文件的状态
dev分支的文件状态

master分支的文件状态

e、如果此时,我们dev分支的功能开发完成了,需要合并到maser分支,让线上使用
此时我们用 git merge命令
先切换到master分支,然后 执行 git merge dev ,合并dev分支到master分支

f、正常情况下,dev分支的代码已经合并到master分支了,dev分支就没有存在的必要了,我们需要把他删除掉
$ git branch -d dev
Deleted branch dev (was be36c1a)
三、结合gitlab
1、如何更新本地代码到gitlab
git remote add origin 你的仓库地址
git push origin master
git remote add origin xxxxx 的意思是,以后origin就代指远程仓库的地址,以后就不用写那么长的远程仓库的地址了,也就是给远程仓库起别名,这里不一定要用origin,可以用任何名字
git push origin master的意思是,推送本地master分支的代码到远程仓库,也就是origin代指的仓库
2、首次如何拉取远程仓库代码到本地
git clone 远程仓库地址
3、后续如何更新本地的代码和远程一致
git pull origin dev
拉取远程仓库的dev分支代码到本地
4、gitlab如何配置免密登录
a、http的方式
git remote add origin https://用户名:密码@仓库地址
git push origin master
b、ssh的方式
本地生成公钥和私钥
$ ssh-keygen.exe

c、然后拷贝公钥到github的ssh密钥的地方

d、本地用ssh的方式配置远程仓库地址即可
git add remote origin git@仓库地址
四、解决冲突
我们在合并merge经常会有文件冲突,此时我们只能手动解决冲突。此时我们借助软件beyond compare来解决
我们可以做如下配置

配置:--local的意思是只对当前的项目生效;
git config --local mergetool.keepbackup false的意思 解决完成冲突后的旧文件不要做备份
如果出现冲突,则调用git mergetool 就可以开始解决冲突
五、gitignore文件
配置忽略文件,常用的语法如下

