Java教程

Git学习笔记

本文主要是介绍Git学习笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Git安装部署

[root@master ~]# yum install git -y
[root@master ~]# git config
usage: git config [options]

Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object
[root@master ~]# git config --global user.name 'vampire'
[root@master ~]# git config --global user.email '2733885168@qq.com'
[root@master ~]# git config --global color.ui true  # 语法高亮
# 配置用户
[root@master ~]# git config --list
user.name=vampire
user.email=2733885168@qq.com
color.ui=true

[root@master ~]# cat .gitconfig 
[user]
        name = vampire
        email = 2733885168@qq.com
[color]
        ui = true

git初始化

初始化工作目录、对已经处在的目录进行初始化

[root@master ~]# mkdir data
[root@master data]# git init  # 初始化仓库(可以时空目录)
Initialized empty Git repository in /root/data/.git/

# 查看工作区的状态
[root@master data]# git status
# On branch master  # 分支 主
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
隐藏文件
branches : 分支目录
config: 定义项目特有的配置选项
description: 仅供git web程序使用
HEAD: 指示当前的分支
hooks: 包含git钩子文件
info: 包含一个全局排除文件(exclude文件)
objects: 存放所有数据内容,有info和pack两个子文件夹
refs: 存放指向数据(分支)的提交对象的指针
index: 保存暂存区信息,在执行git init的时候,这个文件还没有
[root@master data]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

git操作

