【经验总结】ubuntu 20.04 git 上传本地文件给 github,并解决出现的问题

1. 在GitHub

上创建仓库

  1. 登录 GitHub 个人网站

  2. 点击 New

  3. 填写 Repository name , 以及 Description (optional)

    选择 Public

    并添加 Add a README file

    点击 Create repository

  4. github repository 创建成功

2. 设置SSH key

在本地 bash 运行:

bash 复制代码
$ ssh-keygen -t rsa -C "xxx@gmail.com"

查看钥匙

bash 复制代码
$ ls ~/.ssh
id_rsa  id_rsa.pub  known_hosts
$ cat ~/.ssh/id_rsa.pub

id_rsa id_rsa.pub就是 SSH Key 的秘钥对,id_rsa 是私钥,不能泄露出去,id_rsa.pub 是公钥。

3. GitHub上传 SSH Key 公钥

点击右上角的头像,会出现

点击 Settings ,出现:

再点击 SSH and GPG keys ,出现:

点击 New SSH key

填写 Title 下面的长框
Key 下面的文本框 复制 cat ~/.ssh/id_rsa.pub 的内容

再点击 Add SSH key

这时 GitHub 会跳转到登录页面,输入 用户名和密码重新登录

4. 本地文档上传至Github

  1. 查看能否连接到Github
bash 复制代码
$ ssh -T git@github.com
Hi 6master6! You've successfully authenticated, but GitHub does not provide shell access.
  1. 设置用户名和邮箱
    用户名和邮箱为注册Github时的名字
bash 复制代码
$ git config --global user.email xxx@gmail.com
$ git config --global user.name "6master6"
$ git config -l
user.email=xxx@gmail.com
user.name="6master6"
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:6master6/llm_study.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
  1. 新建本地代码仓库
bash 复制代码
$ cd ~/your_directory

3.1 使用 git init 命令,可以将一个普通的目录转变为一个可以使用 Git 进行版本控制的代码库。在代码库中,Git 将跟踪和管理代码的历史记录、分支和更改。

bash 复制代码
$ git init
Initialized empty Git repository in /home/wxf/workspace/LLM/LLM-quickstart/transformers/.git/

在 Git 中,代码更改需要经过两个步骤才能被提交到代码库:

工作区(Working Directory) :这是当前正在进行编辑和修改的文件和目录。
暂存区(Staging Area):这是一个中间区域,用于暂时存储要提交的更改。

3.2 使用 git add 命令,可以将工作区中的文件或目录的更改添加到暂存区

bash 复制代码
$ git add file.txt          # 添加file.txt文件
$ git add your_directory/         # 添加整个目录
$ git add .                  # 添加当前目录下的所有文件和目录

3.3 使用 git commit 命令将暂存区的更改提交到本地代码库,并附带提交消息
-m "Update README file":使用 -m 参数指定提交消息,该消息是对提交的更改进行简要描述的字符串。示例中,提交消息是 "Update README file",它描述了对 README 文件的更新

bash 复制代码
$ git commit -m "Update README file"

提交后,更改将永久保存在本地代码库中,并获得一个唯一的提交标识符(commit hash)。

3.4 使用 git log 会显示当前分支的提交历史记录,并为每个提交显示其标识符。
git log 命令会按照提交时间的倒序显示提交记录,最新的提交在最上方。每个提交记录包含有关提交的信息,如提交标识符(commit hash)、作者、提交日期和提交消息。

bash 复制代码
$ git log

3.5 使用git status 用于显示当前代码库的状态信息。它会告诉您有关尚未提交的更改、已暂存的更改以及其他与代码库状态相关的信息。

bash 复制代码
$ git status
On branch main
Your branch is up to date with 'origin/main'.

3.6 使用 git branch 用于创建、列出、重命名和删除分支。分支是指向提交历史的可移动指针,它允许在代码库中独立地开发和修改不同的功能、修复错误或实验性的工作。

3.6.1 创建分支: 使用 git branch 命令创建一个新的分支。例如,要创建名为 "feature" 的分支,可以执行以下命令:

bash 复制代码
$ git branch feature

3.6.2 列出分支: 使用 git branch 命令(无参数)可以列出当前代码库中的所有分支。当前分支前会有一个星号标记。例如:

bash 复制代码
  feature
* main

3.6.3 切换分支: 要切换到其他分支,可以使用 git checkout 命令。例如,要切换到名为 "feature" 的分支,执行以下命令:

bash 复制代码
$ git checkout feature
Switched to branch 'feature'

3.6.4 重命名分支: 使用 git branch -m 命令可以将分支重命名为新的名称。例如,要将名为 "feature" 的分支重命名为 "new-feature",执行以下命令:

