git 基础

什么是git

git是一种分布式的版本控制系统。可以对仓库进行备份与协同工作。

工作原理:项目文件存放在服务端上,客户端使用时会将服务端上的内容进行一次完整的克隆,因此,即使服务器上的代码丢失也可以轻易的恢复数据。

git 配置

git config --global user.name '樗木'
git config --global user.email '2082181572@qq.com'

作用: 配置完后每次提交代码,将会显示提交人的姓名与邮箱。方便查询每次代码修改都是由谁完成的。

:--global 代表全局。可不加。加上后,电脑中全部的项目都使用该配置,如不加指针对当前目录的项目生效。

工作区与暂存区

git仓库在项目管理时会生成三个区域。

工作区、暂存区、仓库

工作区

暂存区

仓库

工作区就是,我们看到的,正在编辑的文件。仓库就是修改完的代码。

在开始工作时,一开始我们的代码也就是工作区代码与仓库是一致的,我们正在修改的时候为了不影响仓库的正常工作,方便别人使用,我们将工作区与仓库分割开来,这样我们修改代码时就不会影响仓库,当我们将代码完全修改完,我们就可以让仓库同步到工作区代码。这大大保证了仓库代码的完整性。

暂存区是在工作区 与 仓库间的中间区域。用于选择我们将哪些文件同步到仓库。

例:

我们修改了了1文件与2文件。我们只想让仓库同步1文件,不想同步2文件。我们就将1文件放进暂存区。然后同步,仓库会同步暂存区的内容。没有进入暂存区的2文件将不会被同步。

git的基础用法

在目录下初始化仓库 —— git init

这个不需要多解释,进入项目目录,输入:git init 将项目纳入 git管理

克隆现有的仓库 —— git clone

这个也不用多解释 git clone 链接

链接去具体的GitHub上找就行。

例:

git clone https://github.com/dream520nb/dataChance.git

查看当前仓库的情况

git statu
# 可以查看到当前的状态

例:

我创建了一个演示,输出结果如下

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   1.py

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:   1.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        3.py

# 翻译后结果如下:
在主分支上
待提交的更改:
  (使用 "git restore --staged <file>..." 来取消暂存)
        新文件:   1.py

未暂存的更改:
  (使用 "git add <file>..." 来更新将要提交的内容)
  (使用 "git restore <file>..." 来放弃工作目录中的更改)
        修改过的:   1.py

未跟踪的文件:
  (使用 "git add <file>..." 来包括在将要提交的内容中)
        3.py

待提交的意思是:在暂存区,不在仓库

未暂存的意思是:在工作区,不在暂存区

未跟踪的意思是:新创建的文件,只在工作区存在。

git ls-files 查看仓库中的文件

查看仓库中的文件内容,下面会演示

将文件添加进暂存区 —— git add

这个意思是将文件从工作区保存到暂存区。

下面我新建一个仓库举例说明

# 创建了三个新文件
PS F:\work\gitDemo> git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        1.py
        2.py
        3.py

nothing added to commit but untracked files present (use "git add" to track)

# 可以看到,我输入指令。三个文件都处于未跟踪的状态
# 接下来,我将add 1.py
PS F:\work\gitDemo> git add 1.py
PS F:\work\gitDemo> git status  
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   1.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        2.py
        3.py
# 可以看到1.py已经从工作区移动到了暂存区

提交变动到仓库 —— git commit

一次性将暂存区的内容全部提交到仓库

我们接着以一个例子进行讲解

# 我们继续用上的仓库。
# 先看一下当前的状态
PS F:\work\gitDemo> git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   1.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        2.py
        3.py
# 现在我们提交
PS F:\work\gitDemo> git commit -m '你好'
[main (root-commit) afb6fae] 你好
 1 file changed, 1 insertion(+)
 create mode 100644 1.py

# -m 后加注释。例如:修改了什么什么bug,添加了什么什么功能。这个注释是必须添加的。

# 现在我们再看看状态
PS F:\work\gitDemo> git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        2.py
        3.py

nothing added to commit but untracked files present (use "git add" to track)
# 可以看到暂存区已经没有东西了,全部提交到了仓库。
# 下面我们看看仓库中的东西
PS F:\work\gitDemo> git ls-files
1.py
# 可以看到git仓库中多了一个文件

查看日志 —— git log

PS F:\work\gitDemo> git log
commit afb6faefbee37c8c60d6b85ce1dd7844f98398c4 (HEAD -> main)
Author: dream <2082181572@qq.com>
Date:   Fri Oct 25 11:23:02 2024 +0800

    你好
# 可以看到我们之前提交的信息。

# 下面我们多提交几次

PS F:\work\gitDemo> git add 2.py
PS F:\work\gitDemo> git commit
Aborting commit due to empty commit message.
PS F:\work\gitDemo> git commit -m '第二次提交'
[main 670a22d] 第二次提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.py
PS F:\work\gitDemo> git log     
commit 670a22d85ebc3b4ddab6a9de21b86e5cbc6478f1 (HEAD -> main)
Author: dream <2082181572@qq.com>
Date:   Fri Oct 25 13:11:32 2024 +0800

    第二次提交

commit afb6faefbee37c8c60d6b85ce1dd7844f98398c4
Author: dream <2082181572@qq.com>
Date:   Fri Oct 25 11:23:02 2024 +0800

    你好

# 可以看到有两次提交

# 如果你想简洁一点可以使用下面的指令
PS F:\work\gitDemo> git reflog
670a22d (HEAD -> main) HEAD@{0}: commit: 第二次提交
afb6fae HEAD@{1}: commit (initial): 你好

版本回退 —— git reset

PS F:\work\gitDemo> git reset HEAD@{1}
error: unknown switch `e'
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] [<tree-ish>] [--] <pathspec>...
   or: git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]
   or: git reset --patch [<tree-ish>] [--] [<pathspec>...]
   or: DEPRECATED: git reset [-q] [--stdin [-z]] [<tree-ish>]

    -q, --[no-]quiet      be quiet, only report errors
    --no-refresh          skip refreshing the index after reset
    --refresh             opposite of --no-refresh
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    --[no-]recurse-submodules[=<reset>]
                          control recursive updating of submodules
    -p, --[no-]patch      select hunks interactively
    -N, --[no-]intent-to-add
                          record only the fact that removed paths will be added later
    --[no-]pathspec-from-file <file>
                          read pathspec from file
    --[no-]pathspec-file-nul
                          with --pathspec-from-file, pathspec elements are separated with NUL character
    -z                    DEPRECATED (use --pathspec-file-nul instead): paths are separated with NUL character
    --[no-]stdin          DEPRECATED (use --pathspec-from-file=- instead): read paths from <stdin>

PS F:\work\gitDemo> git ls-files      
1.py

# 我们回退到版本 HEAD@{1} 后发现,仓库中只有1.py文件了

尾言

开始写之前,我以为git东西不多。就想着多举点例子。没想到越写越多。代码加文字已经 5k 了。git的版本管理,分支管理,github的上传于拉取,vscode使用git之后再讲。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值