linux学习(六)git

本文详细介绍如何安装Git、配置邮箱、创建本地repository,并通过解决连接GitHub失败的问题,最终成功将本地代码推送到GitHub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

管理你的代码–git

1. 安装

sudo apt-get install git

2. 配置邮箱

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
git config --list

3. 创建repository

yanglu@ubuntu:~/Desktop$ git init Data_structure
Initialized empty Git repository in /home/yanglu/Desktop/Data_structure/.git/
yanglu@ubuntu:~/Desktop$ cd Data_structure/
yanglu@ubuntu:~/Desktop/Data_structure$ gedit README
yanglu@ubuntu:~/Desktop/Data_structure$ gedit first.cpp
yanglu@ubuntu:~/Desktop/Data_structure$ git add README
yanglu@ubuntu:~/Desktop/Data_structure$ git add hello.cpp
yanglu@ubuntu:~/Desktop/Data_structure$ git commit -m "data structure"
[master (root-commit) 7815769] data structure
 2 files changed, 8 insertions(+)
 create mode 100644 README
 create mode 100644 hello.cpp

4. 上传github

yanglu@ubuntu:~/Desktop/Data_structure$ git remote add origin https://github.com/Data_structure.git
yanglu@ubuntu:~/Desktop/Data_structure$ git branch -M main
yanglu@ubuntu:~/Desktop/Data_structure$ git push -u origin main

报错

fatal: unable to access 'https://github.com/': Failed to connect to github.com port 443: Connection refused

解决方法
step1:检查邮箱是否与github邮箱一致
step2:在hosts文件尾部添加github可以响应的ip

sudo vi /etc/hosts

尾部添加

# GitHub Start 
140.82.113.3      github.com
140.82.114.20     gist.github.com
151.101.184.133    assets-cdn.github.com
151.101.184.133    raw.githubusercontent.com
151.101.184.133    gist.githubusercontent.com
151.101.184.133    cloud.githubusercontent.com
151.101.184.133    camo.githubusercontent.com
151.101.184.133    avatars0.githubusercontent.com
199.232.68.133     avatars0.githubusercontent.com
199.232.28.133     avatars1.githubusercontent.com
151.101.184.133    avatars1.githubusercontent.com
151.101.184.133    avatars2.githubusercontent.com
199.232.28.133     avatars2.githubusercontent.com
151.101.184.133    avatars3.githubusercontent.com
199.232.68.133     avatars3.githubusercontent.com
151.101.184.133    avatars4.githubusercontent.com
199.232.68.133     avatars4.githubusercontent.com
151.101.184.133    avatars5.githubusercontent.com
199.232.68.133     avatars5.githubusercontent.com
151.101.184.133    avatars6.githubusercontent.com
199.232.68.133     avatars6.githubusercontent.com
151.101.184.133    avatars7.githubusercontent.com
199.232.68.133     avatars7.githubusercontent.com
151.101.184.133    avatars8.githubusercontent.com
199.232.68.133     avatars8.githubusercontent.com
# GitHub End

再次运行
git push -u origin main,输入名字和密码即可。在这里插入图片描述

总结

git还需要多使用才能掌握

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值