Git操作

  • 安装

    1
    apt install git
  • 查看版本

    1
    git --version
  • 生成密钥

    1
    ssh-keygen -t rsa -C "YOUREMAIL"
  • 查看公钥

    1
    cat ~/.ssh/id_rsa.pub 
  • GIthub -> Settings -> SSH and GPG keys -> New SSH key

  • 检查本地是否能远程访问github服务器,出现You’ve successfully authenticated,说明认证通过。

    1
    ssh -T git@github.com
  • 配置自己的名称和电子邮件地址

    1
    2
    git config --global user.name "YOURNAME"
    git config --global user.email "YOUREMAIL"
  • 查看设置

    1
    git config --list
  • 新项目

    新建项目 https://github.com/new

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    mkdir ~/test_project
    cd ~/test_project
    git init //初始化一个空的git repository
    git remote add origin https://github.com//username//test_project.git //第一次初始化
    git checkout -b main //切换本地分支为main

    git add . //添加文件
    git commit -am "first commit" //-m指定本次提交的描述信息
    git push -u origin main

    git branch --set-upstream-to=origin/main main
    git push //之后push用这个
  • 已有项目

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    git clone git@github.com:username/test_project.git

    cd ~/test_project
    git remote add origin https://github.com//username//test_project.git //第一次初始化
    git checkout -b main //切换本地分支为main

    git pull origin main

    git branch --set-upstream-to=origin/main main

    git add . // .代表添加所有文件
    git commit -am "对文件操作的简易描述"
    git push

出现错误error: failed to push some refs to 'https://github.com/xxx/xxx.git'

出现错误的原因是github中的某些文件不在本地代码目录中。

也就是说我们需要先将远程代码库中的任何文件先pull到本地代码库中,才能push新的代码到github代码库中。

使用如下命令:git pull —rebase origin main

然后再进行上传: git push -u origin main

出现错误fatal: LF would be replaced by CRLF in xx

1
2
git config core.autocrlf false
git config core.safecrlf false

clone别的代码,上传到自己的远程仓库,仍持续跟进别人代码的更新

1
2
3
4
5
6
git remote remove origin 删除clone的远程连接
git remote add origin 码云上的仓库地址
git remote -v 看到新的origin地址及upstream
git add .
git commit -m ""
git push