加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入
  • 正文
  • 推荐器件
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

第一本Git命令教程(4) - 转移

2020/02/13
219
阅读需 13 分钟
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

今天是 Git 系列课程第四课,上一课我们在 Git 空间里做了一些文件改动并且知道了如何利用 Git 查看这些变动,今天痞子衡要讲的是将这些变动提交到 Git 本地仓库前的准备工作。

  

Git 仓库目录下的文件改动操作默认都发生在 Git 工作区内,Git 并不会主动管理。如果希望 Git 能够管理这些变动,你需要主动通知 Git。共有 3 种通知 Git 的命令(git add/rm/mv),痞子衡为大家一一讲解。

1. 将工作区文件改动添加到暂存区 git add

git add 是第一种通知 Git 命令,这个命令用于告诉 Git 我们新增了文件改动,被 git add 命令操作过的文件(改动)便会处于 Git 暂存区。

1.1 添加单文件改动 git add [file path]

上一节课我们已经在工作区创建了 3 个文件,让我们开始用 git add 将它们一一添加到暂存区:

// 将 main.c,test.c, dummy.c 分别添加到暂存区

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add main.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add app/test.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add app/dummy.c

// 查看此时的文件状态,3 个文件都已在暂存区中了

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app/dummy.c
        new file:   app/test.c
        new file:   main.c

1.2 添加文件夹内全部改动 git add -A [folder path]

有没有觉得 git add [filepath]一次只能添加一个文件不够高效?别急,你还可以按文件夹来提交。这时我们再做一些改动,将 dummy.c 文件删除,将 test.c 文件里的内容全部删除,再新增一个名叫 trash.c 的文件。

// 查看 dummy.c,test.c, track.c 状态

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app/dummy.c
        new file:   app/test.c
        new file:   main.c

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    app/dummy.c
        modified:   app/test.c

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

        app/trash.c
  

让我们试试 git add -A [folderpath],执行完这个命令后,我们可以看到 app/ 文件夹下的所有类型的文件改动(新增、修改、删除)被一次性地存储到了暂存区,这下是不是效率高了很多。有兴趣的朋友还可以继续研究 git add .(不包括删除操作)和 git add -u(不包括新增操作)两个命令,实际上 git add -A 是这两个命令的并集。

// 将 app/ 文件夹下的所有类型的文件改动全部添加到暂存区

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add -A app/

// 查看此时 Git 状态,尤其是 app/ 文件夹下的文件状态

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app/trash.c
        new file:   app/test.c
        new file:   main.c

2. 将暂存区文件改动退回 git rm

git rm 是第二种通知 Git 命令,这个命令用于告诉 Git 我们想把之前用 git add 添加的文件改动从 Git 暂存区里拿出去,不需要 Git 记录了。拿出去有两种拿法,一种是从暂存区退回到工作区,另一种是直接丢弃文件改动。让我们试着将 test.c 退回到工作区,trash.c 直接丢弃。

2.1 退回文件改动到工作区 git rm --cache [file path]

// 将 test.c 的改动从暂存区移回工作区

jay@pc MINGW64 /d/my_project/gittest (master)
$ git rm --cache app/test.c

rm 'app/test.c'

// 查看 test.c 是否已经移回到工作区

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app/trash.c
        new file:   main.c

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

        app/test.c

2.2 直接删除文件 git rm -f [file path]

// 将 track.c 文件直接从 Git 空间里删除,不留痕迹

jay@pc MINGW64 /d/my_project/gittest (master)
$ git rm -f app/trash.c

rm 'app/trash.c'
jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   main.c

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

        app/

3. 将暂存区文件移动位置 / 重命名 git mv

git mv 是第三种通知 Git 命令,这个命令用于告诉 Git 我们想把之前用 git add 添加的文件直接在暂存区里重新命名或移动到新位置。让我们试着将 main.c 重命名为 app.c,并移动到 app/ 文件夹下。

3.1 文件重命名 git mv [src file] [dest file]

// 将 main.c 在暂存区里直接改名为 app.c(app.c 也记录在暂存区)

jay@pc MINGW64 /d/my_project/gittest (master)
$ git mv main.c app.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app.c

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

        app/

3.2 移动文件位置 git mv [src file] [dest dir]

// 将 app.c 从 gittest 主目录移动到 app/ 目录下(移动操作记录在暂存区)

jay@pc MINGW64 /d/my_project/gittest (master)
$ git mv app.c app/

jay@pc MINGW64 /d/my_project/gittest (master)
$ git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   app/app.c

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

        app/test.c

推荐器件

更多器件
器件型号 数量 器件厂商 器件描述 数据手册 ECAD模型 风险等级 参考价格 更多信息
STM32F407VGT6 1 STMicroelectronics High-performance foundation line, Arm Cortex-M4 core with DSP and FPU, 1 Mbyte of Flash memory, 168 MHz CPU, ART Accelerator, Ethernet, FSMC

ECAD模型

下载ECAD模型
$20.39 查看
STM32F207IGH6TR 1 STMicroelectronics High-performance Arm Cortex-M3 MCU with 1 Mbyte of Flash memory, 120 MHz CPU, ART Accelerator, Ethernet

ECAD模型

下载ECAD模型
$69.82 查看
FT232RL 1 FTDI Chip Serial I/O Controller, CMOS, PDSO28, 5.30 X 10.20 MM, 0.65 MM PITCH, GREEN, SSOP-28

ECAD模型

下载ECAD模型
$10.08 查看

相关推荐

电子产业图谱

硕士毕业于苏州大学电子信息学院,目前就职于恩智浦(NXP)半导体MCU系统部门,担任嵌入式系统应用工程师。痞子衡会定期分享嵌入式相关文章