bash 复制代码
$ git branch -m feature new-feature
$ git branch
  main
* new-feature

3.6.5 删除分支: 使用 git branch -d 命令可以删除指定的分支。请注意不能删除当前所在的分支。例如,要删除名为 "feature" 的分支

bash 复制代码
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ git branch -d feature
error: branch 'feature' not found.
$ git branch -d new-feature
Deleted branch new-feature (was b66b7cb).
  1. 提交本地代码到Github

4.1 git remote add origin 用于将远程代码库与本地代码库进行关联。它允许将一个远程代码库(通常是在 Git 服务器上)与正在处理的本地代码库进行连接。

bash 复制代码
git remote add <remote-name> <remote-url>

origin 是远程代码库的别名,可以自由命名,通常将其命名为 "origin"。 是远程代码库的 URL 地址,它指定了远程代码库的位置

bash 复制代码
$ git remote add origin git@github.com:6master6/llm_study.git

4.2 git push 用于将本地代码库 的提交推送到远程代码库。它将在本地所做的代码更改上传到远程代码库,使其他人能够查看和访问您的更改。

bash 复制代码
git push <remote-name> <branch-name>

remote-name是远程代码库的别名,指定了要推送到的远程代码库。branch-name 是要推送的本地分支的名称

Git 在本地分支 "main" 上的提交推送到名为 "origin" 的远程代码库:

bash 复制代码
git push origin main

git push 命令的常见用法包括:

  1. 将本地分支的提交推送到远程代码库。
  2. 在推送时使用 -u 或 --set-upstream 选项,将本地分支与远程分支进行关联,以便在后续的 git pull 或 git push 中可以直接使用分支名称,而无需指定远程代码库和分支名称。
  3. 使用 --force 选项强制推送,覆盖远程分支上的提交,但在使用此选项时要小心,以免覆盖他人的工作。

4.3 git pull 用于从远程代码库 拉取最新的更新并合并到本地代码库中。它可以将远程代码库的最新更改同步到本地代码库,以确保工作处于最新状态。

bash 复制代码
git pull <remote-name> <branch-name>

Git 将会从名为 "origin" 的远程代码库拉取最新的更改,并将其合并到当前所在的分支中:

bash 复制代码
git pull origin main

git pull 命令的常见用法包括:

  1. 从远程代码库拉取最新的更改并将其合并到当前分支。
  2. 在拉取时使用 --rebase 选项,将本地的提交重新应用到拉取的远程分支的顶部,以避免产生额外的合并提交。
  3. 使用 --force 选项强制拉取,覆盖本地的更改。
  4. 在拉取时使用 --all 选项,从所有远程分支中拉取更新。
bash 复制代码
$ git push -u origin main
To github.com:6master6/llm_study.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'git@github.com:6master6/llm_study.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin main
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 16.04 KiB | 74.00 KiB/s, done.
From github.com:6master6/llm_study
 * branch            main       -> FETCH_HEAD
 * [new branch]      main       -> origin/main
Merge made by the 'recursive' strategy.
$ git push -u origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 9.37 KiB | 3.12 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:6master6/llm_study.git
   23bf525..b66b7cb  main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

5. 问题解决

5.1 fatal: 远程 origin 已经存在

此时只需要将远程配置删除,重新添加即可

bash 复制代码
git remote rm origin

5.2 git操作是出现Username for 'https://github.com':的验证问题

参考:

  1. git push提示Username for 'https://github.com' 解决办法
  2. git操作是出现Username for 'https://github.com':的验证问题

6. 参考

  1. 上传本地文件(夹)到GitHub和更新仓库文件
  2. 两种方法上传本地文件到github
相关推荐
赵健zj6 分钟前
鸿蒙Next开发,配置Navigation的Route
android·linux·ubuntu
好奇的菜鸟35 分钟前
Linux 系统下的 Sangfor VDI 客户端安装与登录完全攻略 (CentOS、Ubuntu、麒麟全线通用)
linux·ubuntu·centos
Ronin3051 小时前
【Linux系统】进程切换 | 进程调度——O(1)调度队列
linux·运维·服务器·ubuntu
tmacfrank2 小时前
Git 使用技巧与原理(一)—— 基础操作
git
Kevin Wang7272 小时前
Ubuntu服务器安装Miniconda
linux·服务器·ubuntu
Jinxiansen02112 小时前
TypeScript 中的内置工具类型(Utility Type)
javascript·ubuntu·typescript
dilvx2 小时前
git 配置 default editor
git
qianmoQ2 小时前
GitHub 趋势日报 (2025年07月12日)
github
冬夜戏雪3 小时前
阿里云ubuntu安装mysql docker容器(拉,运行,测试完整版)
mysql·ubuntu·阿里云