前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

一、开发常备

1、本地直接创建仓库

git init / git init --bare <仓库名>
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/HealerJean123/fas.git
git push origin master

2、创建分支

当我们初次创建项目点的时候,是没有分支的,我们需要先创建一个 master 分支出来,并且 pushgit 上,这样才是真正创建了一个分支,否则,如果直接创建的两个不同的分支,将会没有同一个祖先,不能够进行合并

git checkout -b zhangyj

1)查看分支

git branch

2)查看所有的分支

git -a branch	

3、切换分支

git checkout zhangyj

4、提交全部更改到本地

git status

git add –A

git commit -m"add index.html"

5、从远程分支拉取代码

git pull origin develop  

develop为远程分支的名字

拉取某个分支到本地

git clone -b develop http://git.github.com/healerjean/test.git

6、推送到自己的远程分支

不可以跨分支 push 代码,比如现在是 zhangyj 分支,直接 push develop 是不会起作用的,我们需要网页发起请求(当我们没有权限操作 master 的时候),或者切换到其他分支,然后进行 merge 操作,如下

git push origin zhangyj

强制上传,一般不要使用,一般用在 --hard回退版本之后的上传

git –f push origin

远程分支有 healerjean, 而本地是从别的分支拉出来的名字叫 healerjean,为了同步

git branch --set-upstream-to=origin/healerjean healerjean

1584011039878

本地建立了分支 healerjean,远程分支没有,要推送到远程分支并同步

git push --set-upstream origin healerjean

7、合并到 develop 分支

1)git 网址操作

打开 git 网址

1572835086522

因为我们是使用的 develop 分支,默认提交的分支是 master ,所以这里要进行改变

1572835099908

1572835104945

1572835158526

1572835164711

2)merge 合并

develop 合并到 zhangyj

git merge develop

或者  

git merge develop zhangyj

3)margerebase

marge 特点:自动创建一个新的 commit 当合并时遇到冲突,修改后重新 commit 即可

优点:将 commit 的实际情况进行记录,便于以后查看

缺点:由于每次 merge 会自动产生一个 merge commit,所以在使用一些 gitGUI tools,如果 commit 频繁,这样会使得feature分支很杂乱,如果系统功能比较简单,这时可以考虑使用rebase来进行合并处理。

rebase 特点:commit 历史进行合并

优点:项目历史比较简单,少了 merge commit

缺点:当发生冲突时不容易定位问题,因为 re-writehistory

https://blog.csdn.net/liuxiaoheng1992/article/details/79108233 https://blog.csdn.net/happyjume/article/details/87450696

二、必要命令

1、删除分支

1)删除本地分支

git branch -D zhangzyj 

2)删除远程分支

git push origin –-delete zhangzyj

2、回退版本 --mix --hard --soft

1)介绍

git reset 将当前分支的 HEAD 指向给定的版本,并根据模式的不同决定是否修改 indexworking tree

常用的有三种模式,--soft, --mixed, --hard,如果没有给出<mode>则默认是 --mixed

HEAD: HEAD 就是指向当前分支当前版本的游标

Index: Index 即为暂存区,当你修改了你的 git 仓库里的一个文件时,这些变化一开始是 unstaged 状态,为了提交这些修改,你要使用 git add 把它加入到 index,使它成为 staged 状态。当你提交一个 commit 时,index 里面的修改被提交。

working tree: 即当前的工作目录。

a、--mixed

mixed 修改了 indexindex 中给定 commit 之后的修改被 unstaged。如果现在执行 git commit 将不会发生任何事,因为暂存区中没有修改,在提交之前需要再次执行 git add

1572841357874

b、--soft

使用--soft 参数将会仅仅重置HEAD到制定的版本,不会修改 indexworking tree,本地文件的内容并没有发生变化,而index 中仍然有最近一次提交的修改,这时执行 git status 会显示这些修改已经在再暂存区中了,无需再一次执行 git add

1572841345953

c、--hard

使用 --hard同时也会修改 working tree,也就是当前的工作目录,那么最后一次提交的修改,包括本地文件的修改都会被清楚,彻底还原到上一次提交的状态且无法找回。所以在执行 reset --hard 之前一定要小心

2)使用

a、回退到某个版本

git reset 057d

b、回退某个文件到指定版本

git reset  fcd2093 a.jsp

c、将本地的状态回退到和远程的一样

git reset –hard origin/master  (也可以是develop分支) 

d、真实使用

有时候,如果我们上传的文件太大,失败了,但是又已经 commit 了,很难受,又不能撤回,有时候又不知道到底是哪个文件太大了。所以需要了撤回到上次 commit 的状态。也就是说本地的修改可以通过git status显示出来

