git_笔记

git

学习视频

【【狂神说Java】Git最新教程通俗易懂】 https://www.bilibili.com/video/BV1FE411P7B3/?p=10&share_source=copy_web&vd_source=271b4386fd06dbbfd6504cf65bb85f31

git的下载与安装

git官网: https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git
在这里插入图片描述

git镜像网址: https://registry.npmmirror.com/binary.html?path=git-for-windows/

在这里插入图片描述

建议下载 .exe安装包直接无脑下一步下载

启动git

Git Bash:Unix与Linux风格的命令行,使用的最多,推荐

Git CMD:windows风格的命令行

Git GUI:图形界面的Git,不建议使用

在这里插入图片描述

git中的Linux基本命令

cd		改变目录
cd..	回退到上一个目录,直接cd进入默认目录
pwd		显示当前所在的目录路径
ls		列出当前目录中的所有文件
touch	新建一个文件,如touch index.js就会在当前目录下新建一个index.js文件
rm		删除一个文件,如rm index.js就会把当前目录下的index.js文件删除
mkdir	新建一个目录,即新建一个文件夹
rm -r	删除一个文件夹,rm -r src 删除src目录
mv		移动文件,mv index.html src index.html是要移动的文件,src是目标文件夹
reset	重新初始化终端/清屏
clear	清屏
history 查看命令历史
help	帮助
table	自动补全命令
#		表示注释

git配置

所有的配置文件都保存在本地,一般都保存在C盘

查看配置

git config -l

查看全局配置

git config --global --list

D:\VSCode\Microsoft VS Code\bin\code 则是配置文件的目录

显示的是用户名和email不显示密码

在这里插入图片描述

设置用户名与邮箱(用户表示,非常必要)

在Git Bash命令窗口中设置

git config --global user.name "JiaQi"	#"填写名字"
git config --global user.email 2900209219@qq.com	#填写邮箱

在gitee上创建一个项目

示例 https://blog.csdn.net/qq_46311811/article/details/122264266

SSH 公钥设置

官网示例 https://help.gitee.com/base/account/SSH%E5%85%AC%E9%92%A5%E8%AE%BE%E7%BD%AE

git连接Gitee(码云)的环境配置及使用方法

CSDN网友示例 https://blog.csdn.net/weixin_43736652/article/details/116028009

1.生成公钥

# 进入 c:\Users\Administrator\.ssh 目录
# 在git中生成公钥 #-t rsa 加密算法,具体步骤略
ssh-keygen -t rsa

2.注册到Gitee

在这里插入图片描述
在这里插入图片描述

3.Gitee上新建一个仓库

在这里插入图片描述

许可证:开源是否可以随意转载,开源但不能商用,不能转载,相当于搞一堆限制

git基本理论(核心)

工作区

git本地有三个工作区域:工作目录(Working Directory)、暂存区(Stage/Index)、资源库(Repository或Git Directory)。如果在加上远程的git仓库(Remote Directory)就可以分为四个工作区域。文件在这四个区域之间的转换关系如下:

在这里插入图片描述

  • Workspace:工作区,就是平时存放项目代码的地方
  • Index/Stage:暂存区,用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息
  • Repository:仓库区(或本地仓库),就是安全存放数据的位置,这里有你提交所有版本的数据。其中HEAD指向最新放入仓库的版本
  • Remote:远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换

本地的三个区域确切的说应该是git仓库中HEAD指向的版本:

在这里插入图片描述

  • Directory:使用git管理一个目录,也就是一个仓库,包含我们的工作空间和Git的管理空间
  • WorkSpace:需要通过git进行版本控制的目录和文件,这些目录和文件组成了工作空间
  • .git:存放git管理信息的目录,初始化仓库的时候自动创建
  • Index/Stage:暂存区,在提交进入Local Repo之前,我们可以把所有的更新放在暂存区
  • Local Repo:本地仓库,一个存放在本地的版本库;HEAD会只是当前的开发分支(branch)
  • Stash:隐藏,是一个工作状态保存站,用于保存、恢复WorkSpace中的临时状态

工作流程

git的工作流程一般是这样的

1、在工作目录中添加、修改文件;

2、将需要进行版本管理的文件放入暂存区域;

3、将暂存区的文件提交到git仓库

因此,git管理的文件有三种状态:已修改(modified),已暂存(staged),已提交(committed)

git项目搭建

创建工作目录与常用指令

工作目录(WorkSpace)一般就是你希望git帮助你管理的文件夹,可以是项目目录,也可以是一个空目录,建议不要有中文

日常使用只要记住下图的6个命令

在这里插入图片描述

创建git代码库

1、创建全新的仓库,需要git管理的项目的根目录执行:

#在当前目录新建一个git代码库,
$ git init

.git文件夹是一个隐藏目录

在这里插入图片描述

克隆远程仓库

2、另一种方式是克隆远程目录,由于是将远程服务器(gitee)上的仓库完全镜像一份本地

#克隆一个项目和他的整个代码历史(版本信息)
$ git clone [url]