git add files  # 添加文件到暂存区
git add . 或者git add * 添加当前所有文件到
[root@master data]# git add .
[root@master data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   a
#       new file:   b
#       new file:   c
1.# 暂存区撤回
[root@master data]# git rm --cached c
rm 'c
删除文件 撤出暂存区,删除文件

2. 直接从暂存区同工作目录一同删除文件命令
[root@master data]# git rm -f b

通过版本控制系统 管理文件
>1.工作目录必须有代码文件
>2.通过git add file 添加文件到暂存区域
>3.通过git commit -m "输入信息" 添加本地仓库
commit是将暂存区的文件放到本地仓库

[root@master data]# git commit -m "add newfile a"
[master (root-commit) f5a29a0] add newfile a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
改名
mv a a.txt
git status
git rm --cached a
git status
git add a.txt
git status
git commit -m "modified a "
直接改名
[root@master data]# git mv a.txt a
You have new mail in /var/spool/mail/root
[root@master data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    a.txt -> a
[root@master data]# git commit -m "modified a.txt"
[master 51f6655] modified a.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a.txt => a (100%)
git 文件比对
# git diff 默认比对工作目录和暂存区有何不同
[root@master data]# git diff
比对暂存区和本地仓库
[root@master data]# git diff --cached
如果某个文件已经被仓库管理,如果在更改此文件 直接需要一条命令即可
git commit -am "add newfile"

git commit # 相当于虚拟机的镜像、任何操作都被做了一次快照,可恢复到任意一个位置

git log一行简单的显示commit信息[root@master data]# git log --oneline1b16520 add index51f6655 modified a.txt21b29cf modified af5a29a0 add newfile a[root@master data]# git log -p #显示具体内容的变化[root@master data]# git log -1 #只显示1条内容

回滚数据到某一个提交

[root@master data]# git log --oneline1b16520 add index51f6655 modified a.txt21b29cf modified af5a29a0 add newfile aYou have new mail in /var/spool/mail/root[root@master data]# git reset --hard f5a29a0HEAD is now at f5a29a0 add newfile a[root@master data]# git reflog
查看当前指针指向[root@master data]# git log --oneline --decorate1b16520 (HEAD, master) add index51f6655 modified a.txt21b29cf modified af5a29a0 add newfile a创建一个testing分支[root@master data]# git branch testingYou have new mail in /var/spool/mail/root查看当前分支[root@master data]# git branch* master  testing切换到testing分支[root@master data]# git checkout testingSwitched to branch 'testing'  创建一个分支并切换[root@master data]# git checkout -b testinghistory  分支不会影响主干touch aaa bbb cccgit add aaagit commit -m "add aaa"git add bbbgit commit -m "add bbb"git add cccgit commit -m "add ccc"git tsatusgit statusgit branch -d testinggit branchgit checkout -b testingllgit log --oneline --decoratetouch test-dddgit add .git commit -m "add test-ddd"git log --oneline --decorate

代码合并

git merge testing  (当前在主分支)

代码合并冲突

master分支touch aaaecho 123>aaagit add aaagit commit -m "add aaa"git checkout -b testingtouch aaaecho 456>aaagit add aaagit commit -m "add aaa"git checkout mastergit merge testing冲突解决 vi aaa保留什么代码 ,,重新提交git commit -am "modified aaa" 

git标签使用

标签也是指向了一次commit提交,是一个里程碑式的标签,回滚打标签直接加标签号,不需要加唯一字符串。

[root@master data]# git log --oneline1b16520 add index

-a: 指定标签的名字

[root@master data]# git tag -a v1.0 1b16520 -m "tag v1.0 add index"git show  查看具体标签的信息[root@master data]# git show v1.0tag v1.0Tagger: vampire <2733885168@qq.com>Date:   Fri Aug 6 00:15:41 2021 -0400tag v1.0 add indexcommit 1b16520864c9a51f8b74143c2f8f412cd9daf329Author: vampire <2733885168@qq.com>Date:   Thu Aug 5 21:24:56 2021 -0400    add indexdiff --git a/a b/aindex e69de29..9015a7a 100644--- a/a+++ b/a@@ -0,0 +1 @@+index[root@master data]# git reset --hard v1.0HEAD is now at 1b16520 add index[root@master data]# git tagv1.0v2.0删除 git tag -d v1.0

github使用

Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓库,还拥有了更美观的web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业

  1. 注册用户
  2. 配置ssh-key
  3. 创建项目
  4. 克隆项目到本地
  5. 推送新代码到github

添加远程仓库

[root@master data]# git remote add origin https://github.com/Healer20/learn.git[root@master data]# git remoteorigin

将本地分支代码推送到github

[root@master data]# git push -u origin masterUsername for 'https://github.com': Healer20Password for 'https://Healer20@github.com': Counting objects: 15, done.Delta compression using up to 4 threads.Compressing objects: 100% (10/10), done.Writing objects: 100% (15/15), 1.18 KiB | 0 bytes/s, done.Total 15 (delta 3), reused 0 (delta 0)remote: Resolving deltas: 100% (3/3), done.To https://github.com/Healer20/learn.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.

clone远程仓库的代码

[root@master tmp]# git clone git@git.zhlh6.cn:Healer20/learn.git# history   14  sudo yum install -y curl policycoreutils-python openssh-server perl   15  sudo systemctl enable sshd   16  sudo systemctl start sshd   17  sudo firewall-cmd --permanent --add-service=http   18  sudo firewall-cmd --permanent --add-service=https   19  sudo systemctl reload firewalld   20  sudo yum install postfix   21  sudo systemctl enable postfix   22  sudo systemctl start postfix   23  curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash   24  curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash   25  sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ce   26  cd /etc/gitlab/   27  ls   28  vi gitlab.rb    29  gitlab-ctl reconfigure   30  cat /etc/gitlab/initial_root_password

创建项目

>Git global setup
git config --global user.name "Administrator"git config --global user.email "admin@example.com"
Create a new repository
git clone git@192.168.200.66:test/git_data.gitcd git_datagit switch -c maintouch README.mdgit add README.mdgit commit -m "add README"git push -u origin main
Push an existing folder
cd existing_foldergit init --initial-branch=maingit remote add origin git@192.168.200.66:test/git_data.gitgit add .git commit -m "Initial commit"git push -u origin main
>Push an existing Git repository
cd existing_repogit remote rename origin old-origingit remote add origin git@192.168.200.66:test/git_data.gitgit push -u origin --allgit push -u origin --tags

Add_ssh KEYS

[root@master data]# git config --global user.name "kang"[root@master data]# git config --global user.email "2733885168@qq.com"

删掉原来的origin

git remote remove origin

添加远程仓库

[root@master data]# git remote add origin git@192.168.200.66:test/git_data.git[root@master data]# git remoteorigin将本地仓库的项目代码推送到gitlab[root@master data]# git push -u origin masterCounting objects: 3, done.Writing objects: 100% (3/3), 198 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@192.168.200.66:test/git_data.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.(touch aaaecho kang > aaa git add .git commit -m "echo") 

master分支保护

代码合并流程

代码合并请求

web界面操作

Maven-nexus私服配置

Maven是一个项目管理和综合工具。Maven提供给开发人员构建一个完整的生命周期框架。开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期。Apache的开源项目主要服务于JAVA平台的构建、依赖管理、项目管理。

Project 0bject Model,项目对象模型。通过xml格式保存的pom .xml文件。该文件用于管理∶源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。该文件是由开发维护,我们运维人员可以不用去关心。

下载maven
清华源: https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/
解包
[root@node ~]# tar -xf apache-maven-3.3.9-bin.tar.gz [root@node ~]# mv apache-maven-3.3.9 /usr/local/
软连接
[root@node ~]# cd /usr/local/[root@node local]# ln -s apache-maven-3.3.9/ maven
加环境变量
# vi /etc/profileexport PATH=/usr/local/maven/bin/:$PATH# source /etc/profile

安装mvn之前 需要安装java环境

[root@node ~]# yum install java-1.8.0 java-1.8.0-devel -y

测试

进入代码项目 打包
mvn package

常用命令:

maven -v   # 查看版本号validate(验证): 验证项目是否正确,并且所有必要信息可用。compile(编译): 编译项目代码test:(测试): 使用合适的单元测试框架测试编译后的源码。package(打包): 源码编译之后,使用合适的格式(例如jar格式)对编译的源码进行打包integration-test: 集成测试: 如果有需要,把包处理并部署到可以运行集成测试的环境中去。verify(验证): 进行各种测试来验证包是否有效并且符合质量标准。install(安装)︰ 把包安装到本地仓库,使该包可以作为其他本地项目的依赖。deploy(部署)︰在集成或发布环境中完成,将最终软件包复制到远程存储库,以与其他开发人员和项目共享。mvn clean(清除)︰清除上次编译的结果mvn package#会去maven的中央仓库去下载需要的依赖包和插件到.m2目录下

创建maven-nexus私服

阿里云

[root@node conf]# cp settings.xml{,.bak}[root@node conf]# vi settings.xml    159     <mirror>    160   <id>aliyunmaven</id>    161   <mirrorOf>*</mirrorOf>    162   <name>阿里云公共仓库</name>    163   <url>https://maven.aliyun.com/repository/public</url>    164   </mirror>
部署私服nexus
1.上传解包2.移动到/usr/local# mv nexus-3.13.0-01 /usr/local软连接# ln -s nexus-3.13.0-01/ nexus# cd nexus3.进入bin目录./nexus start./nexus status访问 :  ip:8081        admin        admin123
去maven配置
/usr/local/maven/conf修改 setting.xml在servers标签中添加<server>  <id>my-nexus-releases</id>  <username>admin</username>  <password>admin123</password></server>在mirrors标签中添加<mirror>  <!--This seds everying else to /public-->  <id>nexus</id>  <mirrosOf>*</mirrorOf>  <url>http://192.168.200.88:8081/nexus/centos/groups/public/</url></mirror>找到<profiles>标签 添加仓库信息<profile>      <id>nexusProfile</id>      <repositories>        <repository>			<id>central</id>			<name>111</name><url>http://localhost:8081/nexus/content/groups/public/</url>			<snapshots>				<enabled>true</enabled>			</snapshots>			<releases>				<enabled>true</enabled>			</releases>			<layout>default</layout>		</repository>      </repositories>    </profile>        激活<activeProfiles>		<!--激活了才生效-->		<activeProfile>nexusProfile</activeProfile>	</activeProfiles>

重启 nexus服务

这篇关于Git学习笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!