Files
MakeNote/02.git.md
2025-11-06 16:46:31 +08:00

16 KiB
Raw Blame History

Git简介

Git的诞生

1991年 Linus开创了开源的Linux

2002年以前 Linus手动合并代码

2002年 BitKeeper免费授权

2005年 BitKeeper被尝试破解收回授权

2005年 Linus花了两周时间用C写出了git并且一个月之内将Linux搬迁到git上

2008年 GitHub上线

集中式与分布式

集中式

CVS、SVN

分布式

Git

Git安装

git最新版本没有安装包的话去官方地址下载

https://git-scm.com/

一路next直到安装完毕保持默认的选项

安装完毕后打开windows的cmd 命令界面,输入如下命令,来设置自己的用户名和邮箱。

git config --global user.name "Aaron"
git config --global user.email "Aaron@eagleslab.com"

创建版本库

版本库又名仓库,英文名repository 你可以简单的解成一个目录这个目录里面的所有文件都可以被Git管理起来每个文件的修改、删除Git都能跟踪以便任何时刻都可以追踪历史或者在将来某个时刻可以“还原”。

第一步 找个空的地方创建一个文件夹

第二步 在文件夹中打开cmd界面,然后输入git init 命令把这个目录变成Git可以管理的仓库

C:\Users\simid\Desktop\testrepo>git init
Initialized empty Git repository in C:/Users/simid/Desktop/testrepo/.git/

我们也可以发现,在这个文件夹中会创建一个.git的目录, 这个目录是Git来跟踪管理版本库的

添加文件到版本库

这边要注意git只能追踪文本文件的改动而二进制文件只能记录大小的变化。

第一步

  • 在我们的git文件夹中创建一个记事本文件编辑内容
  • 在记事本文件中写一些内容,比如哈哈哈大家一起来学习git
  • 使用命令git add告诉git把文件添加到暂存区中
git add readme.txt

执行上面的命令,没有任何显示

第二步

用命令git commit告诉Git把文件提交到仓库

C:\Users\simid\Desktop\testrepo>git commit -m "write a readme file"
[master (root-commit) 3e22070] write a readme file
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

git commit后面的-m是输入本次提交的说明的,可以输入任何内容,最好是自己能看懂的,这样就可以从理事会记录里面方便的找到改动记录。

执行成功后1 file changed 一个文件被改动了,也就是我们添加的readme.txt

