本地项目上传到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

相关推荐
金融小师妹2 小时前
基于多模态宏观建模与历史序列对齐:原油能源供给冲击的“类1970年代”演化路径与全球应对机制再评估
大数据·人工智能·能源
播播资源2 小时前
OpenAI2026 年 3 月 18 日最新 gpt-5.4-nano模型:AI 智能体的“神经末梢”,以极低成本驱动高频任务
大数据·人工智能·gpt
GJGCY3 小时前
中小企业财务AI工具技术评测:四大类别架构差异与选型维度
大数据·人工智能·ai·架构·财务·智能体
九河云4 小时前
云上安全运营中心(SOC)建设:从被动防御到主动狩猎
大数据·人工智能·安全·架构·数字化转型
武子康4 小时前
大数据-252 离线数仓 - Airflow + Crontab 入门实战:定时调度、DAG 编排与常见报错排查
大数据·后端·apache hive
jinanwuhuaguo4 小时前
OpenClaw、飞书、Claude Code、Codex:四维AI生态体系的深度解构与颗粒化对比分析
大数据·人工智能·学习·飞书·openclaw
Rubin智造社4 小时前
# OpenClaude命令实战|核心控制三剑客/reasoning+/verbose+/status 实操指南
大数据·人工智能
青晚舟4 小时前
AI 时代前端还要学 Docker & K8s 吗?我用一次真实部署经历说清楚
前端·github
星浩AI4 小时前
现在最需要被 PUA 的,其实是 AI
人工智能·后端·github
华奥系科技5 小时前
智慧经济新格局:解码社区、园区与城市一体化建设逻辑
大数据·人工智能·科技·物联网·安全