Git

168 阅读13分钟

Git

Git概述

Git是一个免费的,开源的分布式版本控制系统

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git易于学习,占地面积小,性能级快.它具有廉价的本地库,方便的暂存区域和多个工作流分支等特性.其性能优于Subversion(SVN),CVS,Perforce和ClearCase等版本控制工具.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

====================Git==================

Git介绍 分布式版本控制工具 VS 集中式版本控制工具

Git安装 基于官网发布的最新版本 安装讲解

Git命令 基于开发案例详细讲解Git常用命令

Git分支 分支特性,创建,转换,合并 代码合并冲突解决

Idea 如何使用Git(集成Git)

==================GitHub================

创建远程库

代码推送 Push

代码拉取 Pull

代码克隆 Clone

SSH免密登录

Idea 集成GitHub

==================Gitee==================

创建远程库

Idea 集成Gitee

码云链接GitHub进行代码的复制和迁移

==================GitLab=================

GitLab服务器的搭建和部署

Idea集成Gitlab

分布式版本控制工具 VS 集中式版本控制工具

集中式版本控制工具

集中式版本控制工具,如CVS,SVN等.

集中式版本控制工具是指所有的项目版本都存储在唯一的服务器中,而团队中使用者本地只保存有最新版本。因此,当服务器宕机或故障时,服务器中文件如果损坏或缺失,使用者本地只有最新版本,因此很难恢复全部版本文件记录,容易造成文件缺失。

如果多个使用者更新同一个文件,当第一个人更新并上传文件后,会提醒后续提交的人更新文件,否则无法提交。只有在更新文件后才能将自己的文件与服务器中的文件合并。依次如此执行,最终得到的最新文件就是所有人修改后的版本。

但是如果多个人同时修改了同个文件中的同一行代码或者无法进行对比的二进制文件等,比如图片等,那么在进行版本控制提交时容易产生冲突,造成无法提交的问题。此时在第一个人提交修改的文件到库中后,后续提交的人也会提示更新文件,并且会造成冲突,只有按照提示解决冲突后才能继续进行提交库,最终得到的最新文件是最后一个提交库的人解决冲突的版本。

分布式版本控制工具

分布式版本控制工具,如: Git等.

分布式版本控制工具不再有唯一的服务器,每个使用者会将整个项目文件镜像到自己的本地中,因此每个使用者的电脑就是一个服务器。这样一来,当远程代码存储平台出现故障时,也不用担心代码文件以及版本丢失的问题。

分布式版本控制工具解决了部分集中式版本控制工具的缺陷和问题:

在断网的情况下依旧可以进行开发(所有版本的代码本地都有)

使用GitLab进行团队协作,哪怕GitLab平台挂了,每个使用者本地也都存储着完整的项目代码。

Git安装

基本没有任何难度,一路NEXT即可.

Git常用命令

1.初始化本地库

1)基本语法

git init

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo
$ git init
Initialized empty Git repository in E:/git/git-demo/.git/
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ ll
total 0Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 Administrator 197121 0 Aug 10 18:33 ./
drwxr-xr-x 1 Administrator 197121 0 Aug 10 18:31 ../
drwxr-xr-x 1 Administrator 197121 0 Aug 10 18:33 .git/
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ cd .git
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo/.git (GIT_DIR!)
$ ll -a
total 11
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 ./
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 ../
-rw-r--r-- 1 Administrator 197121  23 Aug 10 18:33 HEAD
-rw-r--r-- 1 Administrator 197121 112 Aug 10 18:33 config
-rw-r--r-- 1 Administrator 197121  73 Aug 10 18:33 description
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 hooks/
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 info/
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 objects/
drwxr-xr-x 1 Administrator 197121   0 Aug 10 18:33 refs/
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo/.git (GIT_DIR!)

2.查看本地库状态

1)基本语法

git status
// others
vim filename //新建文件
vim edit ===> i
vim esc ===> Esc
vim copy ===> yy
vim paste ===> p
vim save ===> :wq
vim nosave ===> :q
cat filename //查看文件
tail -n number filename //倒序查看文件 number行数

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git status
On branch master //分支 master
​
No commits yet //没有提交过任何过文件
​
nothing to commit (create/copy files and use "git add" to track)
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)

3.添加暂存区

1)基本语法

git add filename
//others
git rm --cached filename //从暂存区删除文件

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git status
On branch master
​
No commits yet
​
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
​
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)

4.提交本地库

1)基本语法

