如何将本地项目上传到github上

将本地项目上传到github上有很多种方法,这里只讲述我认为最简单快捷的一种,先在github中创建一个仓库,接着在本地建文件夹,用命令行将项目推送到本地仓库,然后连接远程仓库,将本地项目推送到远程仓库上。要熟练掌握git的常用语法!!!!

1.登录github账号,建立仓库

登录之后,在首页面找搜索框旁边的按钮。点击"+"号,点击"New repository"

点击之后,会出现新仓库的页面,填写基础信息。

必填项:repository name 和public(一般此处均选择public)

之后下滑,点击绿色按钮"Create repository" 出现此页面,成功建立仓库。

2.本地建文件夹

1.在本地建立一个文件夹,最好和仓库起相同的名字

进入文件夹,右键选择"git bash"

进入命令行,通过命令git init把这个文件夹变成Git可管理的仓库

输入"ll"此时可以看到此文件夹下所有文件的目录

total 0 表示此时没有文件,将你的项目文件粘贴在所建的文件夹中,继续输入"ll"

输入"git status"查看仓库状态,此时红色文件名即表示还未进行推送。

(注:里面多了个.git文件夹,它是Git用来跟踪和管理版本库的。如果看不到,是因为它默认是隐藏文件,那需要设置一下让隐藏文件可见。)

输入"git add ."把项目添加到仓库,注意点是用空格隔开的,绿色文件名,表示在暂存区

输入git commit把项目提交到仓库。"git commit -m "描述信息" "

此时,查询状态"git status",出现以下语句,证明推送成功。

3.连接远程仓库

找到github上的仓库地址,https 复制其下的地址

输入"git remote add origin + 你在github上创建的仓库地址"

关联好之后,将你仓库里的东西推送到远程仓库,输入" git push -u origin master"成功

第一次仓库是空的,因此加上"-u" ,此后再推送,输入"git push origin master" 即可

(注:有时候网不好,可以多推送几次)

github页面会出现,绿色按钮和黄色框框,"master had recent pushes 9 minutes ago"

网站上有两个分支 main(默认)和master,提交到master分支上。以前的github,是将master作为默认分支的,但是最近改成了main,具体可以看这篇:https://github.com/github/renaming

stackoverflow中如下描述:(后附有道翻译结果)

复制代码
 By default and convention, the way we do this using git push is pretty simple:
    git push origin main for instance.
    The git push part is the command
    that means send commits and ask them to set a name. The origin part is
    what Git calls a remote: a short name that, mostly, holds a URL. The
    main part at the end, here, is our branch name. That's the one our Git
    is using to find our commits. We'll have our Git send our commits,
    then ask their Git to set their main too.

    This last part---where we've put in main here---is what Git calls a
    refspec. Refspecs actually let us put in two names, separated by a
    colon, or a couple of other forms. We can, for instance, use HEAD:main
    as in Arka's answer (although for technical reasons we might want to
    use HEAD:refs/heads/main in many cases). But in simple cases, we can
    just use one branch name: git push origin main. The simple branch name is a simple form of refspec.


默认情况下,我们使用git push的方式非常简单:
例如Git push origin main。
git push部分是命令
这意味着发送提交并要求它们设置一个名称。原点部分是
Git称之为远程:一个简短的名称,通常包含一个URL。的
最后的主要部分是我们的分支机构名称。这是我们的Git
是用来查找提交的。我们会让Git发送提交,
然后让Git也设置main函数。

最后一部分------我们在这里把main放在这里------是Git调用的a
refspec。Refspecs实际上让我们输入两个名称,用a分隔
冒号,或者其他形式。例如,我们可以使用HEAD:main
就像Arka的回答一样(尽管出于技术原因,我们可能想要这样做)
在很多情况下使用HEAD:refs/heads/main)。但在简单的情况下,我们可以
只需使用一个分支名称:git push origin main。简单分支名称是refspec的一种简单形式。

解决方法:将master分支名称改成main 输入"git branch -m master main"

再提交git push -u origin main

经过如上操作,发现出现了报错代码及原因附下(有道翻译)

复制代码
error: failed to push some refs to 'https://github.com/!!!!!/DemoP.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.  This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

错误:未能将一些refs推到'https://github.com/!!!/DemoP.git'

提示:更新被拒绝,因为远程包含您不需要的工作

提示:有本地。这通常是由于另一个存储库推送到

提示:相同的引用。如果要集成远程更改,请使用

提示:再推之前先拉一下。

提示:详见"git push---help"中的"关于快进的说明"。

根据提示信息,先pull 以下,再进行推送

此时,刷新当前浏览器页面,即可看到文件已在仓库中

至此,文件推送成功!!!!!!

4.常用git指令

git的常用指令

  • 1.git --version 查看git版本
  • 2.git config --global user.name 用户名
  • 3.git config --global user.email 邮箱
  • 4.git init 初始化本地库
  • 5.git status 查看本地库状态
  • 6.git add 文件名 添加到暂存区
  • 7.git commit -m "日志信息" 文件名 提交到本地库
  • 8.git reflog 查看历史记录

"一定要经常敲电脑!!!!"

  • 9.git reset --hard 版本号 版本穿梭
  • 10.git branch 分支名 创建分支
  • 11.git branch -v 查看分支
  • 12.git checkout 分支名 切换分支
  • 13.git merge 分支名 把指定的分支合并到当前分支上
相关推荐
u13013032 分钟前
GitHub 热榜项目:日榜(2026-10-27)
人工智能·github
zroooo1 小时前
GitHub Flow
github
粥里有勺糖2 小时前
视野修炼-技术周刊第137期 | 编程已经面目全非
前端·github·ai编程
miofly2 小时前
GitHub 今日推荐|rea:用 MCP 把反编译器和 AI agent 接到一起的逆向工具
typescript·开源·github
MicrosoftReactor3 小时前
技术速递|超越一刀切:为 GitHub Copilot CLI 和 VS Code 实现决策路由
vscode·ai·github·copilot·决策路由
m4Rk_3 小时前
【论文阅读】Agent 记忆机制(113):Agentic Memory Management——从“检索相关信息”走向“管理 Agent 内部记忆”
论文阅读·人工智能·学习·开源·github
技术达芬奇4 小时前
git bundle介绍
github
暖心hebbie设计学习5 小时前
GitHub 热榜拆解(10.28)|5 个 AI 工具:从设计到写代码,附 issue 区坑点
人工智能·github
逛逛GitHub5 小时前
看这 11 个案例,AI 做游戏真的越来越顶了。
github
前端小万5 小时前
想做个人副业别乱买服务器!我的 5 个月真实开销分享
前端·后端·github