4.1 Git

[TOC]

源码安装 Git

下载

wget https://www.kernel.org/pub/software/scm/git/git-2.11.1.tar.gz

安装依赖

yum groupinstall "Development Tools"
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-CPAN perl-devel perl-ExtUtils-Embed

解压安装

tar -zvxf git-2.11.1.tar.gz
cd git-2.11.1
# 指向ssl安装位置
./configure --with-openssl=/usr/local/openssl
make install

rpm -e git --nodeps
ln -s /usr/local/bin/git /usr/bin/git

git version

基本操作

# 更新远程仓库链接
git remote set-url origin xxxx

# 推送指定分支到远端
git pull <远程主机名> <远程分支名>:<本地分支名>

# 拉取指定分支到本地
git push <远程主机名> <本地分支名>:<远程分支名>

# 取消commit但保留add
git reset --soft HEAD^

# 取消commit和add
git reset HEAD^

# 删除commit和add代码,恢复到上次的提交状态
git reset HEAD^

配置

修改默认编辑器

以sublime为例,需将sublime_text.exe的路径添加到环境变量中。

git config --global core.editor sublime_text.exe

查看用户名和邮箱地址

git config user.name
git config user.email

修改全局用户名和邮箱地址

git config --global user.name "username"
git config --global user.email "email"

文件换行格式

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input
# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

设置代理

# 设置代理
git config --global http.proxy http://proxy.example.cn:3128


# 取消代理
git config --global --unset http.proxy

子模块

# 添加子模块
git submodule add ${子模块仓库地址} ${子模块名称}

# 拉取子模块
# 方法一:正常的使用git clone命令,然后再使用 git submodule init 和git submodule update来获取子模块
# 初始化子模块(只添加子模块版本的指针,文件夹是空的)
git submodule init ${子模块名称}

# 更新子模块
git submodule update ${子模块名称}

# 更新所有子模块
git submodule update

# 方法二:在使用git clone命令时,加上–recurse-submodules或–recursive 这样的递归参数
git clone --recursive ${主仓库地址}
# 递归克隆
git clone --recursive https://github.com/ClickHouse/ClickHouse.git

# 更新所有子模块
git submodule update

提交

修改已commit的注释信息

git commit --amend

查看所有的提交记录

git log

查看最新的commit

git show

查看指定commit hashID的所有修改

git show commitId

查看某次commit中具体某个文件的修改

git show commitId fileName

修改 comment

git commit --amend

回到与远程仓库一致处

git fetch --all  
git reset --hard origin/develop
git pull

标签

# 新建一个标签
git tag <tagname>

# 可以指定标签信息
git tag -a v0.1 -m "version 0.1 released"
# 查看所有标签
git tag
# 推送标签
git push origin v1.5

# 删除标签
git tag -d v1.4-lw

# 删除远程标签
git push origin :refs/tags/v1.4-lw

# 检出标签
git checkout 2.0.0

分支

# 切换到创建分支的基础分支
git checkout develop

# 创建本地新分支 'dev-01'
git branch dev-01

# 查看分支,创建成功
git branch -a

# 检查仓库名称
git remote

# 把新分支推送到远程
git push origin dev-01

远程关联

# 将当前分支与远程某分支关联
git branch --set-upstream-to=origin/dev-01

# 查看远程关联情况
git branch -vv

# 推送
git push origin dev-01

清除本地的新增文件(未add)

git clean -df

统计代码行数

统计时间范围代码行数

,从2020-01-01到2020-07-30的代码行数(排除dependence目录)

git log --pretty=tformat: --since="2020-01-01" --until="2020-07-30" --numstat -- . ":(exclude)dependence"| awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s removed lines : %s new lines: %s\n",add,subs,loc }'

统计个人代码行数

git log  --author="songyang" --since="2020-01-01" --until="2020-07-30" --pretty=tformat:  --numstat -- . ":(exclude)static/built" ":(exclude)static/bower_components" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

说明:

author: 作者

since: 起始时间

until: 结束时间

-- . ":(exclude)folderName" 排除文件夹

-- . ":(exclude)folderName1" ":(exclude)folderName2" 排除多个文件夹

常见问题

fatal: write error: Broken pipe

git config http.postBuffer 104857600

中文乱码处理

# 注释:该命令表示提交命令的时候使用utf-8编码集提交
git config --global i18n.commitencoding utf-8
# 注释:该命令表示日志输出时使用utf-8编码集显示
git config --global i18n.logoutputencoding utf-8
# 注释:设置LESS字符集为utf-8
export LESSCHARSET=utf-8

子模块拉取失败

  1. 删除对应目录
  2. 删除.git/modules/中对应目录

Git LFS

介绍

Git 大文件存储(Large File Storage,简称LFS)目的是更好地把大型二进制文件,比如音频文件、数据集、图像和视频等集成到 Git 的工作流中。我们知道,Git 存储二进制效率不高,因为它会压缩并存储二进制文件的所有完整版本,随着版本的不断增长以及二进制文件越来越多,这种存储方案并不是最优方案。而 LFS 处理大型二进制文件的方式是用文本指针替换它们,这些文本指针实际上是包含二进制文件信息的文本文件。文本指针存储在 Git 中,而大文件本身通过HTTPS托管在Git LFS服务器上。

安装

git lfs install

    Updated git hooks.
    Git LFS initialized.

LFS追踪文件

git lfs track "*.zip"
git lfs track "Anaconda3-4.4.0-Linux-x86_64.sh"

执行命令后,将会在项目中生成.gitattributes文件,该文件保存文件的追踪记录,需要将该文件推送到远程仓库当中。

查看追踪规则

git lfs track

    Listing tracked patterns
        *.iso (.gitattributes)
        grafana-6.4.3-1.x86_64.rpm (.gitattributes)
    Listing excluded patterns

提交&推送

与git基本操作一致,使用”git add“,”git commit“,”git push“命令。

# 添加
git add Anaconda3-4.4.0-Linux-x86_64.sh
# 提交
git commit -m "添加大文件 Anaconda3-4.4.0-Linux-x86_64.sh"
# 推送
git push

查看追踪列表

git lfs ls-files

194faa7784 * grafana-6.4.3-1.x86_64.rpm

克隆&拉取

与git基本操作一致,使用git clone,git pull命令。

# 克隆
git clone git@gitlab.example.com:group/project.git
# 拉取
git lfs fetch origin master

可执行权限

# 查看文件filename的权限
git ls-files --stage filename
> 100644 7365f0cb8734bd2031ddf800e90 0       filename

# 增加文件filename的可执行权限
git update-index --chmod +x filename

# 再次查看文件filename的权限
git ls-files --stage filename
> 100755 7365f0cb8734bd2031ddf800e90 0       filename

# 忽略权限更改
# 当前仓库
git config core.filemode false
# 全局
git config --global core.fileMode false
1. https://docs.gitlab.com/ee/administration/lfs/manage_large_binaries_with_git_lfs.html
Copyright © dalongm.top 2020 - 2021 all right reserved,powered by Gitbook修改时间: 2021-10-27 14:21:15

results matching ""

    No results matching ""