git和github的使用指南

目录

1.git初始化本地仓库

2.远程仓库

3.如何将自己的代码上传到远程仓库的某一个分支


1.git初始化本地仓库

在项目目录中初始化 Git 仓库:

bash 复制代码
cd your-project-directory
git init

将文件添加到暂存区:

bash 复制代码
git add .    //添加所有文件
git add <file-name>    //添加单个文件

提交更改

bash 复制代码
git commit -m "Your commit message"

上传到远程仓库

bash 复制代码
git push origin main

2.远程仓库

1.将本地仓库与远程仓库关联:

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

2.从远程仓库克隆到本地:

bash 复制代码
git clone <repository-url>

一般来说1,2步骤如果是初次的化执行第二个就行,无需建立本地仓库

拉取最新代码

bash 复制代码
git pull origin main

查看远程分支

bash 复制代码
git branch -r

切换目标分支

bash 复制代码
git checkout <branch-name>

拉取指定分支代码

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

3.如何将自己的代码上传到远程仓库的某一个分支

完整步骤

bash 复制代码
//如需克隆某个分支的代码可以改成git clone -b <branch-name> <repository-url>
git clone <repository-url>    //克隆仓库
cd repository                 //进入仓库
git add <file-name>           //提交更改到暂存区
git commit -m "Your commit message"    //将暂存区的文件合并到本地仓库
git push origin <branch-name>          //上传到远程仓库的某个分支,如果该分支不存在会自动创建

如果无法克隆仓库的代码,要先把git的ssh公钥在github中配置

相关推荐
子兮曰9 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github
Selicens17 小时前
git批量删除本地多余分支
前端·git·后端
崔庆才丨静觅1 天前
Claude Code GitHub Actions 使用教程
github·api·claude
闲云一鹤2 天前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化
砖厂小工2 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮2 天前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕2 天前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment2 天前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github