话不多说,直接上干货~

Git命令

入门

下载

https://git-scm.com/downloads

查看版本

$ git --version 
git version 2.30.0.windows.1

常用命令

mkdir oa-parent 
cd oa-parent 
vim .gitignore 
git init 
// 添加文件到暂存区 
git add --all(或 git add .// 提交暂存区文件到本地仓库 
git commit -m "first commit" 
// 设置远程仓库地址 
git remote add origin 
https://gitee.com/ph0211/oa-parent.git 
// 推送本地仓库代码到远程仓库 
git push -u origin master 
// 回滚分支 
git pull 
git reset 4c7ee158
git push -f

配置

// 用户名、邮箱 
git config --global user.name "ph0211" 
git config --global user.email "690574895@qq.com" 
// Windows 下需要禁用 
crlf 自动转换 lf git config --global core.autocrlf false 
// 代理服务器 
git config https.proxy http://xzproxy.cnsuning.com:8080
git config http.proxy http://xzproxy.cnsuning.com:8080

配置文件的三种类型

// 当前项目 
git config window 10 修改文件: 
E:\doc\IdeaProjects\code\.git\config 
// 当前用户 
git config --global 
window 10 修改文件: 
C:\Users\PanHao\.gitconfig 
// 所有用户 
git config --system 
window 10 修改文件: 
D:\dev\Git\etc\gitconfig

Windows 下需要禁用 crlf 自动转换 lf

Windows 中的换行符为 crlf, 而在 Linux 下的换行符为 lf autocrlf = true:表示要求 git 在提交时将 crlf 转换为 lf,而在检出时将 crlf 转换为 lf autocrlf = false:表示提交和检出代码时均不进行转换 autocrlf = input:表示在提交时将 crlf 转换为 lf,而检出时不转换

忽略配置

// IntelliJ IDEA 
.idea/ 
*.iml 
// Compiled class file 
target/

增加/删除文件

git add –all 和 git add.的区别

版本导致的差别

(1)1.x 版本 git add all 可以提交未跟踪、修改和删除文件。 git add . 可以提交未跟踪和修改文件,但是不处理删除文件。 (2)2.x 版本 两者功能在提交类型方面是相同的。

所在目录不同导致的差异

(1) git add all 无论在哪个目录执行都会提交相应文件。 (2) git add . 只能够提交当前目录或者它后代目录下相应文件。

分支

// 列出所有本地分支 
git branch 
// 列出所有远程分支 
git branch -r 
// 列出所有本地分支和远程分支 
git branch -a 
// 查看出每个分支当前指向的 
commit object git branch -v 
// 查看已经被合并进当前分支的分支 
git branch --merged 
// 查看没有被合并进当前分支的分支 
git branch --no-merged 
// 删除分支 
git branch -d 
// 强制删除分支 
git branch -D 
// 将本地仓库 master 分支与远程仓库 master 分支关联 
git branch --set-upstream-to=origin/master master

查看信息

// 查看提交历史 
git log --oneline --abbrev-commit --graph

git status查看代码状态

新建文件TestServiceImpl.java(文件状态:Untracked)

(1)执行命令

// 新增 TestServiceImpl.java 文件 
git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master 
Untracked files: 
(use "git add <file>..." to include in what will be committed) 
oa- auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 
nothing added to commit but untracked files present (use "git add" to track)

将 TestServiceImpl 文件加入暂存区(文件状态:Staged)

(1)执行命令

git add oa- 
auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git add oa- auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 
PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master 
Changes to be committed: 
(use "git restore --staged <file>..." to unstage)
 new file: oa- auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java

提交 TestServiceImpl 文件到本地仓库(文件状态:Unmodify)

(1)执行命令

git commit -m 'add TestServiceImpl class' 
git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git commit -m 'add TestServiceImpl class' 
[master 34806f4] add TestServiceImpl class 
1 file changed, 8 insertions(+) 
create mode 100644 oa- auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master 
nothing to commit, working tree clean

修改 TestServiceImpl 文件(文件状态:Modified)

(1)执行命令

修改 TestServiceImpl.java 文件 
git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master 
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: oa- 
auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 
no changes added to commit (use "git add" and/or "git commit -a")

将 TestServiceImpl 文件加入暂存区(文件状态:Staged)

(1)执行命令

git add oa- 
auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 
git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git add oa- 
auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java 

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master Changes to be committed: 
	(use "git restore --staged <file>..." to unstage) 
	modified: oa- 
auth/src/main/java/com/zhss/oa/auth/service/impl/TestServiceImpl.java

提交 TestServiceImpl 文件到本地仓库(文件状态:Unmodify)

(1)执行命令

git commit -m 'add a method for TestServiceImpl' 
git status

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git commit -m 'add a method for TestServiceImpl' 
[master 89362c4] add a method for TestServiceImpl 
1 file changed, 4 insertions(+) 

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git status 
On branch master nothing to commit, working tree clean

git log查看提交历史

查看提交历史

git log

(1)执行命令

git log

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /e/doc/IdeaProjects/oa-parent (master) 
$ git log 
commit 89362c4377ea82f11103f07b2bcbd0f260e122cc (HEAD -> master) 
Author: ph0211 <690574895@qq.com> 
Date: Thu Jan 28 00:09:40 2021 +0800 
	add a method for TestServiceImpl 
commit 34806f4563e114dbc82a78f66ee6588f9c0fc24b 
Author: ph0211 <690574895@qq.com> 
Date: Wed Jan 27 23:49:17 2021 +0800 
	add TestServiceImpl class 
commit 137d1e64ee796aead7664f6ecc433d8c560d04e8 
Author: ph0211 <690574895@qq.com> 
Date: Wed Jan 27 23:41:32 2021 +0800 
	first commit
git reflog

(1)执行命令

git reflog

(2)Git Bash 执行结果

PanHao@DESKTOP-1K16GB0 MINGW64 /E/doc/IdeaProjects/oa-parent (master) 
$ git reflog 
34806f4 (HEAD -> master) HEAD@{
   0}: reset: moving to HEAD^ 
89362c4 HEAD@{
   1}: commit: add a method for TestServiceImpl 
34806f4 (HEAD -> master) HEAD@{
   2}: commit: add TestServiceImpl class 
137d1e6 HEAD@{
   3}: commit (initial): first commit

查看精简图形结构的提交历史

(1)执行命令

git log --oneline --abbrev-commit --graph 
--oneline 或 --pretty=oneline:以精简模式显示 
–abbrev-commit:头部数据减少 
–graph:图形

(2)Git Bash 执行结果

重置版本

(1)执行命令

git reset --hard 89362c4:回退到指定版本 
git reset --hard HEAD^:回退到上一个版本 
git reset --hard HEAD~5:回退到倒数第5个版本 

HEAD:就是回退到当前版本 
HEAD^:回退到上一版本 
--hard:恢复工作区、暂存区
12-09 14:59