Git 命令介绍

发布时间: 更新时间: 总字数:2919 阅读时间:6m 作者: 分享 复制网址

git常用命令介绍,git 使用技巧和使用优化。

Git 常用命令

git init

git init here       -- 创建本地仓库
git remote add origin git@github.com:用户名/仓库名.git
                    -- 把本地仓库和远程仓库关联起来, 如果不执行这个命令的话,每次 push 的时候都需要指定远程服务器的地址

git config

global 变量:

git config --global user.name "xiexianbin"            -- 配置用户名,上传本地 repository 到服务器上的时候,在 Github 上会显示这里配置的上传者信息
git config --global user.email "me@xiexianbin.cn"     -- 配置邮箱
git config core.filemode true                         -- 配置文件权限mode敏感,在git管理的目录下执行,cat .git/config 查看
git config --global core.fileMode false               -- 同上,全局生效
git config --list                                     -- 查看配置列表

local 变量:

git config user.email "me@xiexianbin.cn"     -- 配置邮箱,可以在 .git/config 中查看对应配置

git clone

  • 用户名密码
https://<username>@<git_url>
  • SSH key
ssh://<username>@<git_url>
  • personal access tokens

获取 github personal access tokens

settings > developer settings > personal access tokens > generate new token
git clone http://<username>:<access tokens>@<git_url>
git clone https://<token>@github.com/owner/repo.git
  • 或使用 ~/.netrc 配置

git clone 子目录

仅 clone 一个 git 项目的子目录

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>

# 配置要 clone 子目录
git config core.sparseCheckout true  // 等价于 git sparse-checkout init
echo "some/dir/" >> .git/info/sparse-checkout  // 等价于 git sparse-checkout set "some/dir/"

# 查看要 clone 子目录,等价于 cat .git/info/sparse-checkout
git sparse-checkout list

# clone
git pull origin <target-branch> [--depth=1]

# --depth 后如何变成 clone 全量代码
git fetch --unshallow

git commit

git commit -m "注释"    -- 把本机缓存中的内容提交到本机的 HEAD 里面
git commit -sv -m "xxx" --date="2018-01-01T00:00:00+0800"  -- 指定提交的时间

git revert

回滚一个提交

git revert <commit-id>

git pull

git pull origin master

git fetch

git pull = git fetch + git merge

git fetch

git push

git add
git add -A      -- 将改动添加到本地仓库中
git rm xxx      -- 从本地仓库中删除指定文件
git rm -r xxx   -- 从本地仓库中删除指定文件夹

git push origin master      -- 把本地的 commit push 到远程仓库中

git push origin HEAD:master

指定分支push:

git push origin HEAD:stable/jdk8

git log/show

git show <commit-id>  # 查看某次提交的内容
git log --pretty=oneline <file-path>  # 查看某文件的修改历史
git log --stat <commit-id>  # 仅显示修改行数
git log -- <file>  # 只显示某文件的提交日志
git log --oneline -n 1  # 日志只显示一行

# 查看某个分支或 tag 的某个文件内容
git show main:.gitignore

git branch

# 查看所有分支
git branch -a

# 删除本地分支,-D 为强制删除
git branch -d  [branchName]
git branch -D  [branchName]

# 删除 origin 分支
git push origin --delete [branchName]
git push origin :[branchName]

# 清除 origin 不存在,单本地存在的分支
git remote prune origin

git tag

git tag -l

# 显示离当前提交最近的标签
git describe --tags

获取当前分支的 tag 或 branch

function current_branch () {
    local folder="$(pwd)"
    [ -n "$1" ] && folder="$1"
    git -C "$folder" rev-parse --abbrev-ref HEAD | grep -v HEAD || \
    git -C "$folder" describe --exact-match HEAD || \
    git -C "$folder" rev-parse HEAD
}

# current_branch .
  • git -C 指定 git 的根目录

为文件添加执行权限

# 新文件
git add --chmod=+x -- entrypoint.sh

# 已有问题
git update-index --chmod +x entrypoint.sh

# 若不生效,直接使用 chmod a+x entrypoint.sh; git add .
# 查看文件权限
git ls-files --stage