例如下列命令直接克隆git@gitee.com:cxyax/WeChatComputer-roomMaintenance.git的项目到~/Desktop/git-test文件夹中

在这里插入图片描述
在这里插入图片描述

git文件操作

文件4中操作

  • Untracked:未跟踪,此文件在文件夹中,但并没有加入到git库,不参与版本控制。通过git add状态变为staged
  • Unmodify:文件已经入库,未修改,即版本库中的文件快照内容与文件夹中完全一致。这种类型的文件有两种去处,如果它被修改,而改变为Modified。如果使用git rm移除版本库,则成为Untracked文件
  • Modified:文件已修改,仅仅是修改,并没进行其他的操作,这个文件也有两个去处,通过git add可进入暂存staged状态,使用git checkout则丢弃修改过,返回到unmodify状态,这个 git checkout即从库中取出文件,覆盖当前修改!
  • Staged:暂存状态,执行git commit则将修改同步到库中,这时库中的文件和本地文件又变为一致,文件为unmodify状态,执行git reset HEAD filename取消暂存,文件状态为Modified

查看文件状态

#查看指定文件状态
git status [filename]

#查看所有文件状态
git status

#添加所有文件到暂存区
git add .

# 提交暂存区中的内容到本地仓库 -m 提交信息
git commit -m	"消息内容"

在这里插入图片描述

忽略文件

前端的项目总 npm_moudles千万不要打包进去

在这里插入图片描述

后端的一些本地仓库的jar包不需要提交

有时候不想把某些文件纳入版本控制,比如数据库文件、临时文件、设计文件等