git commit -m "instr" filename
//if appear *** please tell who you are.
git config --global user.email "your's email" //设置邮箱
git config --global user.name "your's name" //设置名称

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git status
On branch master
​
No commits yet
​
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
​
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git commit -m "My first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) d7e9487] My first commit
 1 file changed, 21 insertions(+)
 create mode 100644 hello.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)

5.查看日志

1)基本语法

git reflog
git log

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git reflog
d7e9487 (HEAD -> master) HEAD@{0}: commit (initial): My first commit
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git log
commit d7e948791ddfdcef5261c76c21a8dde03fc42ee4 (HEAD -> master)
Author: huoyue <836141836@qq.com>
Date:   Wed Aug 10 18:57:32 2022 +0800
​
    My first commit
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)

6.版本穿梭

1)基本语法

git reset --hard version 

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git reset --hard d7e9487
HEAD is now at d7e9487 My first commit
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)
$ git reflog
d7e9487 (HEAD -> master) HEAD@{0}: reset: moving to d7e9487
473395f HEAD@{1}: reset: moving to 473395f
473395f HEAD@{2}: commit: My third commit
dc5e4f6 HEAD@{3}: commit: My second commit
d7e9487 (HEAD -> master) HEAD@{4}: commit (initial): My first commit
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/git/git-demo (master)

Git分支

什么是分支?

在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支.使用分支意味着程序员可以把之际的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行,对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本.(分支底层其实也是指针的调用)

顾名思义,分支就是从主线上分离出来进行另外的操作,而又不影响主线,主线又可以继续干它的事,是不是有点像线程,最后分支做完事后合并到主线上而分支的任务完成可以删掉了。这样是不是很方便,主线继续做它的事,分支用来解决临时需求,二者互不相干。

git的分支功能特别的强大,它不需要将所有数据进行复制,只要重新创建一个分支的指针指向你需要从哪里开始创建分支的提交对象(commit),然后进行修改再提交,那么新分支的指针就会指向你最新提交的这个commit对象,而原来分支的指针则指向你原来开发的位置,当你在哪个分支开发,HEAD就指向那个分支的最新提交对象commt。没弄清楚没关系,先有这么一个概念,后面慢慢就会弄清的。

分支的好处

同时可以并行推进多个功能开发,提高开发效率

各个分支在开发工程中,如果某一个分支开发失败,不会对其他分支有任何影响.

分支的操作

git branch branchname --创建本地分支
git branch -v --查看分支
git checkout branchname --切换分支
git merge branchname --把指定的分支合并到当前分支上

查看分支

1)基本语法
git branch -v
git branch ##查看本地分支
git branch -r ##查看远程分支
git branch -a ##查看所有分支
git branch --set-upstream-to=orgin/branchname loclbranchname ##建立远程分支和本地分支的联系 orgin/branchname 远程分支 localbranchname 本地分支
git branch -m oldbranchname newbrachname ##重命名分支 M为强制重命名
git branch -d branchname ##删除分支 分支如果有 没有合并到当前分支的内容,使用-D强制删除,并且不能删除当前分支
git branch -d -r branchname ##删除远程分支
2)案例
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git branch -v
* master fd6e33b update file
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

创建分支

1)基本语法
git branch branchname
2)案例
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git branch -v
  hot-fix fd6e33b update file
* master  fd6e33b update file
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

修改分支

1)基本语法
git branch -m oldbranchname newbrachname ##重命名分支 M为强制重命名
git branch -d branchname ##删除分支 分支如果有 没有合并到当前分支的内容,使用-D强制删除,并且不能删除当前分支

切换分支

1)基本语法
git checkout branchname
2)案例
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)

合并分支

1)基本语法
git merge branchname
2)案例
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git merge hot-fix
Updating fd6e33b..cd9f594
Fast-forward
 helloworld.txt | 1 +
 1 file changed, 1 insertion(+)
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

合并冲突

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ vim helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git add helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git commit -m "master edit" helloworld.txt
[master 7b5fc7a] master edit
 1 file changed, 1 insertion(+)
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ cat helloworld.txt
hello world this is git's world!
I am editing this file.
Test branch checkout.
master edit.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ vim helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git add helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git commit -m "hot-fix edit" helloworld.txt
[hot-fix d1fdd41] hot-fix edit
 1 file changed, 1 insertion(+)
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git merge hot-fix
Auto-merging helloworld.txt
CONFLICT (content): Merge conflict in helloworld.txt
Automatic merge failed; fix conflicts and then commit the result.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
​
Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   helloworld.txt
​
no changes added to commit (use "git add" and/or "git commit -a")
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ vim helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
​
Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   helloworld.txt
​
no changes added to commit (use "git add" and/or "git commit -a")
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git add helloworld.txt
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)
​
Changes to be committed:
        modified:   helloworld.txt
