目录
4.4通过IDE提供的终端使用git命令更新--->提交--->推送
1.Git简介
Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。
1.1Git的特点
-
分支更快、更容易。
-
支持离线工作;本地提交可以稍后提交到服务器上。
-
Git 提交都是原子的,且是整个项目范围的,而不像 CVS 中一样是对每个文件的。
-
Git 中的每个工作树都包含一个具有完整项目历史的仓库。
-
没有哪一个 Git 仓库会天生比其他仓库更重要。
Git 是用于 Linux内核开发的版本控制工具。与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持(wingeddevil注:这得分是用什么样的服务端,使用http协议或者git协议等不太一样。并且在push和pull的时候和服务器端还是有交互的。),使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的合并跟踪(merge tracing)能力。
实际上内核开发团队决定开始开发和使用 Git 来作为内核开发的版本控制系统的时候,世界开源社群的反对声音不少,最大的理由是 Git 太艰涩难懂,从 Git 的内部工作机制来说,的确是这样。但是随着开发的深入,Git 的正常使用都由一些友好的脚本命令来执行,使 Git 变得非常好用,即使是用来管理我们自己的开发项目,Git 都是一个友好,有力的工具。现在,越来越多的著名项目采用 Git 来管理项目开发。
1.2Git 与 SVN 区别
-
GIT是分布式的,SVN不是
-
GIT把内容按元数据方式存储,而SVN是按文件
-
GIT分支和SVN的分支不同
-
GIT没有一个全局的版本号,而SVN有
-
GIT的内容完整性要优于SVN
1.3Git的缺点
-
学习资料少。
-
学习周期相对而言比较长。
-
不符合常规思维。
-
代码保密性差。
2.安装Git
2.1下载Git
肯定是装window版本
下载地址:Git - Downloads(官网)
2.2下载后安装
下载后安装无脑下一步即可
右击电脑文件空白区域有此两个选项即代表安装成功!
2.3创建项目
步骤省略......
2.4创建仓库
2.5配置Git
第一次使用Git仓库需要配置全局设置
git config --global user.name "abcdzzr"
git config --global user.email "1149876322@qq.com"
配置idea中的过滤文件 (SpringBoot自带.gitignore文件)
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
3.Git常用命令
更多命令可在官网查看:Git 大全 - Gitee.com
#初始化git仓库
git init
#查看git文件状态(查看文件是否被git追踪)
git status
#如果没有被追踪
git add <file>...
git add pom.xml
#撤销追踪
git rm --cached <file>
git rm --cached pom.xml
#全部进行追踪
git add .
#提交到本地仓库
git commit -m "提交的消息"
#查看是否配置远程仓库
git remote -v
#设置远程仓库地址
git remote add origin https://gitee.com/AngeGit/springboot_miaosha.git
#提交到远程仓库
git push -u origin master
#克隆远程地址的项目
git clone https://gitee.com/AngeGit/springboot_miaosha.git
#重复上述步骤
#拉取远程仓库的代码
git pull
cmd命令上传远程仓库
在项目文件下打开cmd
Microsoft Windows [版本 10.0.19043.1586]
(c) Microsoft Corporation。保留所有权利。
D:\SpringBootProject\getDemo>git init
Initialized empty Git repository in D:/SpringBootProject/getDemo/.git/
D:\SpringBootProject\getDemo>git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
.mvn/
mvnw
mvnw.cmd
pom.xml
src/
nothing added to commit but untracked files present (use "git add" to track)
D:\SpringBootProject\getDemo>git add .
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .mvn/wrapper/maven-wrapper.properties.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in mvnw.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in mvnw.cmd.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in pom.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/main/java/com/hzf/getdemo/GetDemoApplication.java.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/main/resources/application.properties.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/test/java/com/hzf/getdemo/GetDemoApplicationTests.java.
The file will have its original line endings in your working directory
D:\SpringBootProject\getDemo>git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: .mvn/wrapper/maven-wrapper.jar
new file: .mvn/wrapper/maven-wrapper.properties
new file: mvnw
new file: mvnw.cmd
new file: pom.xml
new file: src/main/java/com/hzf/getdemo/GetDemoApplication.java
new file: src/main/java/com/hzf/getdemo/contorller/GitController.java
new file: src/main/resources/application.properties
new file: src/test/java/com/hzf/getdemo/GetDemoApplicationTests.java
D:\SpringBootProject\getDemo>git commit -m"方哥提交仓库"
[master (root-commit) 3777dce] 方哥提交仓库
10 files changed, 623 insertions(+)
create mode 100644 .gitignore
create mode 100644 .mvn/wrapper/maven-wrapper.jar
create mode 100644 .mvn/wrapper/maven-wrapper.properties
create mode 100644 mvnw
create mode 100644 mvnw.cmd
create mode 100644 pom.xml
create mode 100644 src/main/java/com/hzf/getdemo/GetDemoApplication.java
create mode 100644 src/main/java/com/hzf/getdemo/contorller/GitController.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/test/java/com/hzf/getdemo/GetDemoApplicationTests.java
D:\SpringBootProject\getDemo>git remote -v
D:\SpringBootProject\getDemo>git remote add origin https://gitee.com/abcdzzr/mygit.git
D:\SpringBootProject\getDemo>git push -u origin master
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 8 threads
Compressing objects: 100% (17/17), done.
Writing objects: 100% (27/27), 59.07 KiB | 9.84 MiB/s, done.
Total 27 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.3]
To https://gitee.com/abcdzzr/mygit.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
D:\SpringBootProject\getDemo>
4.IDEA操作git
4.1更新项目
4.2提交代码
4.3推送到远程仓库
4.4通过IDE提供的终端使用git命令更新--->提交--->推送
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
PS D:\SpringBootProject\getDemo> git branch
* master
PS D:\SpringBootProject\getDemo> git branch t139
PS D:\SpringBootProject\getDemo> git checkout t139
Switched to branch 't139'
M src/main/java/com/hzf/getdemo/contorller/GitController.java
PS D:\SpringBootProject\getDemo> git branch
master
* t139
PS D:\SpringBootProject\getDemo> git status
On branch t139
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: src/main/java/com/hzf/getdemo/contorller/GitController.java
no changes added to commit (use "git add" and/or "git commit -a")
PS D:\SpringBootProject\getDemo> git add .
PS D:\SpringBootProject\getDemo> git commit -m"方哥添加了新分支"
[t139 0370d79] 方哥添加了新分支
1 file changed, 4 insertions(+)
PS D:\SpringBootProject\getDemo> git push --set-upstream origin t139
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), 823 bytes | 823.00 KiB/s, done.
Total 10 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.3]
remote: Create a pull request for 't139' on Gitee by visiting:
remote: https://gitee.com/abcdzzr/mygit/pull/new/abcdzzr:t139...abcdzzr:master
To https://gitee.com/abcdzzr/mygit.git
* [new branch] t139 -> t139
branch 't139' set up to track 'origin/t139'.
PS D:\SpringBootProject\getDemo> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
PS D:\SpringBootProject\getDemo> git merge t139
Updating 3777dce..0370d79
Fast-forward
src/main/java/com/hzf/getdemo/contorller/GitController.java | 4 ++++
1 file changed, 4 insertions(+)
PS D:\SpringBootProject\getDemo> git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.3]
To https://gitee.com/abcdzzr/mygit.git
3777dce..0370d79 master -> master
PS D:\SpringBootProject\getDemo>
gitee手动创建一个仓库
新建操作
git init 初始化一个本地的git项目
git add . 添加所有本地代码到暂存区
git status 查看文件状态
git commit -m ["提交备注"] 提交到本地仓库
git remote -v 查看是否存在远程仓库地址
git remote add origin [....] 添加远程仓库地址
git push -u origin master 将本地仓库的代码推送到远程仓库
新建分支
git branch 查看分支
git branch ["分支昵称"] 新建分支
git checkout ["分支昵称"] 切换分支
切换完分支后操作完代码块 完成操作后需提交 不然仓库无代码
git status
git add .
git commit -m "备注"
git push --set-upstream origin ["分支昵称"]
合并分支
git checkout master 切换到master主分支
git merge ["分支昵称"] 合并分支
git push 提交到主分支
5.本章总结
-
版本控制系统分为本地版本控制系统丶集中式版本控制系统丶分布式版本控制系统。
-
使用Git进行版本管理通常需要远程仓库和本地仓库相配合。
-
本地仓库可采用git clone或git init方式创建。
-
Git管理下的文件流转区域包括工作目录丶暂存区丶Git仓库目录。
-
Git管理下的文件的几个主要状态包括未跟踪丶已修改丶以暂存丶以提交。
-
Git版本控制中的主要命令包括git status丶git add丶git commit丶git push丶git pull。
-
撤销修改的Git命令主要包括git checkout丶git reset丶git commit --amend丶git revert。
-
使用分支可以把某些人工作从开发主线上分离开来,以免影响开发主线。
-
分支操作的主要命令包括git branch丶git checkout丶git merge。