解决问题:
1.如何在Ubuntu虚拟机中将项目上传至github
2.如何克隆GitHub项目
先检查一下git是否安装
            
            
              css
              
              
            
          
          git --version如果没有安装:
            
            
              sql
              
              
            
          
          sudo apt update
sudo apt install gitgit怎么知道我们使用的是哪个用户呢?这需要配置Git用户信息。
在项目的根目录下进入终端:
            
            
              arduino
              
              
            
          
          git config --global user.name "你的github用户名"
git config --gloabl user.email "你的github邮箱"进入github,点击右上方的加号,选择create a repository
先不需要选择添加.gitignore和README.md
在项目根目录下打开终端:
            
            
              csharp
              
              
            
          
          #初始化git
git init
#将所有文件添加到暂存区,注意add与.之间有一个空格
git add .
#提交更改
git commit -m "更改说明"
#添加远程仓库
git remote add origin https://github.com/你的用户名/仓库名.git
#推送到github
git push -u origin main
#如果是master分支
git push -u origin master
#如果你想重命名本地分支:例如重命名为main
git branch -M main推送时要求我们输入账号密码,但现在Github使用的是personal access token而不是账号密码
生成token:点击头像 -> Settings -> Developer Settings -> Personal access tokens -> Tokens(classic) -> Generate new token -> Generate new token(classic)
note:My VM Git Access(随便填)
Expiration(过期时间)随便填
权限至少选择repo
点击Generate Token
生成Token之后最好复制粘贴到某个地方,因为只显示一次
后续推送时将密码换为Token即可
            
            
              perl
              
              
            
          
          kevin@kevin-virtual-machine:~/goproject/chatroom$ git push -u origin main
fatal: unable to access 'https://github.com/KevinJosephDavis/MyChatWeb.git/': Failed to connect to github.com port 443 after 21044 ms: Connection refused
这是网络问题。先检查一下我们的虚拟机
            
            
              python
              
              
            
          
          kevin@kevin-virtual-machine:~/goproject/chatroom$ ping -c 4 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=313 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=335 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=259 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=229 ms
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 228.787/284.075/335.416/42.204 ms
能ping通谷歌官网,说明是可以正常访问外部网络的。
我们使用ssh解决这个问题
            
            
              bash
              
              
            
          
          #生成SSH密钥
ssh-keygen -t ed25519 -C "github邮箱"
#直接回车使用默认设置
#查看公钥
cat ~/.ssh/id_ed25519.pub将公钥添加到github:Settings -> SSH and GPG keys -> New SSH Key
粘贴刚刚生成的公钥即可
接着:
            
            
              csharp
              
              
            
          
          #删除原来的HTTPS远程仓库
git remote remove origin
#添加SSH地址
git remote add origin git@github.com:你的用户名/仓库名称.git
#推送
git push -u origin main你会看见如下语句:
            
            
              vbnet
              
              
            
          
          The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 无需担心,这是正常的安全验证步骤,这表示:
1.这是系统第一次连接GitHub
2.系统在确认你连接的是真正的Github服务器
3.防止中间人攻击
输入yes后,系统会把这个指纹保存到~/.ssh/known_hosts文件中,以后连接就不会询问了
如果顺利,你会看到:
            
            
              vbnet
              
              
            
          
          kevin@kevin-virtual-machine:~/goproject/chatroom$ git push -u origin main
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Enumerating objects: 58, done.
Counting objects: 100% (58/58), done.
Delta compression using up to 2 threads
Compressing objects: 100% (44/44), done.
Writing objects: 100% (58/58), 52.41 KiB | 3.28 MiB/s, done.
Total 58 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:KevinJosephDavis/MyChatWeb.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.这说明文件已经上传到仓库了
我们可以验证仓库状态
            
            
              bash
              
              
            
          
          #查看仓库状态
git status
#查看远程仓库信息
git remote -v
#查看分支信息
git branch -a现在我需要把这个从虚拟机上传的项目克隆到主机中,并上传一些文件,步骤如下:
先在git bash中进入你想保存这个项目的目录
            
            
              bash
              
              
            
          
          #克隆
git clone git@github.com:你的GitHub名/仓库名.git
#进入项目目录
cd MyChatWeb你可能会遇到这种情况:
            
            
              vbnet
              
              
            
          
          ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.这代表SSH端口22被阻止了,我们可以使用HTTPS替代SSH
            
            
              bash
              
              
            
          
          git clone https://github.com/你的Github名/仓库名.git然后把你需要上传的文件移动/复制到这个克隆下来的文件夹里,cmd中使用copy或remove
            
            
              csharp
              
              
            
          
          #提交推送
git add docs/
git commit -m "添加项目开发日记文档"
git push虚拟机端:
            
            
              bash
              
              
            
          
          #拉取更新
cd 你的项目目录
git pull origin main
#确认文件同步
ls docs/ #查看是否有从Windows主机上传的文件