前言

那么这里博主先安利一些干货满满的专栏了!

首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助。

然后就是博主最近最花时间的一个专栏《Git企业开发控制理论和实操》希望大家多多关注!


Git的远程操作

博主使用码云(Gitee)进行讲解,操作方法和Github完全一样。

理解分布式版本控制系统

我们目前所说的所有内容(工作区,暂存区,版本库等等),都是在本地!也就是在你的笔记本或者计算机上。而我们的 Git 其实是分布式版本控制系统!什么意思呢?
可以简单理解为,我们每个人的电脑上都是一个完整的版本库,这样你工作的时候,就不需要联网了,因为版本库就在你自己的电脑上。既然每个人电脑上都有一个完整的版本库,那多个人如何协作呢?比方说你在自己电脑上改了文件A,你的同事也在他的电脑上改了文件A,这时,你们俩之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

分布式版本控制系统通常也有一台充当“中央服务器”的电脑,但这个服务器的作用仅仅是用来方便“交换”大家的修改,没有它大家也一样干活,只是交换修改不方便而已。有了这个“中央服务器”的电脑,这样就不怕本地出现问题导致文件丢失了。

中央仓库我们叫“远程仓库”,有个很牛的网站叫做Github就是中央仓库的一个作用!

当然我这篇文章的读者可能有部分伙伴访问国外网站比较慢,这里博主会用国内的一个替代网站,叫做码云来进行演示。

创建远程仓库

在Gitee上新建仓库


简单理解issuespull request

Gitee这个平台,为我们提供了四种管理者的身份。

远程仓库是由自己的成员的。

这个issues就是一些有问题的人和仓库成员交流的一个地方。

pull request是什么呢?

首先对于开发者来说,肯定不是在master上进行开发的,我们可能是在dev分支上开发,然后我们就要进行merge操作。

当然,我们知道merge操作是有风险的。因此我们不能随随便便让人来merge

因此,想要进行merge的开发人,就要写一个PR(pull request),可以理解成“合并申请单”,让仓库管理人看。

管理员同意后,我们才能进行merge操作。

克隆远程仓库


这些都是Gitee给我们提供的数据传输协议,常用的就是https和ssh。

HTTPS协议clone


使用这里给的网址,复制,然后使用 git clone 命令。

git clone https://gitee.com/Yufch/remote-gitcode.git

注意:不能在任何本地仓库下执行这个命令。

这个就是远程仓库下有的东西了。用法和之前讲的完全一样。

git remote -v # 查看远程仓库的一些信息
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git remote -v
origin https://gitee.com/Yufch/remote-gitcode.git (fetch)
origin https://gitee.com/Yufch/remote-gitcode.git (push)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

表示我们是有远程仓库的fetch权限(拉权限),和push权限(推权限)。

SSH协议clone

ssh协议是采用一个公钥加密和公钥登陆的一个机制。所以我们必须要把自己本地服务器的一个公钥放到git服务器上进行管理。

我们现在还没有配置公钥。

**第一步:创建SSH Key。**在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa id_rsa.pub 这两个文件,如果已经有了,可直接跳到下一步。如果没有,需要创建SSH Key。

ls -al ~ | grep .ssh # 可以搜索一下~下有没有.ssh文件夹,如果没有就要创建SSH Key

这个命令就是系统的知识了,这里不过多讲解。

设置ssh公钥需要配置邮箱,然后这个邮箱要和码云上的一致。这里可以看。

然后配置SSH公钥命令如下。

ssh-keygen -t rsa -C "xxx.com"
(base) [yufc@ALiCentos7:~]$ ssh-keygen -t rsa -C "yufc779@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yufc/.ssh/id_rsa):
Created directory '/home/yufc/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yufc/.ssh/id_rsa.
Your public key has been saved in /home/yufc/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1PWuf6UgtkssYGh2x5QasdfaSrD4YmS7EL0Yf7VamusiQ yufc779@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
| .+.. . |
| . + . o + . . |
|..o o o * . . |
| .o+ o * . |
|oo+ B * S . |
|o+.= B o .o .. .|
|E.. . o ..oo.. ..|
| o. . o. .. .|
| .o. .. .. |
+----[SHA256]-----+
(base) [yufc@ALiCentos7:~]$

后面的东西全部默认回车即可。

(base) [yufc@ALiCentos7:~/.ssh]$ cat id_rsa.pub
ssh-rsa AAAAB3NzaasdasdfadSMIpxADoRltTaMiJadsfasdflBld/qF7EHq+KwJY4CMhb65A6tsvGPxAPAeYNgufjc4LC5r0v2hdgs0Pk86XIX7vUK6DUrpe6LPTBOdAGTsWmSbcynpJhX97mdM0P2p/3HK0KYmlymyAIzPpEyNF5YRF9goDzI6OBw7Y9WZlp0QWPyWRbnSMOe4olXKY7EFcon5uCrlD3vJNac/3ZIZpKy2TrtvPvcm1fk9sJW5JlcC7P6DByL0CG8ohxGwz2/4l yufc779@gmail.com
(base) [yufc@ALiCentos7:~/.ssh]$

