Git知识点

1.git是什么?

官网地址:
https://git-scm.com/·
distributed version control system分布式版本控制系统

2.version control system?

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
记录文件变化的 并且 之后可以指定版本进行恢复
场景

1.论文: 
	1.xxx_v1.doc 
	2.xxx_v2.doc 
	...
	xxx_最终版.doc 
  1. 种类:
  • Local Version Control Systems(本地版本控制系统): copy files into another directory

    • 优点:
      so simple
    • 缺点:
      • error prone
      • 代码集成效率低下
  • Centralized Version Control Systems(集中式版本控制系统):

    • 解决:
      • 代码集成效率低下
      • 多人协同开发的问题
    • 问题:
      • single point 单点故障[中央服务器突然挂掉]
        故障期间没有人可以进行协同开发,和保存版本变化
  • Distributed Version Control Systems(分布式版本控制系统):

    • 解决: 多人协同开发 单点故障
      1. clients(客户端【多个】)
      2. server(服务端)

3.git:

  1. efficiency.(效率高)
  2. performance.(性能高)
  3. 分支操作、文件备份、定制工作流程
  • 文件版本控制:
    1. 本地仓库
    2. 远程仓库

4.部署

  1. 下载
  2. 安装
  3. 使用:
    git 软件 工具
    1. 可视化界面的方式进行操作 =》 git原理
    2. git 命令操作 =》 linux 命令差不多
	linux 内核 林纳斯 
	git

5.git相关命令

1.查看git版本

$ git -v
git version 2.38.0.windows.1

2.使用:版本控制

  1. 使用的前的配置
  • 要配置我们的项目开发人员的信息
$ git config --global user.name "qiqi"
$ git config --global user.email "w_xuan77@163.com"
  1. 设置git参数 => 本地仓库
    usage: git config []

    Config file location
    –global use global config file --对当前用户(win) 所有仓库都有效
    –system use system config file – 对系统所有登录用户有效
    –local use repository config file – 对某个仓库有效

    思考:
    单单对某一个项目来说 上面哪个参数 有限级高?
    –local

  2. 查看配置参数:

git config [options]
options = option...

按照级别进行查看

	git config --list 
	git config --list --global
	git config --list --local

1.创建仓库

项目:开发的代码文件
仓库:本地仓库 用于进行文件的版本控制的

git init

  1. 创建一个仓库
  2. 重新初始化一个 以及存在的仓库
git init [-q | --quiet] [--bare] [--template=<template-directory>]
          [--separate-git-dir <git-dir>] [--object-format=<format>]
          [-b <branch-name> | --initial-branch=<branch-name>]
          [--shared[=<permissions>]] [<directory>]
git init [<directory>]

项目:

  1. 存在:
    cd 项目目录
    git init
  2. 不存在:(在创建一个仓库的同时生成一个项目)
    git init [<directory>]=> git init <project_name>
    =>git init test01

2.使用

git 管理版本控制的工作流程: 
	工作区: 项目目录
	暂存区: 临时存储文件的地方 可以进行撤回
	本地仓库:版本控制
  1. 入门:
    1.vim 1.log
    2.git add 1.log => 添加到暂存区
    3.git commit -m <msg>=>git commit -m "Add 1.log" => 添加到 本地仓库 -m "..."记录本次提交的信息-m <msg> = --message
    4.git log 查看本地仓库历史 :当前项目的 commit历史

    总结:
    本地仓库 :
    如何进行版本控制?
    commit 进行区分版本的

  2. 切换版本: 可以使用
    git reset --hard [<commit>]
    git reset --hard d7db6e
    d7db6e=>所选择版本的hash值,选择长短看心情
    了解:
    git log --reflog查看以前的历史版本

  3. 练习:
    编写一个小项目 ,提交到git本地仓库

     工作区: 开发代码 => git add xxx
     暂存区 : 暂时存放我们开发的代码文件 => git commit 
     本地仓库:存放各个版本的历史
    

前端项目 github 下载 html 项目

  1. 初始化项目
    git init music
  2. 添加一个index.html文件
git add index.html
git commit -m "Add index.html"
git log
  1. 编写其他文件
git status   查看文件状态(查看工作区和暂存区之间的文件差异)
#文件或者文件夹爆红说明所开发的项目文件只存在于工作区并没有提交到暂存区
git add font/  icon/ image/ music/   提交文件到暂存区
git status   查看文件状态(查看暂存区和工作区之间的文件差异)
#文件或者文件夹爆绿说明所开发的项目文件只存在于暂存区并没有提交到本地仓库
git commit -m "Add other data"       提交项目文件到本地仓库
git status   查看文件状态
git log
  1. 添加页面样式
