git知识


下载地址:https://git-scm.com/downloads
安装时默认设置就行,就是中间让选编辑器的时候选vscode
安装完成后终端输入 gti能看到提示,安装完最好重启一次

不需要上传的文件使用.gitignore

专为gtihub设置http代理,是http,不是scoks代理,gti自身不支持scoks代理,v2rayN的http代理就是在scoks端口+1
git config --global http.proxy http://127.0.0.1:10809

在WSL2的操作系统中,要访问虚拟的连接的本机地址,在宿主windows系统中使用ipconfig 查看WSL的ipv4地址,并在代理工具中允许来自局域网的连接

git config --global http.proxy http://127.0.0.1:10808

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

推送

首先点右下角main,选择分支,有个main本地还有一个小云朵远程,我们每次都先保存本地,然后提交前需要输入确认码,我的分支是main,所以在消息里输入main后提交

然后在gti栏的右上角三个小点,有个推送,或同步

设置不需要推送的文件和目录

在项目根目录创建.gtiignore文件:
然后复制文件的相对路径,把它的反斜杠替换成正斜杠,然后在最前面再添加一个/斜杠将它排除在推送之外,就像这样:

gitignore
/myapp/.vscode
/myapp/node_modules

如果某个文件远程仓库存在,而本地文件不需要跟踪同步可以这样:
git update-index --assume-unchanged config.jsonc //不跟踪config.jsonc
git update-index --no-assume-unchanged config.jsonc//继续跟踪config.jsonc

github使用方法

设置私有和公有
打开仓库项目后》Settings》底部Danger Zone栏》Change repository visibility

删除项目
打开仓库项目后》Settings》底部Danger Zone栏》Delete this repository,然后输入一段确认码

请确保已在git中配置您的user.name和user.email”解决方法

git config --global user.name "LeeKenMr"
git config --global user.email "drweb.co@gmail.com"

需要登陆githu

解除ssl验证

错误:
Git: fatal: unable to access 'https://github.com/caominli/vuedeom.git/': OpenSSl
SSL read: SSL ERROR SYSCALL, errno 0

解决办法,或者使用代理
git config --global http.sslVerify false

无法连接git

错误:
Git: fatal: unable to access "https://github,com/caominli/vuedeom.git/': Failed toconnect to github,com port 443 after 21115 ms: Couldn't connect to server

解决办法:设置http代理

开始一个新项目

git init

不记录所有权的系统

错误:fatal: detected dubious ownership in repository at
解决办法:
git config --global --add safe.directory D:/tongbu/gin_btcmai_i18n 添加列外

克隆项目

git clone https://github.com/fyinfor/fy_mini_app.git 克隆一个新项目并创建文件夹
git clone https://github.com/hiyouga/LLaMA-Factory.git . 克隆到当前目录不创建文件夹,前提当前目录必须是空的

复制自己项目的方法:

在github创建一个新项目,然后在本地项目的根目录下执行命令:
git remote remove origin
#项目名就是新创建的项目
git remote add origin https://github.com/LeeKenMr/chrome_extension_x_chat.git
git remote -v 验证当前仓库地址
git branch -M main
git add . 将文件添加到git
git commit -m "Initial commit" 创建初始提交
git push -u origin main

ubuntu安装git

sudo apt update
sudo apt install git
git --version
如果系统没有git-lfs需要安装
sudo apt install git-lfs 安装
git lfs install 启用

常用命令

rm -rf .git 删除git信息
git init 初始化一个新的仓库
git remote add origin https://github.com/user/new-repo.git 添加远程地址`

python环境从项目根目录开始,避免导入包路径问题

在.env文件中使用
PYTHONPATH=./

如果这个方法不生效,则在项目创建:
.vscode/settings.json

json
{
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}"
    }
}

SSH登录验证

生成一对密钥,后面的是标签注释作用
ssh-keygen -t ed25519 -C "gitlab"
指定名称
指定密码种子(可留空)
重复密码种子

会在执行目录生成对密钥,添加公钥到gitlab

把私钥文件放入~/.ssh/ 中
确保文件权限正确:
chmod 600 ~/.ssh/gitlab

在~/.ssh/config添加内容,其中gitlab.gbox.one换成实际域名,假设私钥文件名为gitlab

bash
Host gitlab.gbox.one
  User git
  HostName gitlab.gbox.one
  IdentityFile ~/.ssh/gitlab
  IdentitiesOnly yes

windows中
管理员运行shell

ruby
Get-Service ssh-agent # 检查自动ssh状态
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519 #加入私钥

测试ssh连接
ssh -T git@gitlab.gbox.one

默认就会使用ssh,直接ssh克隆项目
git clone git@gitlab.gbox.one:ai-poker/crowdpulse.git

使用以下命令将https改为ssh连接(修改域名也是这个命令)
git remote set-url origin git@gitlab.gbox.one:ai-poker/crowdpulse.git

验证当前仓库使用的是否ssh
git remote -v

修改为https验证
git remote set-url origin https://gitlab.gbox.one/crowdpulse.git

https缓存身份验证
git config --global credential.helper store

本地代码新创建项目

必须现在github先创建好一个空项目
git init # 初始化 Git 仓库
git add . # 添加所有文件到 Git 暂存区
git commit -m "首次提交" # 提交文件
git branch -M main # 设置默认分支为 main(可选,但推荐)
git remote add origin https://github.com/你的用户名/你的仓库名.git
git remote add origin https://codeup.aliyun.com/693f71e6c9f33056ad3d8f5c/Nova3D/nova3d_client.git
git remote -v 验证当前仓库地址
git push -u origin main # 推送到 GitHub 上
git push -u origin main --force # 如果有错误,强制推送到git

移除git信息

Remove-Item -Recurse -Force .git windows
rm -rf .git linux mac

大文件下载

windows版的客户端已经自带,如果使用ubuntu请安装
sudo apt install git-lfs 安装
git lfs install 启用

临时滚回

git log --oneline -5 查看过去5个检查点

git switch --detach HEAD~2 临时切到上两个保存点
或者
git switch --detach abc1234 切换到指定检查点

git switch - 回到刚才的最新分支
或者
git switch main 回到主分支

网络环境不好时克隆失败

清理之前的文件残留
Remove-Item -Recurse -Force ".\项目文件夹名" -ErrorAction SilentlyContinue

重新克隆
git -c http.version=HTTP/1.1 -c http.sslBackend=openssl clone --depth 1 --single-branch 克隆地址

或者直接只克隆默认分支和最新代码,不克隆历史
git clone --depth 1 --single-branch 克隆地址