这个东西就是你的公钥。弄到码云上即可。

此时就可以clone了。

git clone git@gitee.com:Yufch/remote-gitcode.git

向远程仓库推送

准备操作

本地仓库要进行一次push操作,将本地某个分支的内容推送到远程仓库中某个分支上。

然后别忘了,进去remote-gitcode仓库之后,要配置一下name和email。这个要和码云上的一致。

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.name "Yufch"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.email "yufc779@gmail.com"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git -l # 这里打错了 ...
Unknown option: -l
usage: git [--version] [--help] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=Yufch
user.email=yufc779@gmail.com
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ # 这样就配置好了

那么现在我们先创建一个文件。然后把他添加到版本库中先(上一章的内容)

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ ls
README.en.md README.md
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ vim file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ cat file.txt
hello gitee
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git add .
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git commit -m "create file.txt"
[master 7352d02] create file.txt
1 file changed, 1 insertion(+)
create mode 100644 file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
git push 命令
git push origin master:master
# origin: 远程仓库的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一个master: 要推送的本地分支
# 第二个master: 要推送到的远程仓库的分支
# 如果冒号左右两边一样,可以省略一个
git push origin master


此时就可以在远程仓库中看到我们修改的内容了。

拉取远程仓库

如果现在的情况是,远程仓库中的代码是领先于本地仓库的(可能别人开发,然后push上去了),我们想在本地看到远程仓库中的内容,我们就要进行拉取操作。

假设有人加了一行hello world,然后push上去了,我们在本地也想看到这一行。

注意:在工作中,千万不要直接改码云或者github上的代码,要改也是拉到本地然后改完去PR。

git pull origin master:master
# origin: 远程仓库的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一个master: 要拉取的远程仓库的分支
# 第二个master: 要拉取到的本地仓库的分支
# 如果冒号左右两边一样,可以省略一个
git pull origin master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git pull origin master:master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitee.com:Yufch/remote-gitcode
7352d02..8513266 master -> master
Warning: fetch updated the current branch head.
Warning: fast-forwarding your working tree from
Warning: commit 7352d02962528eae4ef12d0669a40f4f7cdf35ec.
Already up-to-date.
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

注意:拉下来的分支之后,会做一个合并的操作,所以pull = 拉取 + merge

忽略特殊文件

在日常开发中,我们有些文件不想或者不应该提交到远端,比如保存了数据库密码的配置文件,那怎么让 Git知道呢? 在 Git 工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git 就会自动忽略这些文件了。不需要从头写.gitignore文件,gitee 在创建仓库时就可以为我们生成,不过需要我们主动勾选下。

当然我们刚才创建的时候没选,我们就自己创建一个就行。

touch .gitignore

之类的意思就是让git忽略.so结尾的和.ini结尾的文件。

如下所示:按道理来说,我们创建了.gitignore,key.so,token.ini文件之后,git status会识别到工作区中有三个文件被创建,但是如下所示,git status只看到.gitignore被创建了。这就是因为我们设置了忽略。

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch token.ini
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]

此时如果git add .,刚才那两个被设置过忽略的文件是不会被提交到暂存区的。

但是在这条件下我还是想提交一个key2.so怎么办呢?可以使用以下命令。

git add -f key2.so # 强制add

但是我们一般不想去破坏 .gitignore文件的规则的,所以git还提供给我们一种语法。

这样就可以排出忽略。

然后如果有一天,你的.gitignore文件写了很长很长,然后你创建一个key3.so,你自己都忘记为什么这个被忽略掉了,可以通过一个命令查看为什么被忽略。

git check-ignore -v key.so # 查看一个文件为什么被忽略
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git check-ignore -v key.so
.gitignore:4:*.so key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

然后我们就能看到,是第四行让它被忽略了。

最后我们推送一下,远端就能看到这些文件了。

配置命令别名

如果我觉得我经常要使用git status命令,但是它太难打了,想起个别名:git st行吗?可以的。不过我们要进行一些配置。

git config --global alias.st status

**这个命令的意思就是,给status起别名为st。**是否要--global可以自选。

现在git st就能用了。