git describe

git describe 命令显示离当前提交最近的标签

git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>…​]
git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]
  • 示例
$ git describe --tags
v2.2.12-8-g040935c

说明:

  • v2.2.12 tag 名称
  • 8 表示自 v2.2.12 后有 8 次提交(commit)
  • g040935c
    • g 为 git 的缩写
    • 040935c 为当前提交的 commit id

git rev-list

按时间顺序倒序列出提交对象

# 按时间倒序列出所有 commit id
git rev-list --all

# 按时间倒序列出最后一次提交的 commit id
git rev-list --all -n 1

# 按时间倒序列出最后一次提交的 commit id 缩写
git rev-list HEAD --abbrev-commit --max-count=1

# 和 submodule 结合使用
git submodule foreach git rev-list HEAD --abbrev-commit --max-count=1

git rev-parse

# 显示分支
git rev-parse --symbolic --branches

# 显示tag
git rev-parse --symbolic --tags

# 显示所有
git rev-parse --symbolic --glob=refs/*

# 显示 hash 值
git rev-parse HEAD

git describe

git rev-parse master refs/heads/main

git ls-files

列出 git 未管理的文件

git ls-files --others --exclude-standard

git ls-remote

列出远程存储库中的引用

git ls-remote --heads --tags origin

git merge

将两个或多个分支历史合并在一起,以从 devmain 分支合并为例:

# 首先切换到dev
git checkout dev

# 获取最新 dev 代码
git pull

# 切换到main分支
git checkout main

# 把 dev 分支的代码merge到 main 分支
git merge dev

# 推送到远程 main 分支
git push

git apply

# 生成补丁
git diff > test.patch

# 恢复补丁
git apply test.patch

git ls-files

查看暂存区中的文件

git ls-files

git ls-tree

List the contents of a tree object

$ git help ls-tree
-r(递归)参数表示递归遍历整个树形结构。
--full-tree 参数表示显示完整的树形结构,包括所有子目录和文件。
-t 参数表示在输出中显示文件类型(blob、tree 或者是 commit)。
-z 参数表示以 null 结尾输出,方便使用程序进行处理。
--abbrev=<n> 参数表示对 SHA-1 摘要显示前 n 位。
--long 参数表示使用长格式输出,包括文件的大小,权限信息等。
<tree-ish> 表示需要显示的树形结构的引用,可以是一个分支名称、标签名称或者是一个提交的 SHA-1 摘要。
<path>... 表示需要显示的目录或文件的路径,如果未指定,则显示整个树形结构。
  • 递归查看文件的 commit-id 示例
$ git ls-tree -r main
...
160000 commit 2e3535efbc81aed06994c845d6c289d05de96e2e	themes/hugo-bootstrap-x

git rev-list

Lists commit objects in reverse chronological order 按时间倒序列出提交对象

git rev-list HEAD --abbrev-commit --max-count=1

.gitignore 文件

github 官方维护了一个仓库,专门管理 gitignore 配置:

https://github.com/github/gitignore

作用:忽略指定的内容

使用:

  • 在本地仓库根目录创建 .gitignore 文件
  • 过滤文件和文件夹: [Tt]emp/ 过滤 Temp\temp 文件夹; *.suo 过滤 .suo 文件;
  • 不过滤文件和文件夹: !*.c

表情

git 提交可以使用如下命令填加表情:

git commit -m ":smile: your message"
git commit -m ":books: documentation is completed"

https://www.webfx.com/tools/emoji-cheat-sheet/

加密

git 加密有很多方式

问题

多邮箱问题

remote pull push的时候会有问题,因为设置了 pull 的时候识别的是邮箱,2个 github 账号,2个邮箱,我们自然不能使用global的user.email,修复方式:

首先,取消global:

git config --global --unset user.name
git config --global --unset user.email

然后,设置每个项目repo的自己的user.email

cd project
git config  user.email "me@xiexianbin.cn"
git config  user.name "xiexianbin"

可以在.git/config 中查看对应的设置。

Git 中文乱码

terminal 中执行:

git config --global core.quotepath false

如果解决不了,若采用 oh-my-zsh 可以进行如下配置:

  • 打开oh-my-zsh配置文件 ~/.zshrc
  • 在文件最后面添加如下代码:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
  • 重启下终端或输入 source ~/.zshrc

git remote: HTTP Basic: Access denied 错误

git 操作报错误:git remote: HTTP Basic: Access denied 错误

解决方法:

  • 密码修改导致问题,重新输入账号密码即可,命令:
git config --system --unset credential.helper
  • 清空密码认证信息,重新输入,命令:
git config --global http.emptyAuth true

SSL: no alternative certificate subject name matches target host name

git config --global http.sslVerify false

git pull index-pack failed

用git pull的时候,经常会遇到这样的错误:

fatal: early EOF
fatal: index-pack failed

有几处设置可以尝试着去解决这个问题。首先找到gitconfig文件。它的路径在:

*[git installed path]*/etc/gitconfig

