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中配置

相关推荐
CoderJia程序员甲3 小时前
GitHub 热榜项目 - 日榜(2026-05-03)
ai·大模型·llm·github·ai教程
yyuuuzz4 小时前
aws 基础认知与实践注意点
运维·服务器·网络·云计算·github·aws
weixin_514253185 小时前
430-github-aguvis tmux
github
阿里嘎多学长6 小时前
2026-05-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
努力努力再努力wz6 小时前
【MySQL进阶系列】一文打通事务机制:从锁、Undo Log 到 MVCC 与隔离级别
c语言·数据结构·数据库·c++·mysql·算法·github
武超杰6 小时前
Git 从入门到精通教程
git
程序员夏末7 小时前
【开源经历 | 第一篇】参与开源需要掌握的Git和Github指令
git·开源
钱塘江渔夫8 小时前
一键式解决Windows访问github.com不了问题
windows·github
SNOWPIAOP8 小时前
git status 出现中文乱码的解决方案等
git·乱码·postgres
Resistance丶未来9 小时前
Coding-Interview-University 学习路径实战评测
人工智能·gpt·学习·github·claude·gemini·kimi