本地项目上传到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://我的[email protected]/xxx/xxxx.git
$ git push -u origin master
fatal: unable to access 'https://我的[email protected]/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

相关推荐
漫步者TZ2 小时前
【Netty系列】Reactor 模式 1
java·开发语言·github
加百力2 小时前
戴尔AI服务器订单激增至121亿美元,但传统业务承压
大数据·服务器·人工智能
粥里有勺糖3 小时前
视野修炼-技术周刊第121期 | Rolldown-Vite
前端·javascript·github
TGITCIC5 小时前
数据基座觉醒!大数据+AI如何重构企业智能决策金字塔(下)
大数据·大数据ai·大模型落地·大模型大数据·ai与大数据结合·大数据集成ai·企业ai
寻月隐君7 小时前
从零到 Web3:使用 @solana/kit 快速查询 Solana 账户余额
后端·web3·github
AI码农7 小时前
极限3天:微信小程序实时语音对话 SDK 开发实战(基于 Coze API)
javascript·github
王禄DUT8 小时前
防疫大数据 第27次CCF-CSP计算机软件能力认证
大数据·c++·算法
北漂老男孩8 小时前
Flink SQL 编程详解:从入门到实战难题与解决方案
大数据·sql·flink
梅一一9 小时前
5款AI对决:Gemini学术封神,但日常办公我选它
大数据·人工智能·数据可视化
梓羽玩Python9 小时前
这款开源神器把Excel翻译卷出新高度!免费、无限文件大小、公式精准!
github