Git 使用学习笔记 | 青训营

36 阅读2分钟

基本概念

Git通常的操作流程

image.png

  • 版本库👉.git

    • 当我们使用git管理文件时,比如git init时,这个时候,会多一个.git文件,我们把这个文件称之为版本库。
    • .git文件另外一个作用就是它在创建的时候,会自动创建master分支,并且将HEAD指针指向master分支。
  • 工作区

    • 本地项目存放文件的位置
  • 暂存区 (Index/Stage)

    • 就是暂时存放文件的地方,通常是通过add命令将工作区的文件添加到缓冲区
  • 本地仓库(Repository)

    • 通常情况下,我们使用commit命令可以将暂存区的文件添加到本地仓库
    • 通常而言,HEAD指针指向的就是master分支
  • 远程仓库(Remote)

    • 当我们使用GitHub托管我们项目时,它就是一个远程仓库。
    • 通常我们使用clone命令将远程仓库代码拷贝下来,本地代码更新后,通过push托送给远程仓库。

常用命令

查看文件状态

git status
  • Changes not staged for commit

    • 表示工作区有该内容,但是缓存区没有,需要我们git add
  • Changes to be committed

    • 一般而言,这个时候,文件放在缓存区了,我们需要git commit
  • nothing to commit, working tree clean

    • 这个时候,我们将本地的代码推送到远端即可

配置命令

  • 列出当前配置
git config --list	
  • 列出Repository配置
git config --local --list
  • 列出全局配置
git config --global --list
  • 列出系统配置
git config --system --list
  • 配置用户名
git config --global user.name "your name"
  • 配置用户邮箱
git config --global user.email "youremail@github.com"

分支管理

  • 查看本地分支
git branch
  • 查看远程分支
git branch -r
  • 查看本地和远程分支
git branch -a
  • 从当前分支,切换到其他分支
git checkout <branch-name>
  • 创建并切换到新建分支
git checkout -b <branch-name>
  • 删除分支
git branch -d <branch-name>
  • 当前分支与指定分支合并
git merge <branch-name>
  • 查看哪些分支已经合并到当前分支
git branch --merged
  • 查看哪些分支没有合并到当前分支
git branch --no-merged
  • 删除远程分支
git push origin -d <branch-name>
  • 重命名分支
git branch -m <oldbranch-name> <newbranch-name>
  • 拉取远程分支并创建本地分支
git checkout -b 本地分支名x origin/远程分支名x

远程仓库管理

  • 连接本地仓库与远程仓库
git remote add origin
  • 修改推送源
git remote set-url origin url
  • 查看远程仓库信息
git remote -v
  • 远程仓库重命名
git remote rename old new

提交到缓冲区

  • 全部上传到缓冲区
git add .
  • 上传指定文件
git add 指定文件

提交到本地仓库

git commit -m "message"

提交到远程仓库

git push <远程仓库名> <本地分支名>:<远程分支名>

fetch

git fetch origin <branch-name>:<local-branch-name>