git log 查看comit 的id

git reset  fadsfadsfadsf123    (你运行gitcommit 之前的那个最新commit ID)

git status 就可以看到我们本地刚刚做的修改了哦 哈哈哈,这个才是真正牛逼的东西

3、查看日志

1)查看整个日志记录

a、git log

1572837363209

b、git reflog

D:\workspace\scf-resurgence>git reflog

54c65741 (HEAD -> 20191016-1.0.1-SNAPSHOT) HEAD@{0}: rebase finished: returning to refs/heads/20191016-1.0.1-SNAPSHOT
54c65741 (HEAD -> 20191016-1.0.1-SNAPSHOT) HEAD@{1}: rebase: 信贷核心参数修改
38024757 HEAD@{2}: rebase: 信贷核心参数修改
387f8163 HEAD@{3}: rebase: dev环境配置
d0125a78 HEAD@{4}: rebase: 冲突修改
77107232 HEAD@{5}: rebase: 信贷核心字段修改
6c2ca94f HEAD@{6}: rebase: 年利率改为利率+利率类型
caebb6b9 HEAD@{7}: rebase: 信贷核心参数调整
8139dd8d HEAD@{8}: rebase: 日志补充

2)查看某个版本的历史

a、fileName 相关的 commit 记录

git log filename

b、 只看某次提交中的某个文件变化,可以直接加上 fileName

git show commit-id filename

c、显示 filename 每次提交的diff

git log -p filename

4、彻底清除Git记录

1)清除某个文件的所有历史记录

1、 path-to-your-remove-file 就是你要删除的文件的路径 如果不加 /,则默认是相对路径 ,建议使用绝对路基

2、如果你要删除的目标不是文件,而是文件夹,那么请在 git rm --cached 命令后面添加 -r 命令,表示递归的删除(子)文件夹和文件夹下的文件,类似于 rm -rf 命令。

3、如果文件或路径里有中文,可以使用通配符*号, sound/music_*.mp3, 这样就把sound目录下以 music _ 开头的 mp3文件都删除了.

$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path-to-your-remove-file' --prune-empty --tag-name-filter cat -- --all

2)如果你看到类似下面这样的, 就说明删除成功了:**

Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
# Ref 'refs/heads/master' was rewritten

3) 推送我们修改后的 repo

以强制覆盖的方式推送你的repo,

git push origin master --f --all

4)清理和回收空间

$ rm -rf .git/refs/original/

$ git reflog expire --expire=now --all

$ git gc --prune=now

Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (1378/1378), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1461), reused 1802 (delta 1048)

$ git gc --aggressive --prune=now

Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (2426/2426), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1483), reused 0 (delta 0)

5、恢复不小心删除的分支

步揍1、使用git log -g 找回之前提交的commit

git log -g

步揍2、根据commit_id 创建一个分支

git branch recover_branchc_name 3eac14d05bc1264cda54a7c21f04c3892f32406a

步揍3、查看分支并切换到

git branch

git checkout recover_branchc_name

2.6、删除中间的某几个commit

即使后面有修改这几个commit中的某些文件,修改后的还会保留,所以,放心, 大胆的使用吧

现在有提交 1、 2、 3 、4 ,在4的时候,我修改了2提交的一个文件的信息。也就是2.txt ,我准备删除2 、3

git log 查看日志

HealerJean@MI-201902210704 MINGW64 ~/Desktop/git (master)
$ git log
commit cd144a1d93c7ea675d7d9b6876db3a1b1d10cea6 (HEAD -> master)
Author: HealerJean <healerjean@gmail.com>
Date:   Mon Nov 18 12:03:29 2019 +0800

    4

commit c12b02b4c74dc407775f2d148e91196900174e87
Author: HealerJean <healerjean@gmail.com>
Date:   Mon Nov 18 12:03:08 2019 +0800

    3

commit 368ea0d872032bf9063b4abd0757428b93a9a342
Author: HealerJean <healerjean@gmail.com>
Date:   Mon Nov 18 12:02:53 2019 +0800

    2

commit 6d147c0553a39bd06e68e757851552a12fef6a5f
Author: HealerJean <healerjean@gmail.com>
Date:   Mon Nov 18 12:02:35 2019 +0800

    1

2、找到1 commit_idcommit-id 为要删除的commit的上一个commit号) ,然后会开启一个文本编辑,将编辑文件,将要删除的commit之前的单词改为drop前面的pick改为drop,然后按照提示保存

git rebase -i  commit_id_1