在主目录下建立".gitignore",此文件有如下规则:

  1. 忽略文件中的空行以井号(#)开始的行会将被忽略
  2. 可以使用Linux通配符。例如:星号(*)代表任意多个字符,问号(?)代表一个字符,方括号([abc])代表可选字符范围,大括号({string1,string2,…})代表可选的字符串等。
  3. 如果名称的最前面有一个感叹号(!),表示列外规则,将不被忽略。
  4. 如果名称的最前面是一个路径分隔符(/),表示要忽略的文件在此目录下,而子目录中的文件不忽略。
  5. 如果名称的最后面是一个路径分隔符(/),表示要忽略的的是此目录下该名称的子目录,而非文件(默认文件或者目录都忽略)。
*.txt		#忽略所有 .txt结尾的文件
!lib.txt	#但lib.txt除外
/temp		#仅忽略项目根目录下的TODO文件,不包括其他目录temp
build/		#忽略build/目录下的所有文件
doc/*.txt	#会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

例如下图(狂神说Java的git限制模板)

在这里插入图片描述
在这里插入图片描述

IDAE中集成git

(1、是我自己的操作,图一乐就行别当真,直接看2、)

1、创建项目是直接点git仓库

在这里插入图片描述

知识点:

snapshot是工程版本上的概念,表示快照版本,也就是相对稳定版本,但是会再进行改进,最好不要用于生产环境,在生产环境,应当选择release(发布版本)。

其他相关版本含义:

  • Alpha:内测版,BUG多,开发人员开发过程中使用,希腊字母α,第一,指最初版
  • Beta:早期版本,有缺陷,没有严重BUG,可能加入新功能,进一步开发完善
  • Gamma: 经Beta 版,完善修改,成为正式发布的候选版本(Release Candidate)
  • RC:(Release Candidate):候选版本,几乎就是正式版了
  • GA:(Ggeneral Availability):发行稳定版,官方推荐使用此版本
  • R,RELEASE:正式版,等价于GA

在这里插入图片描述

创建项目后等待依赖加载完成(这里的第三点是添加到缓存区按钮,带个小锁的才是上传至Gitee仓库按钮)

在这里插入图片描述

跳出以下弹窗

在这里插入图片描述

出现了以下报错!!!!

5:46:13.996: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false init
Initialized empty Git repository in E:/java_study/019-RuoYi/git-study/git-demo-01/.git/
15:46:17.336: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- .
warning: in the working copy of '.gitignore', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of '.mvn/wrapper/maven-wrapper.properties', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'mvnw', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'mvnw.cmd', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'pom.xml', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/main/java/com/jiaqi/gitdemo01/GitDemo01Application.java', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/main/resources/application.properties', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/test/java/com/jiaqi/gitdemo01/GitDemo01ApplicationTests.java', LF will be replaced by CRLF the next time Git touches it
16:04:41.961: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- mvnw .mvn/wrapper/maven-wrapper.jar .gitignore mvnw.cmd pom.xml .mvn/wrapper/maven-wrapper.properties src/main/java/com/jiaqi/gitdemo01/GitDemo01Application.java src/main/resources/application.properties src/test/java/com/jiaqi/gitdemo01/GitDemo01ApplicationTests.java
warning: in the working copy of 'pom.xml', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/main/java/com/jiaqi/gitdemo01/GitDemo01Application.java', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/test/java/com/jiaqi/gitdemo01/GitDemo01ApplicationTests.java', LF will be replaced by CRLF the next time Git touches it
16:04:42.076: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\1900\AppData\Local\Temp\git-commit-msg-.txt --author=甲柒 --
fatal: --author '甲柒' is not 'Name <email>' and matches no existing author

上述警告信息不用管,重新上传时不选author

再次点击提交

在这里插入图片描述

可以看到上传成功了~~~

现在去Gitee上看看

上传失败了!!!!

16:10:08.703: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- mvnw .mvn/wrapper/maven-wrapper.jar .gitignore mvnw.cmd pom.xml .mvn/wrapper/maven-wrapper.properties src/main/java/com/jiaqi/gitdemo01/GitDemo01Application.java src/main/resources/application.properties src/test/java/com/jiaqi/gitdemo01/GitDemo01ApplicationTests.java
16:10:08.778: [git-demo-01] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\1900\AppData\Local\Temp\git-commit-msg-.txt --
[master (root-commit) 6096f74] 测试上传到Gitee仓库
 9 files changed, 665 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .mvn/wrapper/maven-wrapper.jar
 create mode 100644 .mvn/wrapper/maven-wrapper.properties
 create mode 100644 mvnw
 create mode 100644 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/jiaqi/gitdemo01/GitDemo01Application.java
 create mode 100644 src/main/resources/application.properties
 create mode 100644 src/test/java/com/jiaqi/gitdemo01/GitDemo01ApplicationTests.java

文心一言的回答

上述代码中并没有包含远程仓库的地址。这是Git命令行的一些操作,它首先添加了一些文件到暂存区,然后提交了这些更改,但并未指定将这些更改推送到哪个远程仓库。

如果你想将更改推送到远程仓库,你需要使用 git push 命令,这通常需要知道远程仓库的地址。例如,如果你有一个名为 “origin” 的远程仓库,你可以使用以下命令推送更改:

git push origin master

在上述命令中,“origin” 是远程仓库的名称,“master” 是你想要推送的分支。如果你不知道远程仓库的地址,你可能需要先使用 git remote -v 命令查看你的远程仓库设置。

在这里插入图片描述

发现没有绑定远程仓库的地址,算了先去看狂神的视频

2、项目绑定git(狂神说版本)

创建项目时不选git仓库

在这里插入图片描述

随便选一个

在这里插入图片描述

我创建完成是这个样子的

在这里插入图片描述
git clone 我的gitee仓库中的项目

在这里插入图片描述

电脑的该文件夹下克隆成功

在这里插入图片描述

将上面三个文件复制到E:\java_study\019-RuoYi\git-study\git-demo-02

直接替换原有文件即可

在这里插入图片描述

IDEA中是这样的

在这里插入图片描述

可以看到git日志

在这里插入图片描述

再次git add.

在这里插入图片描述

现在是添加到暂存区了

在这里插入图片描述

然后点击推送按钮

在这里插入图片描述

第一次推送会出现连接密码,填写之前创建密钥的密码即可

在这里插入图片描述

可以看到我已经推送成功了

在这里插入图片描述

现在去Gitee上看一下,成功了~~~

在这里插入图片描述

3、修改文件,使用IDEA操作git(核心步骤)

  • 添加到暂存区
  • commit提交
  • push到远程仓库

4、提交测试

在这里插入图片描述

提交成功~~~

在这里插入图片描述

说明:git分支(多人开发)

分支在git中相对较难,分支就是科幻电影里面的平行宇宙,如果两个平行宇宙互不干扰,那对现在的你也没啥影响。不过,在某个时间点,两个平行宇宙合并了,我们就需要处理一些问题了!

在这里插入图片描述

git分支中常用指令:

# 列出所有本地分支
git branch

# 列出所有远程分支
git branch -r

# 新建一个分支,但依然停留在当前分支
git branch [branch-name]

# 新建一个分支,并切换到该分支
git checkout -b [branch]

# 合并指定分支到当前分支
$ git merge [branch]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

定义多个分支如果并行执行,就会导致代码不冲突,也就是多个版本!!

这个由于无法测试,就不测试了,哈哈哈哈~~~

多个分支如果并行执行,就会导致我们代码不冲突,也就是同时存在多个版本!

web-api -A(Restful.xx())

web-admin -B 会调用A(修改了A的代码!)

web-app -C 会调用B和A的代码

如果了冲突了就需要协商即可!

如果同一个文件在合并分支时都被修改了则会引起冲突:解决的办法是我们可以修改冲突文件后重新提交选择要保留他的代码还是你的代码!

master主分支应该非常稳定,用来发布新版本,一般情况下不允许在上面工作,工作一般情况下在新建的dev分支上工作,工作完后,比如上要发布,或者说dev分支代码稳定后可以合并到主分支master上来。

git的帮助页面

1900@DESKTOP-694C4A9 MINGW64 /e/java_study/019-RuoYi/git-study/Git-XueXi/git-study02 (master)
$ git --help
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--config-env=<name>=<envvar>] <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

官网的git大全:https://gitee.com/all-about-git

官网的git学习:https://oschina.gitee.io/learn-git-branching/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甲柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值