本地项目上传到GitHub

本文档因使用实际项目提交做为案例,故使用xxx等字符进行脱敏,同时隐藏了部分输出,已实际项目和命令行输出为准

0、 Git 安装与GitHub注册

1) 在下述地址下载Git,安装一路默认下一步即可。安装完成后,随便找个文件夹鼠标右键,存在Git GUI HereGit Bash Here 两个选项即可

https://git-scm.com/downloads

2) GitHub注册

访问下方地址,点右上角 sign up进行注册即可。

https://github.com/

1、 本地工程文件初始化

进入到本地工程的文件夹,右键点击Git Bash Here进入命令行窗口,然后输入git init命令。

shell 复制代码
$ git init
Reinitialized existing Git repository in D:/xx/xxx/.git/

执行完成后,文件夹中会多一个.git文件夹,如果看不到该文件夹,可能是隐藏了,电脑文件资源管理器上方查看 菜单里,将隐藏的项目勾上即可。

2、 代码添加到仓库

执行 git add 命令将代码添加到仓库。这里说的仓库,只是本地仓库,不是GitHub仓库。因为工程文件夹里面会有 targetideaeclipse等IDE的配置文件等,所以这一步可以手动挑选需要上传的文件夹和文件,也可以写一个.gitignore文件添加忽略规则

1、 .gitignore

txtfile 复制代码
.classpath
.project
.settings/
target/

2、 git add

shell 复制代码
$ git add src/
$ git add pom.xml
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   pom.xml
        ......(省略)

add之后可以通过git status命令查看,输出的Changes to be committed会将添加到仓库待提交的文件列举出来。

3、代码提交

使用 git commit命令提交代码,-m参数是一般常用参数,代表注释。

shell 复制代码
$ git commit -m "first commit"
[master (root-commit) d0bbe42] first commit
 11 files changed, 248 insertions(+)
 create mode 100644 pom.xml
 ......(省略)

4、 远程仓库创建及代码push

点击左上角按钮创建

或头像下菜单创建

点击后跳转到页面,填写信息即可。

创建完成后进入到仓库,把这个仓库的地址复制下来

复制了地址之后,回到命令行,输入命令行push代码到GitHub(这里xxx都是脱敏和谐掉的,以实际为准,下同)

shell 复制代码
$ git remote add origin https://github.com/xxx/xxxx.git

接着就可以pushGitHub

shell 复制代码
$ git push -u origin master

这里会让你输入用户名和密码,但是我在提交的时候,会遇到输了账号和密码,但是验证不通过的情况,输出如下

Logon failed, use ctrl+c to cancel basic credential prompt.

Username for 'https://github.com': 我的用户名

remote: Support for password authentication was removed on August 13, 2021.

remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.

fatal: Authentication failed for 'https://github.com/xxx/xxx.git/'

根据 参照 https://blog.csdn.net/weixin_43452467/article/details/121704661 生成了token并修改命令行。重新push

shell 复制代码
$ git remote set-url origin https://我的token@github.com/xxx/xxxx.git
$ git push -u origin master
fatal: unable to access 'https://我的token@github.com/xxx/xxxx.git': Failed to connect to github.com port 443: Timed out

又根据 参照 https://blog.csdn.net/qq_37284798/article/details/127806254才可正常push

shell 复制代码
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
$ git push -u origin master
Enumerating objects: 30, done.
Counting objects: 100% (30/30), done.
Delta compression using up to 12 threads
Compressing objects: 100% (16/16), done.
Writing objects: 100% (30/30), 4.42 KiB | 1.47 MiB/s, done.
Total 30 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/xxx/xxxx/pull/new/master
remote:
To https://github.com/xxx/xxxx.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

至此,本地项目就pushGitHub

5、 默认分支的设置

按照上述命令行,我们是以master分支提交的,而GitHub默认是main分支,这样的话,仓库里面就会有两个分支,有时候需要把默认分支替换。

1、 全局修改

2、 单仓储修改


我这里还把main 分支删了,下面截图是删了以后的

选择main分支进行删除即可

参考文献

https://blog.csdn.net/qq_37284798/article/details/127806254

https://blog.csdn.net/weixin_43452467/article/details/121704661

相关推荐
mnasd5 小时前
python常用模块
大数据
步里软件5 小时前
2611.某音 MCN 运营效率提升指南:从手动重复到自动化全流程
大数据·自动化·抖音关注·抖音评论
直接冲冲冲5 小时前
研0-准备claude
git·github
Agent手记9 小时前
制造业生产流程自动化,Agent需要具备哪些能力?深度拆解2026工业级智能体落地范式与核心架构
大数据·人工智能·ai·架构·自动化
硅基流动9 小时前
光谷爱计算 × 硅基流动:AI 算力联合运营,共建高效“Token 工厂”
大数据·人工智能
Yunzenn9 小时前
深度分析字节最新研究cola-DLM 第 07 章:推理流水线逐行拆解 —— 从 prompt 到生成文本
人工智能·驱动开发·深度学习·chatgpt·架构·prompt·github
xinshu52710 小时前
企业工商和司法风险:从定义到AI识别的完整指南
大数据·人工智能·技术分享
努力努力再努力wz10 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github
anew___10 小时前
国产AI大模型巅峰对决:2026年5月主流模型深度横评
大数据·人工智能
财经资讯数据_灵砚智能10 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月26日
大数据·人工智能·python·信息可视化·自然语言处理·ai编程·灵砚智能