Git使用教程
Git下载安装
通常情况下,git官网下载速度较慢,可以使用阿里的源(百度即可)。
Git配置
-
用户配置
git config --global user.name "name" git config --global user.email name@domain.com此外,可以通过命令行查询git的当前配置
git config --list git config --system -l git config --global --list
Git基本理论
工作区域
工作目录(working Directory),暂存区(stage),资源库(repository)以及远程仓库(remote repository)。文件在这四个区域之间进行转换。
Directory->Stage:git add files
Stage->History:git commit
History->Remote:git push
Remote->History:git pull
History->Stage:git reset
Stage->Directory:git checkout
Git项目搭建
workspace->index:add
index->repository:commit
repository->remote:push
remote->repository:fetch/clone
repository->workspace:checkout
remote->workspace:pull
本地仓库搭建
git init # 本地创建
git clone repo # 远程创建并clone到本地
Git文件操作
文件4种状态
- Untracked: 未跟踪, 在文件夹(工作区)中, 但未加入到git库,通过
git add使状态转变为stage. - Unmodify: 在git库中,且未修改。如果被修改,则状态转换为
Modified。使用git rm移除git库,状态转换为Untracked。 - Modified: 文件被修改。通过
git add进入stage状态。通过git checkout从库中取出快照并覆盖当前修改,从而转换为Unmodified状态。 - Staged: 暂存。通过
git commit将修改同步到库中,使库中快照与当前一致,当前文件转换为Unmodified状态。通过git reset HEAD filename取消暂存,文件状态转换为Modified。
查看文件状态
git status [filename]
git status
忽略文件
.gitignore
- 井号(#)为注释被忽略
- Linux通配符:
- 星号(*)任意多个字符
- 问号(?)一个字符
- 方括号([abd])代表可选字符范围
- 大括号({str1,str2})代表可选的字符串等
- 叹号(!)表示例外规则,不被忽略
- 路径分隔符(/)在前面表示要忽略的文件再次目录下,其子目录中的文件不忽略
- 路径分割符(/)在最后表示要忽略的是子目录
# 注释
*.txt
!lib.txt # lib.txt不被忽略
/temp # 忽略temp下的文件,不包括子目录
build/ # 忽略build下的所有
doc/*.txt # 忽略doc下的所有txt,子目录中的不被忽略
Git分支
git branch # 查看分支
git branch -r # 查看远程分支
git branch [branch-name] # 新建分支
git checkout -b [branch] # 切换分支
git merge [branch] # 合并指定分支到当前分支
git branch -d [branch] # 删除分支
git push origin --delete [branch] # 删除远程分支
git branch -dr [remote/branch] # 删除远程分支
csdvdfvfd
浙公网安备 33010602011771号