git/github basic usage guide


Git/Github Usage and Instruction

note:

  • make sure that target local files are located in repository folder.

  • Cannot operate files outside repository folder.

  • two file transferring paths: http and SSH( Recommaded, openssh required)

  • *set authentication(鉴权).

    • In .git/config, add username and passwd after url

config git

  • totally new env
    create ssh key
    ssh-keygen -t rsa -C "your_email@youremail.com"  
  • set user info
    git config --global user.name "your name"  
    git config --global user.email "your_email@youremail.com"
  • check user
    git config user.name  
    git config user.email
  • check all information
    git config -l

repository initializatioin

相关操作,在 GitHub 创建新 repository 时,会出现提醒,内容如下:

create a new repository on the command line

echo "# README" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <git url>
git push -u origin main

push an existing repository from the command line

git remote add origin <git url>
git branch -M main
git push -u origin main

repository operation

local repository trilayer structure

local repository trilayer structure
  • add files to index(cache space)

    git add file1 file2 ...  
  • add version annatation

    git commit -m "modification_annotation"
  • view current git status

    git status
  • push modification to remote repository

    git push origin master
  • pull latest version from repostory

    git pull

about version

  • get version list (not incluede the versions hereafter)

    git log [ --pretty=online ]
  • set current version

    git reset --hard version_number
  • get all versions

    git reflog

branch command

branch pattern

branch pattern
  • list branch
    git branch
  • create branch
    git branch name
  • switch branch
    git checkout name
  • creat and switch branch
    git checkout -b name
  • delete
    git branch -d name
  • merge
    git merge names

Note: the operations in different branches do not influence each other. Merge operation is in need af final.

solve conflicts

add git sshkey into Github

  • create a SSH key

    ssh-keygen -t rsa -C "your_email@example.com"

    default generated file ~/.ssh/id_rsa.pub

  • add SSH key content into github SSH
    setting -> SSH and GPG keys

日常遇到的问题

Offending RSA key in ~/.ssh/known_hosts:2

在进行 git push/pull 的时候,发生如下错误:

$ ssh -T git@github.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:uNVztksCshcc0ueBujQXVUpZIDTMczCvjtD2.
Please contact your system administrator.
Add correct host key in ~/.ssh/known_hosts to get rid of this message.
Offending RSA key in ~/.ssh/known_hosts:2
RSA host key for github.com has changed and you have requested strict checking.
Host key verification failed.

多番查阅后,总结是 GitHub rsa-id 更新,使用 ecdsa 方式。
所以,一方面要删除 ~/.ssh/known_host 中的 rsa:

  • 可以根据错误提示 Offending RSA key in ~/.ssh/known_hosts:2,进入 known_host 进行手动删除,也可以命令行的方式进行删除(在你知道 host IP 的前提下)
    ssh-keygen -t rsa -R github.com

在此之后,更新 GitHub 账户上的 host-key:

  • ~/.ssh/known_hosts 中信息添加到 Settings -> SSH and GPG keys -> New SSH key

文章作者: 梁绍波
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 梁绍波 !
评论
  目录