Git使用记录 - 持续更新

本地生成 sshkey

  1. 打开git命令工具
    cd ~/.ssh
    ssh-keygen -t rsa -C "实际的eamil地址"
    ···
    // 一路回车,出现以下则说明成功
    Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa
    Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa.pub
    ···
    
  2. 复制 id_rsa.pub 内的内容,粘贴至远程 git 网站设置。

本地切换远程仓库地址

  1. git remote -v 查看本地远程仓库地址
  2. git remote rm origin 删除本地仓库地址
  3. git remote -v 查看本地仓库地址是否删除
  4. git remote add origin xxx 添加新的远程仓库地址,xxx为新的远程仓库地址
  5. git remote -v 查看本地更新的仓库地址是否已经生效

将本地项目关联到远程仓库

有两种办法:
一. clone 项目到本地,然后将本地项目内的文件复制过去,add 后推送到远程
二. 合并两个项目

  1. 本地项目初始化一个git仓库,并将文件加到新建的git仓库中。如果本地项目已经是一个git仓库了,请跳过这一步。
    git init
    git add .
    git commit -m "commit first project"
    
  2. 添加新的远程仓库地址,可先通过 git remote -v 查看本地是否有仓库地址,没有的话直接添加,有的话通过 git remote rm origin 删除。
    git remote add origin xxx
    

    建议远程地址,使用 SSH 地址,因为使用 https 地址时遇到一些鉴权问题。形如 git@github.com:xxx/xxx.git

  3. 拉取下远程内容,注意 github 目前主分支名称已由 master 修改为 main
    git pull origin main
    
    如果含有共同文件时需要:
    git merge origin/main
    
  4. 把本地库的所有内容推送到远程库上
    git push -u origin main
    

本地分支名称修改

提交代码时,遇见以下错误

error: src refspec main does not match any.
error: failed to push some refs to 'xxx.git'

查了下,是本地分支名称和远程仓库不匹配,通过以下方式修改

git branch -m "原名称" "想要推送的远程分支名称"

原名称 可以通过 git branch 查询。

修改SSH链接端口

拉取 github 的代码时,出现错误

> git pull --tags origin main
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因:拉取方式采用的 SSH,然后22端口被防火墙屏蔽了,或者被科学network工具纂改了DNS解析。
解决方案一:使用https协议;
解决方案二:修改端口为443:

  1. 命令行执行 ssh -T -p 443 git@ssh.github.com,测试443端口是否可用,示例如下:
    PS C:\Users\Administrator> ssh -T -p 443 git@ssh.github.com
    The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
    ED25519 key fingerprint is SHA256:+***************************
    This host key is known by the following other names/addresses:
    	C:\Users\Administrator/.ssh/known_hosts:1: github.com
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
    Hi weizwz! You've successfully authenticated, but GitHub does not provide shell access.
    
  2. ~/.ssh/config 文件里添加如下内容,如果没有 config 文件,则新建一个
    # Add section below to it
    Host github.com
      Hostname ssh.github.com
      Port 443
    
    然后重新 pull 代码,则恢复正常

fatal detected dubious ownership in repository at 解决办法

重装系统后,执行原来仓库下文件时报错

fatal: detected dubious ownership in repository at 'xxxxx'

原因:git新的权限安全策略导致的报错,可以按提示把目录添加到信任列表

git config --global --add safe.directory "*"

LF will be replaced by CRLF 警告

背景:
CR为回车符,LF为换行符。Windows结束一行用CRLF,Mac和Linux用LF。
core.autocrlf:
false表示取消自动转换功能。适合纯Windows;
true表示提交代码时把CRLF转换成LF,签出时LF转换成CRLF。适合多平台协作;
input表示提交时把CRLF转换成LF,检出时不转换。适合纯Linux或Mac

单人项目:设置 git config --global core.autocrlf false,关闭提示即可;
多人协作,确定是结尾用CRLF还是LF,然后将统一转换。

posted @ 2022-11-20 11:40  唯之为之  阅读(98)  评论(0编辑  收藏  举报