git命令使用详细介绍

1 环境配置

设置的信息会保存在~/.gitconfig文件中

查看配置信息

shell 复制代码
git config --list
git config user.name

设置用户信息

shell 复制代码
git config --global user.name "有勇气的牛排"
git config --global user.email "1809296387@qq.com"

2 获取Git仓库

2.1 本地初始化一个仓库

shell 复制代码
git init

2.2 从远程仓库克隆

shell 复制代码
git clone 远程仓库地址https://gitee.com/zx7758/mybatis-demo01.git

3 工作目录、暂存区、版本库概念

版本库:.git隐藏文件夹的目录就是工作目录,主要用于存放开发的代码

工作目录(工作区):包含.git文件夹的目录就是工作目录,主要存放开发的代码

暂存区:.git文件夹中有很多文件,其中index文件就是暂存区,主要用于存放开发的代码

4 Git 工作目录下文件的两种状态

  • untracked 为跟踪(未被纳入版本控制)
  • tracked 以跟踪(被纳入版本控制)
  • Unmodified 未修改状态
  • Modified 已暂存状态

5 本地仓库操作

5.1 查看文件状态

shell 复制代码
git status

简洁输出

shell 复制代码
$ git status -s
?? .idea/
?? er.name
?? mybatisDemo.iml
?? pom.xml
?? src/
?? target/
?? web/

5.2 将为跟踪文件加入暂存区

shell 复制代码
git add pom.xml

查看状态

shell 复制代码
git status

5.3 取消加入暂存区

shell 复制代码
git reset pom.xml

5.4 将暂存区文件提交到本地仓库

shell 复制代码
git commit -m *

5.5 删除文件

shell 复制代码

6 远程仓库操作

6.1 查看远程仓库

列出指定的每一个远程服务器

shell 复制代码
git remote
shell 复制代码
git remote show origin

6.2 添加远程仓库

格式:git remote add <shortname> <url>

shell 复制代码
git remote add mybatis https://gitee.com/***/mybatis-demo01.git

6.3 克隆存在远程仓库

shell 复制代码
git clone https://gitee.com/***/mybatis-demo01.git

6.4 移除无效仓库

shell 复制代码
git remote rm origin

注意:此命令只是从本地移除远程仓库的记录,并不会真正影响到远程仓库

6.5 从远程仓库中抓取与拉取

shell 复制代码
git init
shell 复制代码
git remote add origin https://gitee.com/***/mybatis-demo01.git
shell 复制代码
git fetch origin master
shell 复制代码
git fetch origin/master

6.6 从远程仓库获取最新版并merge到本地仓库

shell 复制代码
git init
shell 复制代码
git remote add origin https://gitee.com/***/mybatis-demo01.git
shell 复制代码
git pull origin master

注意

如果当前本地仓库不是从远程仓库克隆,而是本地创建的仓库,并且仓库中存在文件,此时再从远程仓库拉取文件的时候会报错(fatal: refusing to merge unrelated histories )

解决方法

shell 复制代码
git pull --allow-unrelated-histories

couldn't find remote ref --allow-unrelated-histories报错解决

shell 复制代码
git pull --rebase origin master

6.7 将本地分支与远程分支关联

shell 复制代码
git branch --set-upstream-to origin/master master

6.8 推送代码到远程仓库

shell 复制代码
git push origin master

7 分支

几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。Git 的master分支并不是一个特殊分支。 它跟其它分支没有区别。 之所以几乎每一个仓库都有 master 分支,是因为git init 命令默认创建它,并且大多数人都懒得去改动它。

7.1 查看分支

列出所有本地分支

shell 复制代码
git branch

列出所有远程分支

shell 复制代码
git branch -r

列出所有本地分支和远程分支

shell 复制代码
git branch -a

7.2 创建分支

shell 复制代码
git branch
shell 复制代码
git branch one

7.3 切换分支

shell 复制代码
git checkout one

7.3 推送至远程仓库分支

shell 复制代码
git push mybatis one

7.4 合并分支

shell 复制代码
git merge one

7.5 删除分支

shell 复制代码
git beanch -d one

如果正在开发,删除会失败,强制-D

原文地址
https://www.couragesteak.com/edit#/articleId/142

相关推荐
贩卖纯净水.7 小时前
白月光git
git·github
爱吃瓜的猹z11 小时前
git reset 几点疑问
git·源代码管理
悟空201618 小时前
001、Git开发流程规范
git
Li小李同学Li18 小时前
git学习【持续更新中。。。】
git·学习·elasticsearch
晨春计19 小时前
【git】
android·linux·git
念幽20 小时前
Git常用命令
git
神技圈子20 小时前
【git系列】git中的那些迷惑的术语以及概念详解
git
benben04421 小时前
Photoshop使用方法大全
git
ou.cs1 天前
git 删除远程分支的几种写法
git
atlanteep1 天前
Linux·权限与工具-git与gdb
linux·git