安装git
1.windows系统
官网下载对应应用程序安装即可
2.linux系统
centos yum -y install git
ubuntu apt-get install git
配置信息
1.配置用户信息
1 2 3 4 5 6
| # 设置全局参数 git config --global user.name <用户名> git config --global user.email <邮箱> # 设置仓库内参数 git config --global user.name <用户名> git config --global user.email <邮箱>
|
仓库创建
1.本地初始化仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| git init
git init <仓库名>
-b <branch-name>
--shared[=(false|true|umask|group|all|world|everybody|<perm>)]
|
2.从远程拉取仓库
1 2 3 4
| git clone <仓库地址>
git clone --branch <分支名> --single-branch <仓库地址>
|
本地仓库管理
1.暂存区管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git add <filename>
git add *.append
git add .
git restore <file>
git restore --staged <file>
git rm <filename>
git mv <filename>
|
2.提交快照至本地仓库
1 2 3
|
git commit -m "message"
|
3.回退版本
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 35 36 37 38 39 40
|
git reset HEAD
git reset HEAD^
git reset <commit id>
git reset --soft git reset --mixed git reset --hard git reset --merge git reset --keep
git revert git commit -m "message about this revert"
git revert <commit id> git commit -m "message about this revert"
|
4.检视命令
1 2 3 4 5 6 7 8 9 10
| git status
git diff
git diff HEAD git diff <branch name> git diff <commit id>
git show <commit id>
|
同步管理
1.分支管理
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
| git branch git branch -r git branch -a git brance -vv
git branch <new branch> git branch <new branch> <origin branch> git branch <new branch> <repoName>/<origin branch>
git branch -d <branchName>
git branch -m <old branch> <new branch>
git checkout <branchName> git checkout -b <branchName> git switch <branchName>
git branch -r
git remote update <repoName> -p*
git push <repoName> <local branch>:<repo branch> git push origin --delete <branchName>
|
2.标签管理
标签即一条分支上某个commit的别名,需要使用该commit id时均可以使用其对应的tag名作为替代
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git tag git tag -ln
git tag <tagName> git tag <tagName> <commit id>
git tag -d <tagName>
git checkout <tagName>
git push <repoName> <tagName> git push <repoName> --tags git push <repoName> :refs/tags/<tagName>
|
远程仓库管理
1.关联远程仓库
1 2 3 4 5 6
| git remote add origin <repo address>
git remote rename <old> <new>
git remote add <repoName>
|
2.同步操作
拉取相关
1 2 3 4 5 6 7 8 9 10 11 12
|
git fetch <repoName>
git pull git pull <repoName> <branchName> git pull <repoName> <repo branch>:<local branch> git pull <repoName> <repo branch>:<local branch> --rebase
|
推送相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
git push origin master git push <repoName> :<branchName> git push <repoName> <local branch>:<repo branch>
git push -u origin master
git push --all origin
git push --force origin
|
冲突管理
1.合并分支-merge
1 2 3 4 5 6 7 8 9
| git merge <branchName>
|
2.分支换基-rebase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git rebase <branch for new base> <branch need rebase>
git rebase --continue | --skip | --abort | --quit | --edit-todo
|
子仓库管理
1.添加子仓库
1 2 3 4 5
| git submodule add <仓库地址> <路径>
|
2.删除子仓库
3.加载子仓库
1 2
| git submodule update --init --recursive
|
工作区常用git文件
.gitkeep文件
由于git无法追踪空文件夹,需要在git中保留某空目录时,可以在目录下新建一个.gitkeep文件,保证git仓库不会忽略该空目录
.gitignore文件
项目中许多临时生成的文件不需要被git仓库追踪,因此通过在某目录的.gitignore文件中写入规则,帮助git忽略对这个目录下某些命名方式文件的追踪,以下是示例语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # gitignore基本语法 # 用#表示注释 # 用*表示匹配0个或任意多个字符 # 生效范围是.gitignore文件所处目录下的所有子目录
# 忽略特定目录 /node_modules
# 忽略特定名称文件 config.ini # 忽略所有名为config.ini的文件 doc/*.txt # 忽略doc/notes.txt但不包括 doc/server/arch.txt /TODO # 仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
# 忽略特定后缀名的所有文件 *.log *.o !main.o # 表示main.o除外
# 忽略所有文件 !.gitkeep
# 注: # push之后创建的.gitignore文件将不在生效
|
.gitattributes
参考: .gitattributes 作用详细讲解(git大佬必会技能)_StarJava_的博客-CSDN博客_gitattributes
gitattributes 是一个文本文件,文件中的一行定义一个路径的若干个属性,主要用于定义每种文件的属性,以方便 git 帮我们统一管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| ### 基本格式 ### # 匹配的文件模式 属性1 属性2 ... ### 可设置属性 ### # text 视为文本文件 # -text 不视为文本文件 # eol=lf 行末字符(eol)为lf————入库时将行尾规范为LF(回车),检出时行尾不强制转换为CRLF(换行、回车) # eol=crlf 行末字符(eol)为crlf入库时将行尾规范为LF(回车),检出时将行尾转换为CRLF(换行、回车) # diff 强制视为文本文件,即使它包含一些通常从不会出现在文本文件的字节值,例如NUL # !diff 表示为非文本文件,没有设置diff属性的路径会生成differ二进制文件(如果启用了二进制补丁,会生成二进制补丁)
### 常用语法 ### # 定义js jsx json后缀名的文件,其 *.js eol=lf *.jsx eol=lf *.json eol=lf # 对于txt后缀的文件,标记为文本文件 *.txt # 对于sh后缀的文件,标记为文本文件,并规定其行末字符(eol)为lf *.sh text eol=lf # 对于vcproj后缀的文件,标记为文本文件,并规定其行末字符(eol)为crlf *.vcproj text eol=crlf # 对于jpg后缀的文件,标记为非文本文件 *.jpg -text
|