3、最后保存提交到远程分支即可

1574050787800

2.7、Git突破 100M限制

官方方案 https://git-lfs.github.com/

1、问题出现

HealerJean@MI-201902210704 MINGW64 /d/study/HealerJean.github.io/_posts/DDKJ (master)
$ git push origin
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 120.72 MiB | 7.48 MiB/s, done.
Total 8 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: error: Trace: 90d5580f36ff974d67199826d55835c5
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File _posts/book/2_分布式消息中间件/《分布式消息中间件实践 》_倪炜.pdf is 151.75 MB; this exceeds GitHub's file size limit of 100.00 MB
To https://github.com/HealerJean/HealerJean.github.io.git
 ! [remote rejected]   master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/HealerJean/HealerJean.github.io.git'

2、安装软件

https://github.com/git-lfs/git-lfs/releases/download/v2.8.0/git-lfs-windows-v2.8.0.exe

3、找到我们的仓库,并cd启动到那个大文件下面打开git终端

$ git  lfs install

$ git  lfs track "分布式消息中间件实践.pdf"

4、上传

git status
可以看到帮我门生成了一个文件  /.gitattributes

git commit -m "提交大文件"
git push origin master 

2.8、合并多个commitId 成一个

当前三个commit1234 现在准备合并 1 - 3

2.8.1、idea操作

image-20210605224040237

2、 选择1右键

image-20210605224106622

3、选择action 32 选择 Squash1 选择 pick,并在1上重新输入提交信息 1-3

image-20210605224125037

image-20210605224143185

image-20210605224207076

image-20210605224324934

2.8.2、命令操作

1、git log获取commit信息,获取 1 前面 0commitId

image-20210605224621617

2、git rebase -i (commit-id)

┌─[healerjean@192] - [~/Desktop/github-rebase] 
└─[$] git rebase -i c916fe929596fd0797da762c5315740394a4d705  

3、将 23 前面写 squashs (开始的时候全是pick),然后保存退出

# Note that empty commits are commented out
pick 6874c5c 1
s a0df962 2
s d2e48d6 3
pick 33e9b23 4

# Rebase c916fe9..33e9b23 onto c916fe9 (4 commands)
#

4、3中操作退出之后,会自动进入种类,输入1-3合并后的提交信息

# This is a combination of 3 commits.
# This is the 1st commit message:

1-3

# This is the commit message #2:

# This is the commit message #3:


5、git log 查看变更后的信息,成功

commit 62a8616fc71785c2a1f6b922ce571517614f1fae (HEAD -> develop)
Author: zhangyujin <zhangyujin06@meituan.com>
Date:   Sat Jun 5 22:39:19 2021 +0800

    4

commit 9c23acb522d3de4654fce8ba2c755e4655c8c015
Author: zhangyujin <zhangyujin06@meituan.com>
Date:   Sat Jun 5 22:38:11 2021 +0800

    1-3

commit c916fe929596fd0797da762c5315740394a4d705 (origin/master, origin/develop, master)
Author: zhangyujin <zhangyujin06@meituan.com>
Date:   Fri Jun 4 20:11:54 2021 +0800

    0
~

2.9、合并diff 成一个 commitId

开发分支 rebase ,源分支 master ,现在希望把 diff 合并成一个commit 最终 合并到 master

命令 说明
pick 保留该commit(缩写:p)
reword 保留该commit,但我需要修改该commit的注释(缩写:r)
edit 保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e)
squash 将该commit和前一个commit合并(缩写:s)
fixup 将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f)
exec 执行shell命令(缩写:x)
drop 我要丢弃该commit(缩写:d)
   
   

2.9.1、在rebase分支上输入命令

git rebase -i master

第二部修改下面的

原文:

pick 8e632e02f 日志修改
pick 459f816e5 日志tag提取
pick 5c6c94b46 消息关键信息打印
pick 129d923c4 删除无用类
pick 4b9a37f57 取消行号打印
pick 773d1ab8b 日志类修改位置
pick bd85d3e9b 测试环境 异常日志打印
pick 31aa9db64 jmq4包插入
pick 1086913ef 去除@log

# Rebase aa4374a60..625af4f86 onto aa4374a60 (9 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

2.9.2、修改后

2.9.2.1、使用 squash

保存以下文件后,会继续让我们编辑 commit 信息,它默认会把以下所有的commit信息放到一起,供我们选择,有时候编辑起来会感觉不方便,所以建议用 fixup

