Git

前置知识

需要会点linux和shell才能方便进行一些基本操作

linux

  • ls(英文全拼:list files): 列出目录及文件名
  • cd(英文全拼:change directory):切换目录
  • pwd(英文全拼:print work directory):显示目前的目录
  • mkdir(英文全拼:make directory):创建一个新的目录
  • rmdir(英文全拼:remove directory):删除一个空的目录
  • cp(英文全拼:copy file): 复制文件或目录
  • rm(英文全拼:remove): 删除文件或目录
  • mv(英文全拼:move file): 移动文件与目录,或修改文件与目录的名称

shell

  • i – 切换到输入模式,在光标当前位置开始输入文本。
  • x – 删除当前光标所在处的字符。
  • : – 切换到底线命令模式,以在最底一行输入命令。
  • a – 进入插入模式,在光标下一个位置开始输入文本。
  • o:在当前行的下方插入一个新行,并进入插入模式。
  • O – 在当前行的上方插入一个新行,并进入插入模式。
  • dd – 剪切当前行。
  • yy – 复制当前行。
  • p(小写) – 粘贴剪贴板内容到光标下方。
  • P(大写)– 粘贴剪贴板内容到光标上方。
  • u – 撤销上一次操作。
  • Ctrl + r – 重做上一次撤销的操作。
  • :w – 保存文件。
  • :q – 退出 Vim 编辑器。
  • :q! – 强制退出Vim 编辑器,不保存修改。

初始化配置

1
2
3
4
5
6
7
8
git -v   

git config --global user.name "<username>"

git config --global user.email <email>

#git环境配置信息
git config --global --list

image-20240427132446217

创建仓库

1
2
3
4
5
6
7
#create a new dir to store the gitreso
mkdir <dirname>
git init
# look all resouce
ls -a
#create a new git reso in the present dir
git init <newdirname>

image-20240427133258830

git workspace and file status

git workspace

image-20240427134557468file status

image-20240427135209734

untrack *the new created file

unmodified *the unchanged git file

modified *the changed git file but haven’t add into the Staging Area

staged *the changed git file and already add into the SA

image-20240427135733187

1
2
3
4
5
6
7
8
9
10
# present gitdir status
git status
# add file to AS
git add <filename>
# add present dir's all changed file to AS
# '.' means to present dir
git add .
# add all the type of files is .txt to AS
git add *.txt

1
2
3
4
# commit the files in AS
git commit -m "<title>"
# into Vim mode to commit files in AS
git commit
1
2
#finish add and commit at onec
git commit -am ""
1
2
3
4
#search past commit record
git log
#brief commit record
git log --oneline

image-20240427195749947

image-20240427195931931

git reset

image-20240427200045013

1
2
3
4
5
6
7
8
9
10
11
12
git log --online

git reset --soft <commit_hash>
git reset --hard <commit_hash>
git reset --mixed <commit_hash>
# roolback to last version
git reset --sofe HEAD^
# roolback roolback to the last version
## to get the version number
git reflog
## roolback roolback to the last version
gir reset --hard <commit_hash>

git diff

usually use the GUI tools

but sometimes we need to learn about it to run some machine which without GUI tools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#compare diffrences between the Working directory and Staging area
git diff
#compare diffrences between the Working directory and local repository
git diff HEAD
#compare diffrences between the Staging area and local repository
git diff --cached
#compare diffrences between two version file
git diff <commit_hash1> <commit_hash2>

## two symbol
# 'HEAD' means present version
# '~' or '^' means last version
# so we can do like this to
##
git diff HEAD~ HEAD
git diff HEAD^ HEAD
# and we can input a number behind the ~ which means last N verision
git diff HEAD~2 HEAD
# check specific files diff
git diff HEAD~2 HEAD <filename>

image-20240427202747326

rm files

1
2
3
4
5
6
7
8
9
10
11
12
#just remove the files in Working dirctory but doesn's remove in AS 
rm <filename>
#so we need git add . to update the diff into AS
git add .

# or we can rm directly by use git rm
git rm <filename>

# rm files in local repository but don't rm in WD
git rm --cached <filename>
#at last we need to commit to updata diff into local repository
git commit -m ""

image-20240427204153159

.gitignore

image-20240427204329307

we can search template in github

image-20240427205948513

clone remote Repo

image-20240427210451419

image-20240427210545105