git status
git add  script.js  style.css
git status
git commit -m "Add css&js"
git status
git log
  1. 修改代码文件
git status
修改了代码文件 新增文件 
git status
git add -u   将修改代码后的文件全部更新
git status
git commit -m "Update"
git status
git log

git add -u=git add index.html+git add style.css+...
补充:
1.git add -u = git add --updata在工作区对原本已经上传到暂存区的文件进行更新是可以使用。
2.git log => 查看版本历史:
git log :
--reflog => 查看所有操作的 commit 历史
1.指定输出格式

  • git log
$ git log
commit ba2c4025e5596a4787ecea29d14e5c371650b942 (HEAD -> master)
Author: qiqi <w_xuan77@163.com>
Date:   Wed Oct 11 17:25:03 2023 +0800

    Update

commit a89fcb96c25317e370ef775bffaa200e47ad1693
Author: qiqi <w_xuan77@163.com>
Date:   Wed Oct 11 17:13:43 2023 +0800

    Add CSS&JS

commit bfa8ccf30935ecb6b7eecd11075f09b8d28f6349
Author: qiqi <w_xuan77@163.com>
Date:   Wed Oct 11 16:56:21 2023 +0800

    Add other data

commit 024343528aa91f15537745122098bb7d7b6cff9a
Author: qiqi <w_xuan77@163.com>
Date:   Wed Oct 11 16:39:35 2023 +0800

    Add index
  • format
  1. git log --format=short缺少更新时间
$ git log --format=short
commit ba2c4025e5596a4787ecea29d14e5c371650b942 (HEAD -> master)
Author: qiqi <w_xuan77@163.com>

    Update

commit a89fcb96c25317e370ef775bffaa200e47ad1693
Author: qiqi <w_xuan77@163.com>

    Add CSS&JS

commit bfa8ccf30935ecb6b7eecd11075f09b8d28f6349
Author: qiqi <w_xuan77@163.com>

    Add other data

commit 024343528aa91f15537745122098bb7d7b6cff9a
Author: qiqi <w_xuan77@163.com>

    Add index
  1. git log --format=oneline
$ git log --format=oneline
ba2c4025e5596a4787ecea29d14e5c371650b942 (HEAD -> master) Update
a89fcb96c25317e370ef775bffaa200e47ad1693 Add CSS&JS
bfa8ccf30935ecb6b7eecd11075f09b8d28f6349 Add other data
024343528aa91f15537745122098bb7d7b6cff9a Add index

git log --oneline [常用]

$ git log --oneline
ba2c402 (HEAD -> master) Update
a89fcb9 Add CSS&JS
bfa8ccf Add other data
0243435 Add index

git log --oneline --reflog 可以连用
git log --oneline -n 2 => -n 2显示最近几个commit
-n 2: 使用场景:
1.commit 特别多的时候
2.分支有关

6.分支:

  • 一个分支一条线
  • 默认分支:
    主分支:主线 master
    git branch:
    branch List, create, or delete branches
git branch [--color[=<when>] | --no-color] [--show-current]
[-v [--abbrev=<n> | --no-abbrev]]
[--column[=<options>] | --no-column] [--sort=<key>]
[--merged [<commit>]] [--no-merged [<commit>]]
[--contains [<commit>]] [--no-contains [<commit>]]
[--points-at <object>] [--format=<format>]
[(-r | --remotes) | (-a | --all)]
[--list] [<pattern>…​]
git branch [--track[=(direct|inherit)] | --no-track] [-f]
        [--recurse-submodules] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-c | -C) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>…​
git branch --edit-description [<branchname>]
  1. 创建分支
    git branch -c dev =>默认从master直接复制来的
    git branch -c => 基于前一个分支拷贝完成
asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02 (master)
$ git branch -c dev

asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02 (master)
$ git branch -v
  dev    ba2c402 Update
* master ba2c402 Update

asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02 (master)
  1. 查看分支
    git branch -v
    在这里插入图片描述

*表示的是当前git仓库所在的工作分支
3. 切换分支
git checkout dev

asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02 (master)
$ git branch -c dev

asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02 (master)
$ git branch -v
  dev    ba2c402 Update
