348 lines
8.7 KiB
Markdown
348 lines
8.7 KiB
Markdown
|
---
|
|||
|
title: git基本使用
|
|||
|
date: 2021-01-05
|
|||
|
author: ac
|
|||
|
tags:
|
|||
|
- git
|
|||
|
categories:
|
|||
|
- DevOps
|
|||
|
---
|
|||
|
|
|||
|
## 一、简介
|
|||
|
|
|||
|
`集中式版本控制工具`:不同的个客户端直接跟服务端交互工作,可以进行提交、克隆等操作。不同客户端之间是相互独立的。如:`svn`。
|
|||
|
|
|||
|

|
|||
|
|
|||
|
`分布式版本控制工具`:每个用户都有自己在本地的仓库,用来管理版本控制。每个客户端也同时是一个服务器,当然也可以用一个专门的服务器的仓库用来管理。
|
|||
|
|
|||
|

|
|||
|
|
|||
|
`Git`是一个开源的分布式版本控制工具,可以有效、高速地处理从很小到非常大的项目版本管理。`Git`起初是`Linus Torvalds`为了帮助管理`Linux`内核开发而开发的一个开源的版本控制软件。
|
|||
|
|
|||
|
优点:
|
|||
|
|
|||
|
1. 适合分布式开发,强调个体。
|
|||
|
2. 公共服务器压力和数据量都不会太大。
|
|||
|
3. 速度快、灵活。
|
|||
|
4. 相对容易处理冲突。
|
|||
|
5. 大部分操作在本地完成,不需要联网。
|
|||
|
|
|||
|
## 二、安装配置
|
|||
|
|
|||
|
**windows**:
|
|||
|
|
|||
|
一直下一步下一步默认安装完
|
|||
|
|
|||
|
**Ubuntu**:
|
|||
|
|
|||
|
```shell
|
|||
|
apt-get install git
|
|||
|
```
|
|||
|
|
|||
|
**CentOS/Redhat:**
|
|||
|
|
|||
|
```shell
|
|||
|
yum install git
|
|||
|
```
|
|||
|
|
|||
|
Git 提供了一个叫做 git config 的工具(译注:实际是 git-config 命令,只不过可以通过 git 加一个名字来呼叫此命令。)
|
|||
|
|
|||
|
配置Git环境:
|
|||
|
|
|||
|
- 全局配置:/etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。
|
|||
|
|
|||
|
```shell
|
|||
|
$ git config --system user.name "John Doe"
|
|||
|
```
|
|||
|
|
|||
|
- 用户级别配置:~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。
|
|||
|
|
|||
|
```shell
|
|||
|
# 一般使用用户级别
|
|||
|
# 创建Gitbase账号
|
|||
|
$ git config --global user.name "John Doe"
|
|||
|
$ git config --global user.email johndoe@example.com
|
|||
|
```
|
|||
|
|
|||
|
查看配置信息
|
|||
|
|
|||
|
```shell
|
|||
|
$ git config --list
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
## 三、`Git`常用命令
|
|||
|
|
|||
|

|
|||
|
|
|||
|
**拉取项目**
|
|||
|
|
|||
|
```shell
|
|||
|
#使用https 克隆
|
|||
|
$ git clone ./gitcommand.git
|
|||
|
|
|||
|
#使用ssh克隆,需要先在远程仓库中配置好ssh公钥
|
|||
|
$ git clone git@gitee.com:oschina/git-osc.git
|
|||
|
```
|
|||
|
|
|||
|
> `clone` 命令克隆了一个仓库,命令会自动将其添加为远程仓库并默认以 “origin” 为简写。
|
|||
|
|
|||
|
默认情况下,`git clone` 命令会自动设置本地 master 分支跟踪克隆的远程仓库的 `master` 分支(或其它名字的默认分支)。
|
|||
|
|
|||
|
Tip:
|
|||
|
|
|||
|
使用SSH公钥可以让你在你的电脑和码云通讯的时候使用安全连接(Git的Remote要使用SSH地址)
|
|||
|
|
|||
|
SSH Key默认在用户目录下的.ssh文件夹中,如果还没有ssh key,可以执行下面的命令生成:
|
|||
|
|
|||
|
```shell
|
|||
|
$ ssh-keygen -t rsa -C "user.email"
|
|||
|
```
|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|

|
|||
|
|
|||
|
> `id_rsa.pub`为公钥,`id_rsa`为私钥。项目中添加的是公钥。
|
|||
|
|
|||
|
**本地操作**
|
|||
|
|
|||
|
在现有的某个项目文件目录下执行:
|
|||
|
|
|||
|
```shell
|
|||
|
#初始化本地仓库
|
|||
|
$ git init
|
|||
|
```
|
|||
|
|
|||
|
会生成一个`.git`的隐藏目录(包含Git所有所需的数据和资源),开始仅仅是按照既有的结构框架初始化来里面所有的文件和目录,还没有开始跟踪管理项目中的任何一个文件。
|
|||
|
|
|||
|
```shell
|
|||
|
#将文件添加到Git的暂存区,git add -A 或 git add . 都可以提交当前仓库的所有改动
|
|||
|
$ git add .
|
|||
|
|
|||
|
#查看仓库当前文件提交状态(A:提交成功;AM:文件在添加到缓存之后又有改动)
|
|||
|
$ git status -s
|
|||
|
|
|||
|
#将暂存区提交到仓库,参数 -m 后为当次提交的备注信息
|
|||
|
$ git commit -m "提交时候的说明"
|
|||
|
|
|||
|
#查看git提交的日志
|
|||
|
$ git log
|
|||
|
```
|
|||
|
|
|||
|
`add`命令将所有文件添加到本地的缓存区,在`commit` 提交到本地仓库。
|
|||
|
|
|||
|
`add`操作之后,如何取消呢?
|
|||
|
|
|||
|
1. `git checkout `— 未git add的文件
|
|||
|
2. `git reset HEAD`— 已经git add的文件,可以用这个取消add,然后用上一条命令恢复
|
|||
|
3. `git reset –hard HEAD `—把全部更改的文件都恢复(慎用)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
**远程服务器**:
|
|||
|
|
|||
|
```shell
|
|||
|
#将本地的Git仓库信息推送上传到服务器
|
|||
|
$ git push git@gitee.com:shizishu2017/gitcommand.git
|
|||
|
```
|
|||
|
|
|||
|
> `origin`是Git给你克隆的仓库服务器的默认名称。
|
|||
|
|
|||
|
**常规操作**:
|
|||
|
|
|||
|
#### 1.设置远程仓库
|
|||
|
|
|||
|
- `git remote -v`:查看远程仓库地址(简称+详细地址)
|
|||
|
|
|||
|
```shell
|
|||
|
#查看远程仓库地址
|
|||
|
$ git remote -v
|
|||
|
origin git@gitee.com:shizishu2017/gitcommand.git (fetch)
|
|||
|
origin git@gitee.com:shizishu2017/gitcommand.git (push)
|
|||
|
```
|
|||
|
|
|||
|
- `git remote add <shortname> <url>`:添加远程仓库,`shortname`是简称。
|
|||
|
|
|||
|
```shell
|
|||
|
#添加一个仓库(场景一:git init创建了本地仓库,又想将远程仓库的代码拉取下来)
|
|||
|
$ git remote add origin 仓库地址
|
|||
|
```
|
|||
|
|
|||
|
#### 2. 更新操作
|
|||
|
|
|||
|
- `git fetch <remote>`:会抓取克隆或上次抓取后所有的新推送
|
|||
|
|
|||
|
- `git pull <remote>`:会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支(**更新操作**)
|
|||
|
|
|||
|
> `git fetch` 命令只会将数据下载到你的本地仓库,它并不会自动合并或修改你当前的工作。
|
|||
|
|
|||
|
```shell
|
|||
|
#同步远程仓库中的代码 master为分支名
|
|||
|
$ git pull 仓库地址 master
|
|||
|
|
|||
|
#(更新)拉取远程master的代码
|
|||
|
$ git pull origin master
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
#### 3.提交操作
|
|||
|
|
|||
|
- `git push <remote> <branch>`:推送到远程仓库
|
|||
|
|
|||
|
```shell
|
|||
|
#-u选项会指定一个默认主机,这样以后就可以不加任何参数使用git push
|
|||
|
$ git push -u origin master
|
|||
|
```
|
|||
|
|
|||
|
- `git remote remove <remote>`:移除远程仓库
|
|||
|
- `git remote rename <oldname> <newname>`:重命名
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#### 4.分支
|
|||
|
|
|||
|
- `git branch`:显示所有本地分支
|
|||
|
|
|||
|
- `git branch 分支名`:创建分支
|
|||
|
- `git branch -d 分支名`:删除本地分支
|
|||
|
- `git checkout 分支名`:切换分支
|
|||
|
- `git pull origin master`:拉取远程master分支
|
|||
|
- `git merge 分支名`:合并指定分支到当前分支
|
|||
|
|
|||
|
- `git push origin master`:上传代码并合并
|
|||
|
|
|||
|
- `git push origin feature-branch:feature-branch` //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)
|
|||
|
|
|||
|
```shell
|
|||
|
$ git pull
|
|||
|
$ git status
|
|||
|
$ git add .
|
|||
|
$ git commit -m '备注'
|
|||
|
$ git push
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
## 四、应用
|
|||
|
|
|||
|
在`gitlab`上创建一个git仓库(空项目)后,有三种情况上传项目:
|
|||
|
|
|||
|
- 本地没有该仓库(项目),可以使用`clone`的方式
|
|||
|
- 本地已有项目,需要上传至git仓库
|
|||
|
- 本地已存在一个仓库,但`push`的远程地址不是需要上传的地址。
|
|||
|
|
|||
|
解决方式:
|
|||
|
|
|||
|
情况一:本地没有该仓库(项目),可以使用`clone`的方式
|
|||
|
|
|||
|
```shell
|
|||
|
$ git clone git@192.168.0.2:root/tests.git
|
|||
|
$ cd tests
|
|||
|
$ touch README.md
|
|||
|
$ git add README.md
|
|||
|
$ git commit -m "add README"
|
|||
|
$ git push -u origin master
|
|||
|
```
|
|||
|
|
|||
|
情况二:本地已有项目,需要上传至git仓库
|
|||
|
|
|||
|
```shell
|
|||
|
# 初始化本地仓库
|
|||
|
$ git init
|
|||
|
|
|||
|
# 添加远程仓库
|
|||
|
$ git remote add origin git@192.168.0.105:root/tests.git
|
|||
|
|
|||
|
$ git add .
|
|||
|
$ git commit -m "Initial commit"
|
|||
|
$ git push -u origin master
|
|||
|
```
|
|||
|
|
|||
|
情况三:本地已存在一个仓库,但`push`的远程地址不是需要上传的地址。
|
|||
|
|
|||
|
```shell
|
|||
|
cd existing_repo
|
|||
|
git remote rename origin old-origin
|
|||
|
git remote add origin git@192.168.0.105:root/tests.gitgit push -u origin --all
|
|||
|
git push -u origin --tags
|
|||
|
|
|||
|
#查看当前仓库对应的远程仓库地址
|
|||
|
$ git remote -v
|
|||
|
|
|||
|
#修改仓库对应的远程仓库地址
|
|||
|
$ git set-url origin 仓库地址
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
**分支**
|
|||
|
|
|||
|
场景:有时候我们开发需要开一个分支,这样可以有效的并行开发.
|
|||
|
|
|||
|
开分支有两种方式:
|
|||
|
|
|||
|
+ 一种是在远程开好分支,本地直接拉下来;
|
|||
|
|
|||
|
```shell
|
|||
|
git checkout -b 本地分支名 origin/远程分支名XXX //检出远程的XX分支到本地
|
|||
|
```
|
|||
|
|
|||
|
使用这种方式会在本地仓库新建分支xxx,并自动切换到新建的分支xxx,当然了远程分支xxx的代码也拉取到了本地分支xxx中。采用这种方法建立的本地分支会和远程分支建立映射关系。
|
|||
|
|
|||
|
+ 一种是本地开好分支,推送到远程.
|
|||
|
|
|||
|
```shell
|
|||
|
$ git checkout -b feature-branch //创建并切换到分支feature-branch
|
|||
|
$ git push origin feature-branch:feature-branch //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)
|
|||
|
```
|
|||
|
|
|||
|
+ 当本地没有其他分支时,可以直接克隆分支下来
|
|||
|
|
|||
|
```shell
|
|||
|
$ git clone -b 分支名 仓库地址
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
> git fetch 命令可以刷新本地的远程分支列表
|
|||
|
|
|||
|
项目实战:
|
|||
|
|
|||
|
```shell
|
|||
|
# 1.将自己的更改提交到远程分支
|
|||
|
git push origin 分支名
|
|||
|
|
|||
|
# 2.切换到本地的dev分支
|
|||
|
git checkout dev
|
|||
|
|
|||
|
# 3.拉取远程的dev分支
|
|||
|
git pull origin dev
|
|||
|
|
|||
|
# 4.合并远程的分支到dev分
|
|||
|
git merge origin 分支名
|
|||
|
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|