github拉取与推送等操作

  1. Github新建仓库五步指令
  2. 关于add
  3. 关于克隆
  4. 关于pull
  5. 关于push
  6. 关于分支
  7. 关于token
  8. js发送请求(仅作为模板):
    1. 读取数据:
    2. 更改数据:

Github新建仓库五步指令

# 第一步:创建新的仓库
git init <仓库名称>

# 第二步:添加文件到暂存区
git add .

# 第三步:提交文件到本地仓库
git commit -m "首次提交"

# 第四步:将本地仓库关联到远程仓库
git remote add origin <远程仓库地址>

# 第五步:推送代码到远程仓库
git push -u origin master

关于add

# 添加所有文件到暂存区
git add .

# 添加指定文件到暂存区
git add 文件名

# 添加所有文件到暂存区并提交
git commit -a -m "提交说明"

# 添加特定类型文件到暂存区
git add *.txt

# 使用交互式命令添加文件到暂存区
git add -p

关于克隆

# 克隆远程仓库到本地
git clone <远程仓库地址>

# 克隆指定分支
git clone -b <分支名> <远程仓库地址>

# 克隆到指定目录
git clone <远程仓库地址> <本地目录>

# 浅克隆
git clone --depth=1 <远程仓库地址>

# 克隆指定版本
git clone -b <分支名> <远程仓库地址> --single-branch

关于pull

# 拉取远程仓库代码到本地
git pull

# 拉取远程仓库代码到本地并合并
git pull --rebase

关于push

# 推送本地代码到远程仓库    
git push origin master

# 推送本地代码到远程仓库并覆盖远程仓库代码
git push -f origin master

# 推送本地分支到远程仓库
git push origin <分支名>

# 推送本地分支到远程仓库并覆盖远程仓库代码
git push -f origin <分支名>

关于分支

# 创建分支
git branch <分支名>

# 切换分支
git checkout <分支名>

# 合并分支  
# 合并指定分支到当前分支
git merge <分支名>

# 合并指定分支到指定分支
git merge <分支名> <目标分支名>

# 删除分支
git branch -d <分支名>

# 查看分支
git branch

# 查看所有分支
git branch -a

# 新建分支并切换到该分支
git checkout -b <分支名>

# 推送分支到远程仓库
git push origin <分支名>

# 拉取远程分支到本地
git pull origin <分支名>

关于token

# 使用token推送
git push https://<token>@github.com/username/repository.git

# 缓存token(无需每次推送输入)
git config --global credential.helper cache

# 取消缓存token
git config --global --unset credential.helper

js发送请求(仅作为模板):

读取数据:

const repoOwner = 'username'; // 仓库拥有者
const repoName = 'repository-name'; // 仓库名称
const url = `https://api.github.com/repos/${repoOwner}/${repoName}`;

fetch(url)
    .then(response => {
        if (!response.ok) {
            throw new Error('网络响应不正常');
        }
        return response.json();
    })
    .then(data => {
        console.log(data); // 在控制台输出仓库信息
    })
    .catch(error => {
        console.error('获取数据时出现错误:', error);
    });

更改数据:

const token = 'your_access_token'; // 你的GitHub访问令牌
const repoOwner = 'username'; // 仓库拥有者
const repoName = 'repository-name'; // 仓库名称
const filePath = 'path/to/newfile.txt'; // 要创建的文件路径
const content = btoa('Hello, World!'); // 文件内容,使用base64编码

const url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filePath}`;

fetch(url, {
    method: 'PUT',
    headers: {
        'Authorization': `token ${token}`,
        'Accept': 'application/vnd.github.v3+json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        message: '创建新文件',
        content: content,
        sha: '', // 如果文件已经存在,提供文件的SHA值以避免覆盖
    })
})
    .then(response => response.json())
    .then(data => {
        console.log(data); // 在控制台输出结果
    })
    .catch(error => {
        console.error('发送请求时出现错误:', error);
    });

仅供参考,使用前先验证以免出错


此方悬停
相册 小说 Ai
/*音乐播放器插件*/ /*
*/