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

相关推荐
SelectDB9 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
ApacheSeaTunnel11 小时前
当多表数据涌入,Apache SeaTunnel 如何巧妙化解主键冲突?
大数据·开源·数据集成·seatunnel·技术分享·数据同步
YuePeng12 小时前
写了五年注解的低代码框架,2.0 决定让你连注解都不用写了
github·产品
小白ai12 小时前
从"能 ping 通吗"到"为什么上不了网"——我写了一个网络故障诊断引擎
github
徐小夕14 小时前
jitword 协同文档3.2发布:打造浏览器中最强word编辑器
前端·架构·github
齐翊16 小时前
分享一个在 Claude Code 里 [同时] 用多个 ApiKey 的方法
程序员·github·agent
A_Lonely_Cat16 小时前
记一次 GitHub 幽灵协作者大清洗:强制重写 Git 历史与穿透 CDN 缓存实践
git·github
极光技术熊1 天前
Spring AI 从入门到精通:构建你的 AI 开发知识体系
后端·github
用户39483951075531 天前
怎么让我的 Agent 真正"懂"我?——关于记忆、经验学习与预测的一些真实体验
github
远航_2 天前
git submodule
前端·后端·github