• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
superBugMaker
博客园    首页    新随笔    联系   管理    订阅  订阅
Git教程

Git使用教程

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种状态

  1. Untracked: 未跟踪, 在文件夹(工作区)中, 但未加入到git库,通过git add使状态转变为stage.
  2. Unmodify: 在git库中,且未修改。如果被修改,则状态转换为Modified。使用git rm移除git库,状态转换为Untracked。
  3. Modified: 文件被修改。通过git add进入stage状态。通过git checkout从库中取出快照并覆盖当前修改,从而转换为Unmodified状态。
  4. Staged: 暂存。通过git commit将修改同步到库中,使库中快照与当前一致,当前文件转换为Unmodified状态。通过git reset HEAD filename取消暂存,文件状态转换为Modified。

查看文件状态

git status [filename]
git status

忽略文件.gitignore

  1. 井号(#)为注释被忽略
  2. Linux通配符:
    • 星号(*)任意多个字符
    • 问号(?)一个字符
    • 方括号([abd])代表可选字符范围
    • 大括号({str1,str2})代表可选的字符串等
  3. 叹号(!)表示例外规则,不被忽略
  4. 路径分隔符(/)在前面表示要忽略的文件再次目录下,其子目录中的文件不忽略
  5. 路径分割符(/)在最后表示要忽略的是子目录
# 注释
*.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
posted on 2021-03-30 15:30  superBugMaker  阅读(101)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3