* master ba2c402 Update
  • git log --oneline master => 指定查看某个分支的 commit 历史
  • git log --oneline --gragh => 以图形化界面展示 分支的commit 历史
  • git log --oneline --gragh --all => 以图形化界面展示 所有分支 的commit 历史
  • gitk --all
    1.历史树 分支线
    2.path => 记录当前commit 变更文件
    3.tree => 记录当前commit 下文件结构
  1. 补充: 创建分支同时并切换分支
    分支:
    git checkout
    git checkout [-b|-B|--orphan] <new-branch>] [<start-point>]
    • eg: 创建分支同时并切换分支
      git checkout -b bigdata 61b6bb8
      head指向任意分支的任意commit是指定某次commit的hash值时,保留此次hash值(61b6bb8)及以前的commit历史,此次hash值后的commit历史清空。
    • git commit -am
      -a = --all 表示 工作区 暂存区 文件都有的前提下 ,更改工作区文件 可以直接提交到本地仓库 但是 新增文件 不可以
  2. 删除分支
    git branch (-d | -D) <branchname>…
    merge:合并分支
    -d = --delete=> 删除分支之前 要求merge 到主分支
git branch -d java
error: The branch 'java' is not fully merged.
If you are sure you want to delete it, run 'git branch -D java'

-D = --delete --force=>强制删除分支

7..git文件夹 目录介绍

  1. HEAD 文件:当前项目 正在工作的分支是谁
$ cat HEAD
ref: refs/heads/master
  1. config : 记录项目的一些参数 一些配置信息
$ cat config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02/.git (GIT_DIR!)
$ git config --local user.name "qiqi"  //设置user.name
//如果不想用命令行修改参数,可以直接修改config文件

asus@LAPTOP-2PBDQ2B8 MINGW64 /f/Git/project/music_02/.git (GIT_DIR!)
$ cat config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = qiqi

git config --local user.name "zihang" => set 设置参数
git config --local user.name => get 操作,获取参数所对应的值

  1. refs/
  • heads/ 存储的是分支的指向 哪个commit
    • commit hash =》 对应存储的文件内容是什么?
      • 对应文件存储在/.git/objects
    • git cat-file(git查看文件命令)
      • -t => 文件类型 => git存储文件 有关系
      • -p => 文件内容
    • object:是git存储的真正对象 种类有 如下
      • commit =>
        • tree =>
          • tree =>文件夹
          • blob => 文件

git存储文件高效的原因:不同的文件 只要文件内容有相同的部分 就是一个 blob

8.如何修改commit的msg 信息

  1. 修改最新的commit的msg
    git commit --amend
  2. 修改之前某一个的commit的msg
    git rebase => 变基
    git rebase -i <after-this-commit>
    • -i 表示交互式
    • 修改之前某一个的commit = 基于前一个或多个commit 进行修改当前的commit
      p, pick <commit> = use commit => 选择某个commit
      r, reword <commit> = use commit, but edit the commit message => 重写某个commit msg
      s, squash <commit> = use commit, but meld into previous commit => 合并多个commit msg

9.工作区 vs 暂存区 vs 本地仓库

  1. 查看文件差异
    git status => 文件的个数 名字
  2. 查看文件内容差异
    • 所有文件内容差异:
      1. 比较 不同工作区
        git diff 工作区 vs 暂存区 文件内容差异
        git diff --cached 暂存区 vs 本地仓库 文件内容差异
      2. 比较不同的commit 之间差异
        git diff 9cbef63 0aece3e
        git diff HEAD 0aece3e
        git diff HEAD HEAD~1
        git diff HEAD HEAD~2
        ~1=>前一个版本
        ~2=>前两个版本

比较具体文件之间的差异(不同工作区同样适用)
git diff HEAD HEAD~2 -- 2.log 1.log
3. 恢复操作
工作区 vs 暂存区 vs 本地仓库

  1. 工作区 =》 暂存区
    1. git add
      可以进行撤回
      git restore --staged <file>...
      git restore --staged 1.log 2.log 3.log
    2. 工作区:git add 之前
      编写代码 也可以撤回
      git restore <file>...
      git restore 3.log

10.删除文件 文件重命名

git : 建议
相关命令 进行操作

  1. 删除/重命名文件
    git rm/mv
    git commit -m "Rm 11.log"
  2. 补充: 【了解】
