Git项目开发必备命令
前言
Github:https://github.com/HealerJean
一、开发常备
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
分支出来,并且push
到git
上,这样才是真正创建了一个分支,否则,如果直接创建的两个不同的分支,将会没有同一个祖先,不能够进行合并
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
本地建立了分支 healerjean
,远程分支没有,要推送到远程分支并同步
git push --set-upstream origin healerjean
7、合并到 develop
分支
1)git
网址操作
打开 git
网址
因为我们是使用的 develop
分支,默认提交的分支是 master
,所以这里要进行改变
2)merge
合并
develop
合并到zhangyj
git merge develop
或者
git merge develop zhangyj
3)marge
和 rebase
marge |
rebase |
|
---|---|---|
特点 | 自动创建一个新的 commit 当合并时遇到冲突,修改后重新 commit 即可 |
将 commit 历史进行合并 |
优点: | 将 commit 的实际情况进行记录,便于以后查看 |
项目历史比较简单,少了 merge commit |
缺点 | 由于每次 merge 会自动产生一个 merge commit ,所以在使用一些 git 的 GUI tools ,如果 commit 频繁,这样会使得feature 分支很杂乱,如果系统功能比较简单,这时可以考虑使用 rebase 来进行合并处理。 |
当发生冲突时不容易定位问题,因为 re-write 了 history |
二、必要命令
1、删除分支
1)删除本地分支
git branch -D zhangzyj
2)删除远程分支
git push origin –-delete zhangzyj
2、回退版本 --mix
--hard
--soft
git
reset
将当前分支的HEAD
指向给定的版本,并根据模式的不同决定是否修改index
和working
tree
。常用的有三种模式,
--soft
,--mixed
,--hard
,如果没有给出<mode>
则默认是--mixed
说明 | ||
---|---|---|
HEAD |
就是指向当前分支当前版本的游标 | |
Index |
暂存区:当你修改了你的 git 仓库里的一个文件时,这些变化一开始是 unstaged (未缓存)状态,为了提交这些修改,你要使用 git add 把它加入到 index ,使它成为 staged (缓存)状态。当你提交一个 commit 时,index 里面的修改被提交。 |
|
working tree |
即当前的工作目录。 |
1)参数说明
参数 | 介绍 | |
---|---|---|
--mixed |
mixed 修改了 index ,index 中给定 commit 之后的修改被 unstaged 。如果现在执行 git commit 将不会发生任何事,因为暂存区中没有修改,在提交之前需要再次执行 git add |
![]() |
--soft |
使用--soft 参数将会仅仅重置HEAD 到制定的版本,不会修改 index 和 working tree ,本地文件的内容并没有发生变化,而index 中仍然有最近一次提交的修改,这时执行 git status 会显示这些修改已经在再暂存区中了,无需再一次执行 git add |
![]() |
--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
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
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_id (commit-id
为要删除的commit
的上一个commit
号) ,然后会开启一个文本编辑,将编辑文件,将要删除的commit
之前的单词改为drop
前面的pick
改为drop
,然后按照提示保存
git rebase -i commit_id_1
3、最后保存提交到远程分支即可
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
8、合并多个commitId
成一个
当前三个
commit
,1
,2
,3
、4
现在准备合并1 - 3
1)idea
操作
2、 选择1
右键
3、选择action
3
、2
选择 Squash
;1
选择 pick
,并在1上重新输入提交信息 1-3
2)命令操作
1、git log
获取commit
信息,获取 1
前面 0
的commitId
2、git rebase -i (commit-id)
┌─[healerjean@192] - [~/Desktop/github-rebase]
└─[$] git rebase -i c916fe929596fd0797da762c5315740394a4d705
3、将 2
、3
前面写 squash
或 s
(开始的时候全是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
~
9、合并diff
成一个 commitId
1)命令操作
开发分支
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) |
a、在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
b、使用 squash
保存以下文件后,会继续让我们编辑
commit
信息,它默认会把以下所有的commit
信息放到一起,供我们选择,有时候编辑起来会感觉不方便
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
c、使用 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
3)修改最近的 commit
内容
a、Idea
修改
b、命令修改
git commit --amend
git commit --amend -m "你的新的注释"
2)idea
操作
a、确保本地 master
分支为最新状态
b、切换到开发分支
c、执行交互式变基操作
- 选择 -> “
Git
” -> “Rebase
”。 - 在弹出的对话框里,选择
master
分支作为基准分支。 - 勾选 “
Interactive
” 选项,接着点击 “Rebase
” 按钮。 - 在弹出的交互式变基界面,会列出开发分支从
master
分支分叉之后的全部提交。把除第一个提交之外的提交前面的pick
改成squash
或者s
,然后点击 “OK”。 - 可以对合并后的提交信息进行修改,完成后点击 “
Rebase
”。
d、切换回 master
分支,merge
开发分集合
三、Idea
软件使用
1、将某个文件恢复到最近的版本
四、问题汇总
1、Git
关于 windos
版本问题
1)Windos
文件不允许有空格
2、文件名不能太长
git config --global core.longpaths true
2、乱码问题
1)git
bash
乱码
a、图形化解决方案
b、如果上面的不成功,可以执行下面的
$ 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
2)Idea
终端乱码
a、临时解决
set LESSCHARSET=utf-8
b、永久解决
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、git
分支名一直带 rebasing
如何去除
如果使用以下都没有用,最后直接删除当前目录下的 .git/rebase-apply。然后就ok了。
git rebase --continue
git rebase --skip
git reset --abort
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
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
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
4)查看仓库提交者排名前五
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 20
2024 a
1437 abc
429 def
291 zhangyujin1
5)贡献者人数统计
git log --pretty='%aN' | sort -u | wc -l
27
6)git
提交数统计
git log --oneline | wc -l 5569
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%