GitHub介绍

GitHub 是一个基于 Git 的在线代码托管平台,用于版本控制和协作。下面是一些常用的 Git 命令,这些命令可以帮助你在本地和 GitHub 之间管理和同步代码仓库:

初始化和配置

  1. 初始化本地仓库

    bash 复制代码
    git init

    这将在当前目录下创建一个新的 Git 仓库。

  2. 配置用户信息

    bash 复制代码
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"

    这将设置全局的用户名和邮箱,用于识别提交者身份。

添加和提交更改

  1. 添加文件到暂存区

    bash 复制代码
    git add <file>

    或者添加所有更改的文件:

    bash 复制代码
    git add .
  2. 查看状态

    bash 复制代码
    git status

    显示工作目录和暂存区的状态。

  3. 提交更改

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

    提交暂存区的所有更改,并附带一条描述性的提交信息。

远程仓库操作

  1. 添加远程仓库

    bash 复制代码
    git remote add origin https://github.com/username/repository.git
  2. 推送更改到远程仓库

    bash 复制代码
    git push -u origin main

    第一次推送时,使用 -u 参数将本地分支与远程分支关联。

  3. 拉取远程仓库的更改

    bash 复制代码
    git pull origin main
  4. 克隆远程仓库

    bash 复制代码
    git clone https://github.com/username/repository.git

分支管理

  1. 创建并切换到新分支

    bash 复制代码
    git checkout -b new-feature
  2. 合并分支

    bash 复制代码
    git merge new-feature
  3. 删除本地分支

    bash 复制代码
    git branch -d new-feature
  4. 删除远程分支

    bash 复制代码
    git push origin --delete new-feature

查看历史

  1. 查看提交历史

    bash 复制代码
    git log
  2. 查看某次提交详情

    bash 复制代码
    git show <commit-hash>

解决冲突

  1. 解决合并冲突
    在编辑完冲突文件后,使用 git add 和 git commit 完成合并。

其他命令

  1. 撤销更改

    如果你想撤销上次提交,可以使用:

    bash 复制代码
    git reset HEAD~1
  2. 查看远程仓库信息

    bash 复制代码
    git remote -v
  3. 重置分支到特定提交

    bash 复制代码
    git reset --hard <commit-hash>

以上只是 Git 的一部分常用命令,实际上 Git 提供了非常丰富的功能集,包括标签、补丁、钩子、变基(rebase)等高级特性。如果你想了解更多,可以查阅官方文档或使用 man git 和 git help <command> 获取详细信息。

相关推荐
逛逛GitHub5 小时前
民间大神移植《GTA 6》进度已经到 90% 了,这几个 GitHub 项目关注一下
github
解道Jdon7 小时前
Rust重写OpenCode Rune:性能提升最高100倍
ide·windows·git·svn·eclipse·github·visual studio
徐小夕9 小时前
从 RAG 工具到文档处理平台,聊聊 LlamaIndex 为什么成了 AI 应用的标配底座
前端·vue.js·github
jasmine02449 小时前
GitHub 开源背单词游戏:四六级考研词库,拼写正确就能攻击
游戏·github·工具类
钱栈up12 小时前
GitHub 热榜上手|OpenMausBot:一周+7077星的本地 Agent 聊天客户端
typescript·开源·github
秋千易1 天前
同一提交,旧基线红、新基线绿:基线放行了门禁
github
撸码猿1 天前
1Password 后端被入侵:密码库为什么没被拿走
网络安全·github·供应链安全·钓鱼·1password
一条Go1 天前
26 篇之后:Alioth 的 26 个设计决定
后端·github
一条Go1 天前
开源新生代雷达 · 第 25 期
github