今天是Git系列课程第三课,前两课我们都是在做Git仓库准备工作,今天痞子衡要讲的是Git本地提交前的准备工作。
本地有了仓库,我们便可以在仓库所在目录下做文件增删改操作,这些操作默认都发生在Git工作区内,Git并不会主动管理。如果希望Git能够管理这些变动,你需要主动通知Git。共有3种通知Git的命令,痞子衡为大家一一讲解。
1.查看Git空间文件改动状态git status
前面讲过Git空间内文件改动有4种状态,除了Unmodified状态的文件之外,其他文件改动状态都可以通过git status来查看。让我们开始在工作区创建3个文件:main.c、test.c、dummy.c
dummy.c(路径:/gittest/app/dummy.c)为空白文件。
test.c(路径:/gittest/app/test.c)文件内容如下:
#include <stdio.h>
#include <stdlib.h>
void test(void)
{
printf("this is test\n");
}
main.c(路径:/gittest/main.c)文件内容如下:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("hello world\n");
return 0;
}
新建了3个文件,让我们来看看git记录的状态,从下面结果可知,新增的3个文件在Git空间里都属于Untracked文件,存放在工作区内。
[email protected] MINGW64 /d/my_project/gittest (master)
$ git statusOn branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) app/ main.c
2.将工作区文件改动添加到暂存区git add
git add是第一种通知Git命令,这个命令用于告诉Git我们新增了文件改动,被git add命令操作过的文件(改动)便会处于Git暂存区。
2.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 statusOn 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文件里的内容全部删除,再新增一个名叫duty.c的文件。
[email protected] MINGW64 /d/my_project/gittest (master)
$ git statusOn 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/duty.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 statusOn 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/duty.c new file: app/test.c new file: main.c
3.将暂存区文件改动退回git rm
git rm是第二种通知Git命令,这个命令用于告诉Git我们想把之前用git add添加的文件改动从Git暂存区里拿出去,不需要Git记录了。拿出去有两种拿法,一种是从暂存区退回到工作区,另一种是直接丢弃文件改动。让我们试着将test.c退回到工作区,duty.c直接丢弃。
3.1退回文件改动到工作区git rm --cache [file path]
[email protected] MINGW64 /d/my_project/gittest (master)
$ git rm --cache app/test.crm 'app/test.c'
[email protected] MINGW64 /d/my_project/gittest (master)
$ git statusOn 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/duty.c new file: main.c Untracked files: (use "git add <file>..." to include in what will be committed) app/test.c
3.2丢弃文件改动git rm -f [file path]
[email protected] MINGW64 /d/my_project/gittest (master)
$ git rm -f app/duty.crm 'app/duty.c'
[email protected] MINGW64 /d/my_project/gittest (master)
$ git statusOn 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/
4.将暂存区文件移动位置/重命名git mv
git mv是第三种通知Git命令,这个命令用于告诉Git我们想把之前用git add添加的文件直接在暂存区里重新命名或移动到新位置。让我们试着将main.c重命名为app.c,并移动到app/文件夹下。
4.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 statusOn 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/
4.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 statusOn 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