Git03-.gitignore与分支初步

149 阅读1分钟

1、.gitignore的介绍

开发时,很多文件都不需要加入版本控制系统中,那么这些文件可以加入.gitignore,让git忽略它们。

2、实例

1、创建一个文件并让git忽略掉它

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ echo "my project" > settings.properties

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt

#现在两个都被git所管理
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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:   test.txt

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

如果直接git add xxx文件,当然也可以,但是有时候会误操作,把两个文件都交进去,这肯定不是我们想要的方式。

更好的方式是使用.gitignore

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vim .gitignore

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ ls -al
total 11
drwxr-xr-x 1 16140 197609  0 Jun  3 10:12 ./
drwxr-xr-x 1 16140 197609  0 Jun  1 08:59 ../
drwxr-xr-x 1 16140 197609  0 Jun  3 10:06 .git/
-rw-r--r-- 1 16140 197609  2 Jun  3 10:12 .gitignore
-rw-r--r-- 1 16140 197609 11 Jun  3 10:05 settings.properties
-rw-r--r-- 1 16140 197609 22 Jun  3 10:06 test.txt

#然后去vi这个.gitignore,把settings.properties加入
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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:   test.txt

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

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

#然后再添加到本地版本库

2、使用匹配的表达式

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi .gitignore

#这里加上
*.b
!a.b #就是*.b都忽略,但不忽略!a.b

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ echo 'another file' > a.b

16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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:   .gitignore

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

3、通配符

image.png

/也就是一层目录,现在创建两个文件,mygit下创建file1.txt,mygit下的dir目录下创建个file2.txt,如果直接在.gitignore下写上/*.txt,只会忽略file1,如果写成/**/*.txt,那么就会忽略这两个文件。

4、分支初步

几个分支的基础指令:

#查看分支
git branch

#新建分支
git branch 新分支名

#切换分支
git checkout 分支名

#直接新建并查看分支名
git checkout -b 新分支名

#切换到上一个分支
git checkout -