Git工作目录下对于文件的修改(增加、删除、更新)会存在几个状态,这些修改的状态会随着我们执行Git 的命令而发生变化。

添加文件至忽略列表

一般我们总会有些文件无需纳入Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动 生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以在工作目录 中创建一个名为 .gitignore 的文件(文件名称固定),列出要忽略的文件模式。下面是一个示例:

1
2
3
4
5
6
7
8
9
10
11
12
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

基础操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#####################仓库初始化######################
# 创建目录(git_test01)并在目录下打开gitbash

# 初始化git仓库
git init
#####################创建文件并提交#####################
# 目录下创建文件 file01.txt
touch file01.txt
# 将修改加入暂存区
git add .
# 将修改提交到本地仓库,提交记录内容为:commit 001
git commit -m 'commit 001'
# 查看日志
git log
####################修改文件并提交######################
# 修改file01的内容为:count=1

# 将修改加入暂存区
git add .
# # 将修改提交到本地仓库,提交记录内容为:update file01
git commit --m 'update file01'
# 查看日志
git log
# ⭐️以精简的方式显示提交记录
git-log
####################将最后一次修改还原##################
# 查看提交记录
git-log
# 找到倒数第2次提交的commitID

# 版本回退
git reset commitID --hard
# 版本回退后clear后查看之前的commitID(查看已经删除的提交记录)
git reflog

⭐️远程仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
# 关联远程仓库
git remote add origin <url>
# 删除远程仓库
git remote rm origin
# 查看远程仓库
git remote -v
# 推送提交到远程仓库
git push origin master

# ⭐️设置远程分支的关联,"feature-branch"为本地分支,通常是"master"
git push --set-upstream origin feature-branch
# 设置远程分支的关联后推送到远程分支的操作
git push