​
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git commit -m "merge test" helloworld.txt
fatal: cannot do a partial commit during a merge.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master|MERGING)
$ git commit -m "merge test"
[master a7ae4bf] merge test
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ cat helloworld.txt
hello world this is git's world!
I am editing this file.
Test branch checkout.
master edit.
hot-fix edit.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ cat helloworld.txt
hello world this is git's world!
I am editing this file.
Test branch checkout.
hot-fix edit.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git merge master
Updating d1fdd41..a7ae4bf
Fast-forward
 helloworld.txt | 1 +
 1 file changed, 1 insertion(+)
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ cat helloworld.txt
hello world this is git's world!
I am editing this file.
Test branch checkout.
master edit.
hot-fix edit.
​
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)

Git团队协作机制

团队内协作

团队内协作机制大概如下:

  • 首先主用户创建一个本地库
  • 主用户将代码push到用户托管中心的远程库
  • 用户2将远程库中的代码clone到自己的本地库
  • 用户2将该代码进行修改
  • 用户2将修改后的代码重新push到远程库中
  • push成功需要得到主用户的同意,用户2需要是团队的一员
  • 主用户可以将修改后的代码拉取下来
  • 更新本地库的代码

跨团队协作

跨团队协作机制大概如下:

  • 团队1将自己的代码fork给团队2
  • 团队2将团队1的代码复制一份到自己的远程库
  • 团队2将自己远程库中的团队1代码clone下来到本地库
  • 团队2在自己的本地库中对代码进行修改
  • 修改后将代码保存
  • 推送到团队2的远程库
  • 团队2给对方发送一个拉取请求
  • 团队1接收该请求
  • 对团队2修改后的代码进行审核
  • 审核通过的话将代码合并到自己的远程库
  • 团队1将远程库中的代码拉取到各自本地库中进行学习

GitHub操作

// github.com/xiaoHuoTong…

创建远程库

登录GitHub之后点击有上角加号,选择new repository 配置之后 create即可

创建远程库别名

1)相关命令

git remote -v ##查看当前所有远程库别名
git remote add name address ##创建别名

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git remote -v

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git remote add git-demo https://github.com/xiaoHuoTongZhi/git-demo.git

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)
$ git remote -v
git-demo        https://github.com/xiaoHuoTongZhi/git-demo.git (fetch)
git-demo        https://github.com/xiaoHuoTongZhi/git-demo.git (push)

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (hot-fix)

推送本地库到远程库

1)相关命令

git push resname(name/address) localbranch
git config --global http.sslVerify "false" ##解决 errno 10054
ssh-keygen -t rsa ##创建秘钥
cat .ssh/id_rsa.pub ##查看秘钥

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git push git-demo master
fatal: unable to access 'https://github.com/xiaoHuoTongZhi/git-demo.git/': OpenSSL SSL_read: Connection was reset, errno 10054

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git push git-demo master
fatal: unable to access 'https://github.com/xiaoHuoTongZhi/git-demo.git/': Failed to connect to github.com port 443 after 21045 ms: Timed out

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:RCfTp+NljdVBgTWsPp1WQZiNZqY3pziKiSaHn0eMOk0 Administrator@XTTD-2022ZQBABL
The key's randomart image is:
+---[RSA 3072]----+
|        +..   @Oo|
|       . +. .O.+o|
|        .  o=+. .|
|       .  o.++...|
|      o S. ++ = o|
|     E o  .o + + |
|    = o o . . o  |
|   = =.+ .       |
|    *o.          |
+----[SHA256]-----+

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ cat .ssh/id_rsa.pub
Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git push git-demo master
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 4 threads
Compressing objects: 100% (19/19), done.
Writing objects: 100% (21/21), 1.73 KiB | 591.00 KiB/s, done.
Total 21 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/xiaoHuoTongZhi/git-demo.git
 * [new branch]      master -> master

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

3)问题

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git push git-demo master
fatal: unable to access 'https://github.com/xiaoHuoTongZhi/git-demo.git/': OpenSSL SSL_read: Connection was reset, errno 10054

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

解决:

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git config --global http.sslVerify "false"

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

创建秘钥并填写到github上

拉去远程库到本地库

1)相关命令