rm: git rm`
    rm -rf xxx 
    git add xxx 
    git commit -m "Rm xxx"

mv : git mv

11.紧急任务

开发代码 =》 线上bug =》commit 
		=》开发代码 存起来 =》恢复出来 进行开发

git stash

  1. 开发代码
    $ vim 2.log
  2. 突然线上bug +解决bug
    git stash
    commit
  3. 恢复 解决bug之前的开发代码
    git stash list
    git stash apply 补充 : --index <stash 下标>

12. .gitignore

通过这个文件 可以指定 哪些文件 不需要被git管控
-- java 
		=》 xxx.java  => xxx.class
		=> .idea 文件里面的文件 也是不需要被管控
	
	eg: 
		*.class  =》以.class文件结尾的 不需要被 git管控 
		log/

13.仓库备份:克隆

git clone:
	git clone  <repository> [<directory>]

		repository: 仓库
			本地仓库: path
			远程仓库:url 
  1. 本地仓库 备份到 本地 【不会用的】
    git clone /e/test/music/.git music_blk
  2. 远程仓库 备份到 本地 【常用的】
远程仓库: 
	本地项目
	代码托管平台:github、gitlab、gitee

git clone http://192.168.41.110/dl2262/dl2262.git

14.本地仓库 与 远程仓库 交互

1.本地仓库 连接到 远程仓库

	git remote: 
		git remote add <name> <URL>
  1. 连接远程仓库:
    git remote add gitlab http://192.168.41.110/dl2262/test01.git
  2. 检查远程仓库的连接
    git remote -v
  3. 本地仓库 push 远程仓库
    git push ==> 更新远程仓库的分支
    git push [--all ] ==> 本地所有分支 更新到远程仓库 分支
    git push [<repository> [<refspec>…​]]=》更新 可以选择 哪个远程仓库 哪个分支
eg: 
	git push --all 
$ git push --all

 * [new branch]      dev -> dev   =》 本地dev =》 远程 自动给远程 dev 分支
 ! [rejected]        master -> master (fetch first) 
error: failed to push some refs to 'http://192.168.41.110/dl2262/test01.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决:
先拉下来:

  • git fetch / git pull :
    git fetch [<repository> [<refspec>…​]] ==》可以指定拉取哪个仓库哪个分支
    git fetch --all ==> 拉取远程仓库 所有分支

  • git fetch gitlab master ==>默认先把远程 分支拉下来 但是文件没有和 本地分支 进行合并

  • git merge 远程分支:

  • git merge gitlab/master

    • fatal: refusing to merge unrelated histories
  • git merge --allow-unrelated-histories gitlab/master

  • git push gitlab master

场景:

1.多人协同开发 常用操作

	1.多人共同维护同一个分支 操作不同的文件 【不会有代码冲突问题】
		1.远程仓库创建一个分支 
			bigdata 
		2.准备两个 本地仓库 
		git clone http://192.168.41.110/dl2262/test01.git test01_blk

		3.zhangsan 开发项目 提交到 远程仓库 
		4.lisi开发项目 提交到 远程仓库
			1.git fetch 
			2.git merge 
			3.git push 

		注意: 
			1.拉取远程仓库代码 =》 本地仓库 和 远程仓库保持一致

			git pull  <=> git fetch  + git merge

思考: 【学有余力的同学试试】

	1.注意: 只有一种场景会有冲突:

		1.多人共同维护同一个分支 操作相同的文件 不同位置 =》 不会有冲突

			1.zhangsan 
				vim README.md
				 git add ./README.md
				 git commit -m "Update Readme zhangsan"
				 git branch -av
				  git remote -v
				   git push gitlab bigdata

			2.lisi 
				vim README.md
				 git commit -am "Update Readme lisi"
				 git branch -av
				git remote -v
				 git push origin bigdata
				To http://192.168.41.110/dl2262/test01.git
				! [rejected]        bigdata -> bigdata (fetch first)

				git fetch origin bigdata =>更新本地仓库 
				 git merge origin/bigdata 
					 Auto-merging README.md
					CONFLICT (content): Merge conflict in README.md

					git add README.md
					git commit -m "Resolved conflict by hand"
					git branch -av
					git remote -v
					git push origin  bigdata

		多人共同维护同一个分支 操作相同的文件 的内容相同位置 
			git pull 

2.如何切换远程仓库 版本

		1.本地切换 
			git log  --oneline
			git reset --hard fd1e890
			git branch -av
		2.强制push 
			git push -f gitlab bigdata

			远程仓库有要求:
				保护分支的机制
  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值