(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git st
# On branch master
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

再配一个吧。

我们以前学过这个。

git log --pretty=oneline --abbrev-commit

起个别名。

git config --global alias.lpa 'log --pretty=oneline --abbrev-commit'
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git lpa
7393ea0 add .gitignore
8513266 update file.txt.
7352d02 create file.txt
a9af182 Initial commit
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$

不建议初学的时候配置,建议大家熟悉之后,在工作中的时候再去简化。

Git企业开发控制理论和实操-从入门到深入(四)|Git的远程操作|Gitee的更多相关文章

  1. 2-6 Flutter开发环境与Android开发环境设置实操(Windows)

    通常安装完AS后,sdk的目录 C:\Users\wjw\AppData\Local\Android\sdk 如果在这个目录下没有找到sdk的目录的话 Settings里面搜索sdk,找到Androi ...

  2. SFUD+FAL+EasyFlash典型场景需求分析,并记一次实操记录

    SFUD+FAL+EasyFlash典型场景需求分析:用整个flash存储数据,上千条数据,读取得时候用easyflash很慢,估计要检索整个flash太慢了. 改进方法:分区检索. 1存数据时,根据 ...

  3. Iptables 实操

    目录 Iptables之实操 简介 名称概念: 四表中常用的表 Nat表 Filter表 iptables表和链的工作流程图 iptables过滤图 Iptables安装 Iptables 命令说明 ...

  4. 【强烈推荐,超详细,实操零失误】node.js安装 + npm安装教程 + Vue开发环境搭建

    node.js安装 + npm安装教程 + Vue开发环境搭建 [强烈推荐,超详细,实操零失误] 原博客园地址:https://www.cnblogs.com/goldlong/p/8027997.h ...

  5. mPaaS 小程序架构解析 | 实操演示小程序如何实现多端开发

    对于 mPaaS 小程序开发框架,想必读者们并不陌生.它源自于支付宝小程序框架,继承了易开发性.跨平台性及 Native 性能,不仅帮助开发者实现面向自有 App 投放小程序,还可快速构建打包,覆盖支 ...

  6. Git实操

    使用git首先要理解工作区(working).暂存区(stage或者index).和版本库(repo区),很多命令都是和这三个概念相关的. git init 初始化git仓库,会生成默认的.git文件 ...

  7. 详解git fetch与git pull的区别(实操)

    感谢原文作者:R-H-R 原文链接:https://blog.csdn.net/riddle1981/article/details/74938111 git fetch和git pull都可以将远端 ...

  8. SBT实操指南

    参考资料:1.英文官方文档2.中文官方文档,内容翻译的不全 SBT是类似maven和gradle的自动构建和包依赖管理工具,SBT是Scala技术体系下的包管理工具,都是Lightbend公司开发的, ...

  9. 【Social listening实操】作为一个合格的“增长黑客”,你还得重视外部数据的分析!

    本文转自知乎 作者:苏格兰折耳喵 ----------------------------------------------------- 在本文中,作者引出了"外部数据"这一概 ...

  10. 数据转换工具DBT介绍及实操

    一.什么是DBT dbt (data build tool)是一款流行的开源数据转换工具,能够通过 SQL 实现数据转化,将命令转化为表或者视图,提升数据分析师的工作效率.dbt 主要功能在于转换数据 ...

随机推荐

  1. IDEA 2021.2 新建JavaWeb项目及Tomcat部署

    前文:JSP 简单入门与 IDEA 开发环境配置 参考链接: https://zhuanlan.zhihu.com/p/68133583 https://www.cnblogs.com/javabg/ ...

  2. Python报错:AttributeError: type object 'str' has no attribute '_name_'(机器学习实战treePlotter代码)解决方案

    错误信息: 学习<机器学习实战>这本书时,按照书上的代码运行,产生了错误,但是在代码中没有错误提示,产生错误的代码如下: if type(secondDict[key])._name_ = ...

  3. POJ2965 The Pilots Brothers' refrigerator (精妙方法秒杀DFS BFS)

    这道题和算法进阶指南的一道题解法一样,必须另操作为奇数.见证明过程 证明:要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;可以证明,如果'+'位置对应的行和列上每一个位置都进行一次 ...

  4. LeetCode75 颜色分类 (三路快排C++实现与应用)

    三路快排是快速排序算法的升级版,用来处理有大量重复数据的数组. 主要思想是选取一个key,小于key的丢到左边,大于key的丢到右边,递归实现即可. 具体操作过程参考:https://blog.csd ...

  5. Web Components从技术解析到生态应用个人心得指北

    Web Components浅析 Web Components 是一种使用封装的.可重用的 HTML 标签.样式和行为来创建自定义元素的 Web 技术. Web Components 自己本身不是一个 ...

  6. uni-app学习笔记

    uniapp封装请求方法(含请求期间的Loading样式) https://blog.csdn.net/weixin_43242112/article/details/108019404?utm_me ...

  7. C# 排序算法2:选择排序

    选择排序法 ,是在要排序的一组数中,选出最小(或最大)的一个数与第一个位置的数交换:在剩下的数当中找最小的与第二个位置的数交换,即顺序放在已排好序的数列的最后,如此循环,直到全部数据元素排完为止. 原 ...

  8. 使用Docker部署java项目时遇到的几个错误

    0.简介 本文主要是在学习黑马程序员Docker快速入门到项目部署过程中, 对遇到的问题进行了相关的总结梳理 1.本地已存在mysql服务占用3306端口 问题 当我使用docker run -d - ...

  9. 【译】Visual Studio 2013 退役 :旧版本 Visual Studio 的支持提醒

    新年到了,也是提醒我们 Visual Studio 支持生命周期中即将到来的日期的好时机.对 Visual Studio 2013 的支持将于今年4月9日结束.如果您使用的是旧版本的Visual St ...

  10. [转帖]MySQL如何进行索引重建操作?

    MySQL如何进行索引重建操作? - 潇湘隐者 - 博客园 (cnblogs.com) 在MySQL数据库中,没有类似于SQL Server数据库或Oracle数据库中索引重建的语法(ALTER IN ...