以管理员模式打开这个文件,在[core]部分加上这一句:

compression = 0

这代表不压缩,会引起一定的性能问题。如果之后不再出现以上的错误,可以再去掉这一行。如果还不行,有可能是因为存在Git上的工程太大了,这时候需要把本地的限制调高一些。还是在[core]部分,加上这两行:

packedGitLimit = 512m
packedGitWindowSize = 512m

在[pack]部分加上这三行:

deltaCacheSize = 2048m
packSizeLimit = 2048m
windowMemory = 2048m

大小根据你的实际情况来填写

换行符问题

  • 回车换行
  • CR回车
  • LF换行
  • Windows/Dos CRLF \r\n
  • Linux/Unix LF \n
  • MacOS CR \r
  • Git 配置

推荐设置:

git config --global core.autocrlf input
git config --global core.safecrlf warn
  • CRLF设置

提交时转换为LF,检出时转换为CRLF:

git config --global core.autocrlf true

提交时转换为LF,检出时不转换:

git config --global core.autocrlf input

提交检出均不转换:

git config --global core.autocrlf false
  • SafeCRLF 设置

拒绝提交包含混合换行符的文件:

git config --global core.safecrlf true

允许提交包含混合换行符的文件:

git config --global core.safecrlf false

提交包含混合换行符的文件时给出警告:

git config --global core.safecrlf warn

Git Push: Error writing request body to server

  • 问题描述
  • Git Push错误"Error writing request body to server"

默认Git设置http post的缓存为1M

  • 解决方法

增加http post缓存, 如果使用git命令,设置参数命令如下:

#增加为 500MB
git config http.postBuffer 524288000

如果使用的是Eclipse Git插件,则需要在 Window -> Preferences 中找到 Team -> Git -> Configuration 配置界面,在System Settings Tab页中 点击 “Add Entry…” 按钮来添加一个配置项:

key为: http.postBuffer value为:524288000

ssh 链接慢

修改:/etc/ssh/sshd_config

UseDNS no
# GSSAPI options
GSSAPIAuthentication no
GSSAPICleanupCredentials no

删除 git 密码

$ git credential-manager uninstall
git: 'credential-manager' is not a git command. See 'git --help'.

The most similar command is
        credential-manager-core

F&Q

failed to push some refs

  • 错误日志
$ git push origin master
Username for 'https://github.com': REDACTED
Password for 'https://REDACTED@github.com':
To https://github.com/REDACTED.git
! [rejected]         master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/REDACTED.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  • 解决方法,使用如下命令可以将仓库已经提交的合并到本地
git pull --rebase origin master

skip SSL verify

  • 解决CA引起的问题 fatal: unable to access 'https://xxx.git/': server certificate verification failed. CAfile: none CRLfile: none
export GIT_SSL_NO_VERIFY=1

RPC failed; curl 56 OpenSSL SSL_read: Connection was reset

  • error: RPC failed; curl 56 OpenSSL SSL_read: Connection was reset, errno 10054

远程仓库文件太大导致,设置 http.postBuffer(示例为 500M) 参数:

git config http.sslVerify “false” git config –global http.postBuffer 524288000

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数