git pull <远程主机名> <远程分支名>:<本地分支名>

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ ll
total 2
-rw-r--r-- 1 Administrator 197121 294 Aug 10 19:24 hello.txt
-rw-r--r-- 1 Administrator 197121 111 Aug 17 10:01 helloworld.txt

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ git pull git-demo master:master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 717 bytes | 19.00 KiB/s, done.
From https://github.com/xiaoHuoTongZhi/git-demo
   a7ae4bf..858d3c0  master     -> master
   a7ae4bf..858d3c0  master     -> git-demo/master
warning: fetch updated the current branch head.
fast-forwarding your working tree from
commit a7ae4bf9d8ac5c3073475b6c3b789a6d1ae8787e.
Already up to date.

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)
$ ll
total 3
-rw-r--r-- 1 Administrator 197121 294 Aug 10 19:24 hello.txt
-rw-r--r-- 1 Administrator 197121  39 Aug 17 14:31 hellogithub.txt
-rw-r--r-- 1 Administrator 197121 111 Aug 17 10:01 helloworld.txt

Administrator@XTTD-2022ZQBABL MINGW64 /e/486-git/git-demo (master)

克隆远程库到本地

1)相关命令

git clone address

2)案例

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git
$ git clone https://github.com/xiaoHuoTongZhi/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 24 (delta 6), reused 20 (delta 5), pack-reused 0
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (6/6), done.

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git
$ ll
total 0
drwxr-xr-x 1 Administrator 197121 0 Aug 17 14:56 git-demo/

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git
$ ll -a
total 8
drwxr-xr-x 1 Administrator 197121 0 Aug 17 14:56 ./
drwxr-xr-x 1 Administrator 197121 0 Aug 17 14:43 ../
drwxr-xr-x 1 Administrator 197121 0 Aug 17 14:47 .ssh/
drwxr-xr-x 1 Administrator 197121 0 Aug 17 14:56 git-demo/

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git
$ cd git-demo/

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git/git-demo (master)
$ git remote
origin

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git/git-demo (master)
$ git remote -v
origin  https://github.com/xiaoHuoTongZhi/git-demo.git (fetch)
origin  https://github.com/xiaoHuoTongZhi/git-demo.git (push)

Administrator@XTTD-2022ZQBABL MINGW64 /e/983-git/git-demo (master)

ssh免密登录

1)相关命令

ssh-keygen -t rsa -C "instr"

author ===> settings ===> SSH and GPG keys

IDEA集成Git

配置Git忽略文件

创建忽略规则文件xxxx.ignore(前缀名随便起,建议是git.ignore) 这个文件的存放位置原则上在哪里都可以,为了便于让~/.gitconfig文件引用,建议也放在用户家目录下 git.ignore文件模板内容如下

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java(J2ME)
.mtj.tmp/

# Package Files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.classpath
.project
.setting
target
.idea
*.iml

在.gitconfig文件中引用忽略配置文件(此文件在Windows的家目录中)

[user]
	name = Layne
	email = Layne@qq.com
[core]
	excludesfile = C:/Users/muxu/git.ignore
注意:这里要使用"正斜线(/)",不要使用"反斜线"

idea关联git

image-20220818144733697.png

idea集成Git初始化本地库

image-20220818145059316.png

idea集成Git添加项目

image-20220818145315273.png

idea集成Git提交项目

image-20220818145429267.png

image-20220818145610972.png

idea集成Git切换版本

image-20220818150638838.png

idea集成Git创建分支

image-20220818150825425.png

idea集成Git切换分支

image-20220818150941765.png

idea集成Git合并分支

image-20220818151030733.png

IDEA集成GitHub

同Idea集成git,不过需要到VersionControl下选择Github,添加自己的Github账号

Gitee

同Idea集成git,不过需要到VersionControl下选择gitee,添加自己的gitee账号

自检代码托管平台GitLab

方法一:

  1. 下载rpm安装包

    wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm
    
  2. 文件提权

    chmod 777 gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm
    
  3. 安装

    rpm -ivh gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm
    

    提示缺少“policycoreutils-python”依赖

    yum -y install policycoreutils-python
    
  4. 更改配置

    vim /etc/gitlab/gitlab.rb
    

    gitllab内部包含的模块会占用80(nginx)、8082(sidekiq)以及9090(prometheus)端口,可能会系统默认的端口产生冲突,因此建议修改默认端口配置:

    external_urlhttp://124.223..:8083/gitlab’
    unicorn[‘port’] = 8081
    
  5. 重启服务

    gitlab-ctl reconfigure ##初始化
    gitlab-ctl restart ##重启
    gitlab-ctl status ##查看状态
    