1 insertions(+)`表示插入了一行内容,我们写的那个`哈哈哈大家一起来学习git`

注意

因为在commit前需要add一下,所以可以一次提交多个文件

C:\Users\simid\Desktop\testrepo>git add file1.txt

C:\Users\simid\Desktop\testrepo>git add file2.txt

C:\Users\simid\Desktop\testrepo>git commit -m "two files"
[master 9bcae3f] two files
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file2.txt

回滚操作

我们修改上面的文件readme.txt

然后使用git status查看

C:\Users\simid\Desktop\testrepo>git status
On branch master
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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

git status命令可以让我们时刻掌握仓库当前的状态,上面的命令输出告诉我们,readme.txt被修改过了,但还没有准备提交的修改。

如果想看看具体修改了什么内容,我们可以使用git diff来查看

C:\Users\simid\Desktop\testrepo>git diff
warning: in the working copy of 'readme.txt', CRLF will be replaced by LF the next time Git touches it
diff --git a/readme.txt b/readme.txt
index c237288..d25baa4 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
-哈哈哈大家一起来学习git
\ No newline at end of file
+哈哈哈大家一起来学习git
+下面准备开始提交代码
\ No newline at end of file

可以看到在第一行后面加了一行在下面

下面我们将其添加,然后查看status

C:\Users\simid\Desktop\testrepo>git add readme.txt
warning: in the working copy of 'readme.txt', CRLF will be replaced by LF the next time Git touches it

C:\Users\simid\Desktop\testrepo>git status readme.txt
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.txt

git status告诉我们,将要被提交的修改包括readme.txt,下一步,就可以放心地提交了

C:\Users\simid\Desktop\testrepo>git commit -m "change readme.txt"
[master 6ced986] change readme.txt
 1 file changed, 2 insertions(+), 1 deletion(-)

提交后,我们再用git status命令看看仓库的当前状态:

C:\Users\simid\Desktop\testrepo>git status
On branch master
nothing to commit, working tree clean

Git告诉我们当前没有需要提交的修改而且工作目录是干净working tree clean

版本回退

我们创建一个文件叫game ,在这个文件中写入以下内容

屠龙勇士村来了一个新勇士叫"林克"

然后提交,提交的说明就是"新的开始"

image-20191026212115336

在文件中追加内容

勇士赤手空拳来到了野外

然后提交,提交的说明就是"走出新手村"

image-20191026212310506

在文件中追加内容

勇士林克被Lv1怪物史莱姆一屁股坐死

然后提交,提交的说明就是"阵亡"

image-20191026212439959

在文件中追加内容

勇士林克被公主"塞尔达"所救,丢失全部金币!

然后提交,提交的说明就是"复活"

image-20191026212614476

下面我们来看下这个悲催的勇士的经历,可以使用git log查看历史提交

image-20191026212755780

git log命令显示从最近到最远的提交日志我们可以看到3次提交

如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数

image-20191026212850868

其实勇士的悲剧完全可以逆转,我们可以读档新的开始,让勇士在出门前带把武器

image-20191026213156010

Git的版本回退速度非常快因为Git在内部有个指向当前版本的HEAD指针当你回退版本的时候Git仅仅是把HEAD从指向新的开始

┌────┐
│HEAD│
└────┘
   │
   └──> ○ 复活
        │
        ○ 阵亡
        │
        ○ 走出新手村
        │
        ○ 新的开始

改为指向add distributed

┌────┐
│HEAD│
└────┘
   │
   │   ○ 复活
   │   │
   │   ○ 阵亡
   │   │
   │   ○ 走出新手村
   │   │
   └──>○ 新的开始

在Git中HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

Git提供了一个命令git reflog用来记录你的每一次命令:

工作区和暂存区

工作区

就是你在电脑里能看到的目录,比如上面的gitlearn文件夹

image-20191026213858444

版本库

工作区有一个隐藏目录.git这个不算工作区而是Git的版本库。

Git的版本库里存了很多东西其中最重要的就是称为stage或者叫index的暂存区还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD

image-20191026214144680

前面讲了我们把文件往Git版本库里添加的时候是分两步执行的

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

因为我们创建Git版本库时Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。

你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

管理修改

git不是管理文件而是管理修改的操作。

我们修改readme.txt然后add提交到暂存区

C:\Users\simid\Desktop\testrepo>cat readme.txt
哈哈哈大家一起来学习git
下面准备开始提交代码
修改一哈
C:\Users\simid\Desktop\testrepo>git add readme.txt
warning: in the working copy of 'readme.txt', CRLF will be replaced by LF the next time Git touches it

然后再次修改readme.txt这次我们直接commit

C:\Users\simid\Desktop\testrepo>cat readme.txt
哈哈哈大家一起来学习git
下面准备开始提交代码
修改一哈
修改二哈
C:\Users\simid\Desktop\testrepo>git commit -m "猜猜是一还是二"
[master d716d69] 猜猜是一还是二
 1 file changed, 2 insertions(+), 1 deletion(-)

然后查看状态

C:\Users\simid\Desktop\testrepo>git status
On branch master
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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

我们会发现第二次修改的并没有被提交

查看一下工作区的文件和仓库的文件的区别

C:\Users\simid\Desktop\testrepo>git diff head -- readme.txt
warning: in the working copy of 'readme.txt', CRLF will be replaced by LF the next time Git touches it
diff --git a/readme.txt b/readme.txt
index cf326ab..d04489f 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@
 哈哈哈大家一起来学习git
 下面准备开始提交代码
 修改一哈
+修改二哈

撤销修改

  • 当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file
  • 当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD ,就回到了上一种情况,第二步按上一种情况操作操作。

删除文件

我们将文件从工作区删除

image-20191026220634410

git会直接察觉到我们的删除操作如果这个时候提交那么就会从版本库中删除该文件

image-20191026220737779

但是如果误删除了,可以还原到版本库中的最新版本

image-20191026220846139

如果要删除暂存区中的文件,可以使用git rm

远程仓库

注册gitee

配置密钥

  • 打开命令行创建用于登录gitee仓库的key
C:\Users\simid\Desktop\testrepo>ssh-keygen -t rsa -C "simidaxu@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\simid/.ssh/id_rsa):  <-- 注意,这里直接回车
Enter passphrase (empty for no passphrase): <-- 注意,这里直接回车
Enter same passphrase again: <-- 注意,这里直接回车
Your identification has been saved in C:\Users\simid/.ssh/id_rsa
Your public key has been saved in C:\Users\simid/.ssh/id_rsa.pub  <-- 注意这个是密钥位置
The key fingerprint is:
SHA256:MlHuxb1mUBUHCxDtNljwtAT2nqN4C9AjhCjnpGO+068 simidaxu@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
|        . **=.+o.|
|   . . o o Boo o |
|. + . o . ++= .  |
| *   . + ..o+o   |
|o..   = S  .B.   |
|o.     = o + .   |
| ..     o o      |
| ...     o .     |
| ..Eo.    .      |
+----[SHA256]-----+
  • 打开gitee账号设置

image-20251106162201573

  • 打开ssh公钥

image-20251106162235508

  • 随便起个标题,建议和当前的设备挂钩,这样方便后续管理各个设备是否可以登录
  • 公钥就直接粘贴上面生成的文件里面的内容,使用cat 上面公钥的位置 命令可以查看

image-20251106162431939

  • 添加成功后

image-20251106162530872

  • 测试能否连接成功!
C:\Users\simid\Desktop\testrepo>ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.76.198.225)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes <-- 此处输入yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi AaronXu(@hiaaron)! You've successfully authenticated, but GITEE.COM does not provide shell access.

添加远程仓库

  • 点击新建仓库

image-20251106163038203

  • 填写名称和描述

image-20251106163204796

  • 创建成功之后,按照仓库给出的入门教程,可以将已有的仓库同步上来

image-20251106163301152

  • 开始操作中途第一次会弹出来输入gitee的用户名和密码之后就不会有了
C:\Users\simid\Desktop\testrepo>git config --global user.name "AaronXu"

C:\Users\simid\Desktop\testrepo>git config --global user.email "718827633@qq.com"

C:\Users\simid\Desktop\testrepo>git remote add origin https://gitee.com/hiaaron/testrepo.git

C:\Users\simid\Desktop\testrepo>git push -u origin "master"
remote: [session-104850bf] hiaaron: Incorrect username or password (access token)
fatal: Authentication failed for 'https://gitee.com/hiaaron/testrepo.git/'

C:\Users\simid\Desktop\testrepo>git push -u origin "master"
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 16 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (12/12), 997 bytes | 332.00 KiB/s, done.
Total 12 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag c24dfd94
To https://gitee.com/hiaaron/testrepo.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.
  • 代码提交成功

image-20251106163525799

仓库使用

  • 一些常用的操作

image-20251106163627958