git

基本命令

0.首先分三块区域
	1.代码区 写代码的文件 任意修改
	2.缓存区 git add 提交 可以删除 没人发现
	3.版本区 提交就无法删除 只能新版本覆盖
1.首先做全局用户签名 [必须先签名 否则无法提交代码]
	1.git config --global user.email tomorrow@gmail.com
	2.git config --global user.name tomorrow
	3.这个只是标识身份用 知道是谁发布了版本 和 将来推送 github 和 gitee 无关
2.git init 初始化本地库
	1.会在当前目录下生成.git的隐藏目录
3.git status 查询当前状态
	1.没有任何文件时候的提示
		$ git status
		On branch master
		No commits yet
		nothing to commit (create/copy files and use "git add" to track)
	2.当创建一个 hello.txt
		$ git status
		On branch master
		No commits yet
		Untracked files:
		  (use "git add <file>..." to include in what will be committed)
		hello.txt[红色提示]
		nothing added to commit but untracked files present (use "git add" to track)
	3.当执行 git add hello.txt 后
		$ git status
		On branch master
		No commits yet
		Changes to be committed:
		  (use "git rm --cached <file>..." to unstage)
				new file:   hello.txt [绿色提示]
4.git add file 添加到 缓存区
	1.此时可以悄无声息的删除掉 git rm --chached hello.txt 删除
	2.删除后 执行 git status hello.txt [红色提示]
	3.如果添加到缓存区 编辑了 hello.txt 后
		$ git status
		On branch master
		No commits yet
		Changes to be committed:
		  (use "git rm --cached <file>..." to unstage)
				new file:   hello.txt [绿色提示]

		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:   hello.txt [红色提示]
	4.git add . 提交所有文件到 暂存区
	5.工作区 红 暂存区 绿 本地仓库 不再显示提示
5.git rm --cached hello.txt
6.git restore hello.txt 可以把当前文件 恢复到 缓存区的 状态
7.git commit -m "original version" hello.txt
	1.屏幕显示
		$ git commit -m "original version" hello.txt
		[master (root-commit) 446eb2b] original version
		1 file changed, 13 insertions(+)
		create mode 100644 hello.txt
	2.此时执行 git status 世界干净了
		$ git status
		On branch master
		nothing to commit, working tree clean
	3.如果修改了 hello.tet 还没有 git add 提交到缓存区 直接执行 git commit 也是可以的 执行后 世界安静
		1.此时 git add 默认被执行了 也就是说 缓存区 已经是最新内容了 所以要谨慎 commit
8.git reflog 查看简单的 版本区信息
	1.执行后 如下
		$ git reflog
		58de253 (HEAD -> master) HEAD@{0}: commit: can i commit
		446eb2b HEAD@{1}: commit (initial): original version
	2.记录得很详细 像整体的日志 包括 切换 分支 但是 git log 只显示当前分支的 版本,切换之类的信息不会显示
9.git log 查看详细的版本信息
	commit 58de2535b3b84809a5a55930fcf5e199e28e28c2 (HEAD -> master)
	Author: tomorrow <tomorrow@gmail.com>
	Date:   Mon Jul 3 00:45:32 2023 +0800
		can i commit

	commit 446eb2b4b9c02c8dc8d0740c8d88eb7ffa3c6a51
	Author: tomorrow <tomorrow@gmail.com>
	Date:   Mon Jul 3 00:40:23 2023 +0800
		original version
10.git reset --hard version
	1.如下
		$ git reset --hard 446eb2b
		HEAD is now at 446eb2b original version
	2.此时执行 git reglog 会发现 master 指针发生了 变化
		$ git reflog
		446eb2b (HEAD -> master) HEAD@{0}: reset: moving to 446eb2b //从 哪里 变化到哪里
		09b3a4d HEAD@{1}: commit: third submit
		58de253 HEAD@{2}: commit: can i commit
		446eb2b (HEAD -> master) HEAD@{3}: commit (initial): original version // 当前版本指针
	3.此时工作区的文件 也恢复到 这个版本提交时候的状态 多出的文件被删除掉了
11.git branch //查看分支 git branch branchName 创建分支 也就是类似把当前文件复制一份 但本质不是 是指针操作而已
	1.此时在 .git\refs\heads 记录指针指向的文件夹下面 有两个文件 分别记录着当前自己分支的 版本号
		1.matster
			1.446eb2b4b9c02c8dc8d0740c8d88eb7ffa3c6a51
		2.hot-fix
			1.446eb2b4b9c02c8dc8d0740c8d88eb7ffa3c6a51
		3.所以本质上操作的是指针 和 不同分支的版本记录 文件还是那个文件
	2.当只有 master 一个分支的时候
		$ git branch
		* master
	3.当添加一个分支 hot-fix 后
		$ git branch
		  hot-fix
		* master
	4.git branch -v 可以查看详细的 * 表示当前所在的分支
		hot-fix 446eb2b original version
		* master  446eb2b original version
12.git checkout hot-fix 切换分支 分支的当前 hello.txt 内容和 master 的内容 回随着 版本的指向不同而不同
	1.对于在同一个文件下 的 物理文件 hello.txt 本身 随着 分支的 切换 和 不同分支的 指针不同 也随着改变
	2.如果你切换回了 hot-fix 当你修改了 hello.txt 但是没有 commit 是不允许切换到 master 分支的 也就是说你切换
		.分支前 你不能让工作在工作区 必须到 版本区