方法二:Centos7.5傻瓜式安装Gitlab

一、 安装并配置必要的依赖关系

在CentOS系统上安装所需的依赖:ssh、防火墙、postfix(用于邮件通知) 、wget、以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问。

1.安装ssh

yum install -y curl policycoreutils-pythonopenssh-server

2.将SSH服务设置成开机自启动,安装命令:sudo systemctl enable sshd

3.启动SSH服务,安装命令:sudo systemctl start sshd

4。安装防火墙

yum install firewalld systemd -y

5.开启防火墙,安装命令:service firewalld start

6.添加http服务到firewalld,pemmanent表示永久生效,若不加--permanent系统下次启动后就会失效。

firewall-cmd --permanent --add-service=http

7.重启防火墙,安装命令:systemctl reload firewalld

8.接下来,安装Postfix以发送通知邮件,安装命令:yum install postfix

9.将postfix服务设置成开机自启动,安装命令:systemctl enable postfix

10.启动postfix,安装命令:systemctl start postfix

在安装Postfix期间,可能会出现配置屏幕。选择“Internet Site”并按enter键。使用您的服务器的外部DNS以“mail name”并按enter。如果出现额外的屏幕,继续按enter键接受默认值。

11.wget 用于从外网上下载插件

检查系统中是否已经安装wget -V,使用命令若出现下图wget相关版本描述则说明系统中已经安装wget 若报系统找不到命令说明wget未安装

若wget未安装则进行安装,安装命令:yum -y install wget

12.安装vim编辑器 安装命令:yum install vim -y

13.安装policycoreutils-python 安装命令:yum -y install policycoreutils-python

二、添加GitLab镜像源并安装gitlab服务器

1.添加gitlab镜像

wget mirrors.tuna.tsinghua.edu.cn/gitlab-ce/y…

2.安装gitlab 安装命令:rpm -i gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm

进入编辑器后按“i”键进入编辑状态,ESC键退出编辑状态

退出并保存,命令输入“:wq”

ps:注意这里设置的端口不能被占用,默认是8080端口,如果8080已经使用,请自定义其它端口,并在防火墙设置开放相对应得端口

4.重置并启动GitLab

执行:

编译命令:gitlab-ctl reconfigure

Gitlab重启命令:gitlab-ctl restart

6.访问 GitLab页面

如果没有域名,直接输入服务器ip和指定端口进行访问,初始账户: root 初始无密码:第一次登录修改密码

方法三:官网方法

1. 安装和配置必须的依赖项

在 CentOS 7上,下面的命令也会在系统防火墙中打开 HTTP、HTTPS 和 SSH 访问。这是一个可选步骤,如果您打算仅从本地网络访问极狐GitLab,则可以跳过它。

sudo yum install -y curl policycoreutils-python openssh-server perl
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld

(可选)下一步,安装 Postfix 以发送电子邮件通知。如果您想使用其他解决方案发送电子邮件

sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

在安装 Postfix 的过程中可能会出现一个配置界面,在该界面中选择“Internet Site”并按下回车。把“mail name”设置为您服务器的外部 DNS 域名并按下回车。如果还有其它配置界面出现,继续按下回车以接受默认配置。

2. 下载/安装极狐GitLab

配置极狐GitLab 软件源镜像。

curl -fsSL https://packages.gitlab.cn/repository/raw/scripts/setup.sh | /bin/bash

接下来,安装极狐GitLab。确保您已正确设置您的 DNS,并更改 gitlab.example.com 为您要访问极狐GitLab 实例的 URL。安装包将在该 URL 上自动配置和启动极狐GitLab。

对于 https 站点,极狐GitLab 将使用 Let's Encrypt 自动请求 SSL 证书,这需要有效的主机名和入站 HTTP 访问。您也可以使用自己的证书或仅使用 http://(不带s)。

如果您想为初始管理员用户(root)指定自定义密码,请查看文档。如果未指定密码,将自动生成随机密码。

执行如下命令开始安装:

sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-jh

3. 访问极狐GitLab 实例并登录

除非您在安装过程中指定了自定义密码,否则将随机生成一个密码并存储在 /etc/gitlab/initial_root_password 文件中(出于安全原因,24 小时后,此文件会被第一次 gitlab-ctl reconfigure 自动删除,因此若使用随机密码登录,建议安装成功初始登录成功之后,立即修改初始密码)。使用此密码和用户名 root 登录。

有关安装和配置的详细说明,请参阅我们的文档

4. 后续配置

完成安装后,请参考建议的后续配置,包括身份验证选项和注册限制的配置。