痞子衡嵌入式:第一本Git命令教程- 转移(add/rm/mv)

Posted 痞子衡嵌入式

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了痞子衡嵌入式:第一本Git命令教程- 转移(add/rm/mv)相关的知识,希望对你有一定的参考价值。


  今天是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分别添加到暂存区
[email protected] MINGW64 /d/my_project/gittest (master)
$ git add main.c

[email protected] MINGW64 /d/my_project/gittest (master)
$ git add app/test.c

[email protected] MINGW64 /d/my_project/gittest (master)
$ git add app/dummy.c

// 查看此时的文件状态,3个文件都已在暂存区中了
[email protected] 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的文件。

[email protected] 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是这两个命令的并集。

[email protected] MINGW64 /d/my_project/gittest (master)
$ git add -A app/

[email protected] 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]

[email protected] MINGW64 /d/my_project/gittest (master)
$ git rm --cache app/test.c

rm 'app/test.c'

[email protected] 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]

[email protected] MINGW64 /d/my_project/gittest (master)
$ git rm -f app/trash.c

rm 'app/trash.c'

[email protected] 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]

[email protected] MINGW64 /d/my_project/gittest (master)
$ git mv main.c app.c

[email protected] 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]

[email protected] MINGW64 /d/my_project/gittest (master)
$ git mv app.c app/

[email protected] 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

以上是关于痞子衡嵌入式:第一本Git命令教程- 转移(add/rm/mv)的主要内容,如果未能解决你的问题,请参考以下文章

痞子衡嵌入式:第一本Git命令教程(7.1)- 清理之缓存(stash)

痞子衡嵌入式:第一本Git命令教程- 提交(commit/format-patch/am)

痞子衡嵌入式:极精简的Git命令教程- 连接(remote/clone)

痞子衡嵌入式:极精简的Git命令教程- 准备(init/config/.gitignore)

《痞子衡嵌入式半月刊》 第 75 期

《痞子衡嵌入式半月刊》 第 2 期