13.git merge hot-fix 分支合并
	1.在工作中必须先切换回 master 分支 然后再执行 [也可以 hot-fix 合并 master]
	2.合并后 master 拥有 hot-fix commit 的 每个版本 就像自己修改的一样
	3.如果合并有冲突 可以 用 git merge --abort 终止合并 文件会退回到合并之前的版本
	4.当合并冲突 手动处理后 提交的时候 git commit -m "merge v1" 后面不要写名字了 因为合并的时候 已经说明过了
		1.提交后 后面的 [Mergeing 消失]
14.git remote add aliasName url //添加地址别名
	1.git remote add hello https://github.com/82992606/hello.git
	1.git remote -v 查看当前别名
		$ git remote -v 查看 推拉地址
		hello   https://github.com/82992606/hello.git (fetch)
		hello   https://github.com/82992606/hello.git (push)
15.远程命令
	1.git push 别名/url[github/gitee] 分支
		1.登录用户名密码 ok
	2.git clone url [不需要登录] https://gitee.com/tomorrow9527/hello.git
		1.里面有各个版本 等于复制了本地一份 给别人
	3.git pull url 分支
		1.如果在 master 分支 拉了 hot-fix 那么 直接处于 merge 状态 如果没有冲突 直接合并 有冲突等待处理
	4.git push remote master 提交到远程分支 master (远程已经创建了 master 的前提下)
		1.git push remote develop:develop 提交到 远程 develop 目录 如果没有就创建
	5.git remote -v
		1.查看Git别名的远程地址
			h1      https://gitee.com/tomorrow9527/hello.git (fetch)
			h1      https://gitee.com/tomorrow9527/hello.git (push)
			hello   https://github.com/82992606/hello.git (fetch)
			hello   https://github.com/82992606/hello.git (push)
	6.git remote set-url <remote-name> <new-url>
		1.修改 别名的远程地址
			1.例如,如果您想要将名为origin的远程仓库的URL地址修改为https://github.com/example/new-repo.git
			2.可以执行以下命令 git remote set-url origin https://github.com/example/new-repo.git
	7.git config remote.h1.url 查询 远程 h1 这个 别名的 url
			1.结果:https://gitee.com/tomorrow9527/hello.git
			3.也可以这样修改 git config remote.origin.url https://github.com/example/new-repo.git
	8.git remote rename <old-name> <new-name>
		1.修改远程地址的别名
	9.git push gitee develop:develop
		1.要不要:develop都可以 实测 就算远程没有这个分支 一样会创建

经验

1.如果在 gitee 在线编辑了文件 保存后 本地在没有 pull 之前 是无法 push 的 因为网络的版本更新一些 首先要pull 然后再 push
2.如果在 develop 分支 创建了一个文件 d.txt 但是 没有 add 没有 commit 这时候 master 和 develop 的仓库版本是一致的 所以切换到
	1.master 分支 d.txt 也是存在的 但是如果在 分支 develop  下 commit 后 再切换到 master 分支下 d.txt 会消失 因为 仓库的
	2.版本不同 但是如果没有执行 git commit 添加到仓库 而是仅仅 git add . 切换、分支后 文件是不会消失的 所以仓库才是标准
	3.并且此时 master 下 git status 也是绿色 说明了 工作区和暂存区大家是公用的
3.git push 免密(测试也没有需要密码啊)
	1.ssh-keygen -t rsa 生成公钥
		1.Enter file in which to save the key: 可以不写 后面还有 三次都不用输入 回车就可以
		2.执行后在 C:\Users\Administrator\.ssh 下会生成两个文件
			1.id_rsa
			2.id_rsa.pub
				1.这个是公钥 用记事本打开 复制过去 到 gitee.com 设置 ssh 公钥 然后输入 密码了
	2.复制公钥到设置中
	3.clone ssh 协议的链接
		1.git clone git@gitee.com:tomorrow9527/hello.git
			1.平时用的是 https 协议
			2.克隆的时候 会询问 输入 yes 建立链接就可以了
		2.clone 后 (就算之前的分支都是 https 协议的) 会自动变成 git 协议
			1.git remote -v
				origin  git@gitee.com:tomorrow9527/hello.git (fetch)
				origin  git@gitee.com:tomorrow9527/hello.git (push)
			2.但是 别名已经发生了变化 之前的别名是 gitee  因为 别名只存在本地的 设置中 并不同步到 gitee 所以clone下来的
				1.就不包含这些信息  origin 是默认的写法

git文件夹结构

1.head [指针 内容:ref: refs/heads/master 执行 master] 查看 refs/heads/master 会看到当前的版本号
2.446eb2b4b9c02c8dc8d0740c8d88eb7ffa3c6a51

如何在github 上 push 项目

1.登录 github 后 创建一个 repository 名字随便起 只要不和自己的重名就行 选 public 后 直接创建就行 创建完毕 后 有一个地址 https/ssh
2.复制地址 起别名 git remote add hello weburl

杂项

1.关于乱码的设置 复制的时候 乱码 [[200
	1.options>text->charset:utf-8
2.有的命令显示的太长 比如 当版本过多后  git log 几页都显示不完 : q  可以退出显示 回到命令行
posted @ 2023-07-02 23:55  闭区间  阅读(8)  评论(0编辑  收藏  举报