pick 8e632e02f 日志修改
s 459f816e5 日志tag提取   
s 5c6c94b46 消息关键信息打印   
s 129d923c4 删除无用类   
s 4b9a37f57 取消行号打印   
s 773d1ab8b 日志类修改位置   
s bd85d3e9b 测试环境 异常日志打印   
s 31aa9db64 jmq4包插入   
s 1086913ef 去除@log

# Rebase aa4374a60..625af4f86 onto aa4374a60 (9 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


2.9.2.2、使用 fixup,我习惯用这个

fixup:与 squash 相同,只是不会保留这行 commit 的提交 message 信息,

注意:如果要修改 commit 信息需要在保存后运行下面的命令(修改最近一条commit命令)

git commit --amend
pick 8e632e02f 日志修改
f 459f816e5 日志tag提取   
f 5c6c94b46 消息关键信息打印   
f 129d923c4 删除无用类   
f 4b9a37f57 取消行号打印   
f 773d1ab8b 日志类修改位置   
f bd85d3e9b 测试环境 异常日志打印   
f 31aa9db64 jmq4包插入   
f 1086913ef 去除@log

# Rebase aa4374a60..625af4f86 onto aa4374a60 (9 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

2.9.3、修改最近的 commit 内容

2.9.3.1、Idea修改

2.9.3.2、命令修改

git commit --amend
git commit --amend -m "你的新的注释" 

3、Idea软件使用

3.1、将某个文件恢复到最近的版本

1572837980928

1572837987994

4、问题汇总

4.1、Git关于windos版本问题

4.1.1、Windos文件不允许有空格

4.2.1、文件名不能太长

解决方案
git config --global core.longpaths true

4.2、乱码问题

4.2.1、git bash乱码

4.2.1.1、图形化解决方案

1576203214477

1576203223887

1576203234548

4.2.1.2、如果上面的不成功,可以执行下面的

$ git config --global core.quotepath false          # 显示 status 编码
$ git config --global gui.encoding utf-8            # 图形界面编码
$ git config --global i18n.commit.encoding utf-8    # 提交信息编码
$ git config --global i18n.logoutputencoding utf-8  # 输出 log 编码
$ export LESSCHARSET=utf-8

4.2.2、Idea 终端乱码

4.2.2.1、临时解决

set LESSCHARSET=utf-8

4.2.2.2、永久解决

1576203486877

4.3、Github 每次上传都要输入用户名和密码

git remote -v

-- 移除旧的提交方式
git remote rm origin


git remote add origin git@xxx.git  



ssh-keygen -t rsa -C "youemail@example.com"

4.4、git 分支名一直带 rebasing 如何去除

如果使用以下都没有用,最后直接删除当前目录下的.git/rebase-apply。然后就ok了。

git rebase --continue
git rebase --skip
git reset --abort

5、统计

5.1、统计该项目所有的代码数

git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'


added lines: 217791, removed lines: 105074, total lines: 112717

5.2、统计某个人某个时间段内的代码提交量

git log --author=zhangyujin1 --since=2020-01-01 --until=2022-02-01 --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | grep "\(.html\|.java\|.xml\|.properties\)$" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done


zhangyujin      added lines: 6194, removed lines: 2974, total lines: 3220
zhangyujin1     added lines: 6117, removed lines: 2884, total lines: 3233

5.3、统计代码总行数

 find . "(" -name "*.java" ")" -print | xargs wc -l  


    33 ./merchant-trade-web/src/main/java/com/jdd/baoxian/core/trade/merchant/web/resource/QrxPolicyAttachResourceImpl.java
     127 ./merchant-trade-web/src/main/java/com/jdd/baoxian/core/trade/merchant/web/resource/MerchantPercentPlanResourceImpl.java
     197 ./merchant-trade-web/src/main/java/com/jdd/baoxian/core/trade/merchant/web/resource/MerchantRiskStrategyResourceImpl.java
      31 ./merchant-trade-web/src/main/java/com/jdd/baoxian/core/trade/merchant/web/tool/impl/MerchantToolsServiceImpl.java
      16 ./merchant-trade-web/src/main/java/com/jdd/baoxian/core/trade/merchant/web/tool/MerchantToolsService.java
   97873 total

5.4、查看仓库提交者排名前五

 git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 20
 
 
 2024 a
1437 abc
 429 def
 291 zhangyujin1

5.5、贡献者人数统计

git log --pretty='%aN' | sort -u | wc -l


 27

5.6、git 提交数统计

git log --oneline | wc -l                                                                                                                               5569

5.7、统计某人代码提交量

git log --author="zhangyujin1" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %sn", add, subs, loc }' -

added lines: 6473, removed lines: 2990, total lines: 3483n% 

ContactAuthor