git的笔记

这篇文章最后更新的时间在六个月之前,文章所叙述的内容可能已经失效,请谨慎参考!

git init
git add 文件名
git commit -m "这是注释"
git clone 远程仓库地址
git clone 远程仓库地址 目录
git clone -b 分支名/标签 --single-branch --no-tags --depth=1  远程仓库地址 目录
--single-branch 仅克隆单一分支
--no-tags 不下载标签
--depth=1 浅层克隆(shallow clone),仅获取最近 1 个提交,而非完整历史
git pull
git push origin master
git branch 分支名
git checkout 分支名
git switch 分支名
git branch
git branch -a
git branch -D 分支名
git merge 分支名
git merge 远程仓库名/分支名
git log
只显示最近的两条记录
git log -2

git fetch; git log --author="username@163.com"  --full-history --pretty="%h %S %ce %ci %s" --date-order --decorate=full --skip=0 --after="2023-12-31" --branches --tags --remotes
git fetch; git log --author="username@163.com"  --full-history --pretty="%h %S %ce %ci %s" --date-order --decorate=full --skip=0 --after="$(date -d "$(date +%Y%m01) last day" +%Y-%m-%d)" --branches --tags --remotes
git remote add 远程仓库名 远程仓库地址
例子
git remote add test3 ssh://username@127.0.0.1//alidata/www/.git
git push 远程仓库名
git pull 远程仓库名
git remote -v
git remote rename 旧名字 新名字
git remote rm 仓库名
git status
更改->暂存的更改(add)->提交(commit)(提交暂存文件)->推送(push)
    1. git fetch:相当于是从远程获取最新版本到本地,不会自动合并。
    git fetch origin master
    git log -p master..origin/master
    git merge origin/master
以上命令的含义:
首先从远程的origin的master主分支下载最新的版本到origin/master分支上然后比较本地的master分支和origin/master分支的差别最后进行合并
上述过程其实可以用以下更清晰的方式来进行:
    $ git fetch origin master:tmp
    $ git diff tmp 
    $ git merge tmp
    2. git pull:相当于是从远程获取最新版本并merge到本地 
    git pull origin master
    上述命令其实相当于git fetch 和 git merge在实际使用中,git fetch更安全一些,
    因为在merge前,我们可以查看更新情况,然后再决定是否合并。
git config user.signingkey [用户ID]
git tag -s tagname -m 'msg'
git tag -v tagname
git show tagname
git commit -S -m 'msg'
git verify-commit commitid
git log --show-signature -10
git reset --soft HEAD~1
git commit --amend
git push -f
git reset -q HEAD -- .
git checkout -- .
git clean -fd
git reset --hard
git stash
git stash push
git stash pop
git stash apply
git stash list
git stash show
git stash drop
git stash clear

pre-commit

#!/bin/bash

# 获取暂存区中修改的文件列表
echo "获取暂存区文件..."
changed_files=$(git diff --cached --name-only)

# 检查是否有文件被修改
if [ -z "$changed_files" ]; then
    echo "没有暂存的文件"
    exit 0
fi

# 将文件列表按换行符分割成数组
IFS=
    #39;\n' read -d '' -ra files <<< "$changed_files"

# 创建一个数组来存储符合条件的文件
php_files=()

# 筛选出以 .php 和 .phtml 结尾的文件
echo "筛选 PHP 相关文件..."
for file in "${files[@]}"; do
    # 检查文件是否以 .php 或 .phtml 结尾
    if [[ "$file" == *.php ]] || [[ "$file" == *.phtml ]]; then
        # 检查文件是否存在(避免已删除的文件)
        if [ -f "$file" ]; then
            php_files+=("$file")
        fi
    fi
done

# 检查是否有符合条件的文件
if [ ${#php_files[@]} -eq 0 ]; then
    echo "没有找到 .php 或 .phtml 文件需要检测"
    exit 0
fi

# 显示要检测的文件
echo "检测以下文件:"
for file in "${php_files[@]}"; do
    echo "  - $file"
done

# 使用 phpcs 对筛选出的文件进行格式检测
echo "开始代码格式检测..."

# phpcs_result=0
# for file in "${php_files[@]}"; do
#     echo "检测文件: $file"
#     # vendor/bin/phpcs --standard=Magento2 --warning-severity=3 "$file"
#     vendor/bin/phpstan analyse --no-progress --no-ansi -l 4 "$file"
#     if [ $? -ne 0 ]; then
#         phpcs_result=1
#     fi
#     echo "------------------------"
# done
# if [ $phpcs_result -eq 0 ]; then
#     echo "所有文件格式检测通过"
# else
#     echo "发现代码格式问题,请修复后重新提交"
#     exit 1
# fi

vendor/bin/phpstan analyse --no-progress --no-ansi -l 4 "${php_files[@]}"
if [ $? -eq 0 ]; then
    echo "所有文件格式检测通过"
    exit 0
else
    echo "发现代码格式问题,请修复后重新提交"
    exit 1
fi