Git教程|手把手教你使用git及github

本文详细介绍了Git的用途、工作流程,以及在Mac上的安装方法。通过Homebrew或Xcode安装Git,并展示了如何创建SSH key、配置用户信息。此外,还讲述了如何在GitHub上设置SSH key,以及将本地项目推送到GitHub的步骤。内容包括克隆、添加、提交和推送等基本Git命令的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、关于git

Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

二、Git 工作流程

git一般工作流程如下:

  1. 克隆 Git 资源作为工作目录。
  2. 在克隆的资源上添加或修改文件。
  3. 如果其他人修改了,你可以更新资源。
  4. 在提交前查看修改。
  5. 提交修改。
  6. 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

三、Git 下载

首先我们来查看自己的电脑是否安装了git,在终端中输入git,安装过则会输出:

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

3.1Mac中关于git的安装

1、通过homebrew安装Git

  • 若未安装homebrew,需安装homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安装git
brew install git

2、通过Xcode安装
直接从AppStore安装Xcode,Xcode集成了Git,不过默认没有安装,你需要运行Xcode,选择菜单“Xcode”->“Preferences”,在弹出窗口中找到“Downloads”,选择“Command Line Tools”,点“Install”就可以完成安装了。

3、在Mac的终端上直接安装git

https://git-scm.com/downloads

四、Git 的使用

4.1创建ssh key、配置git

  • 1、设置 username 和 email(github每次commit都会记录他们)
git config --global user.name "Viviana-0"
git config --global user.email "自己的邮箱地址@qq.com"

⚠️name和email后面一定要有空格!空格!空格!!!

  • 2、通过终端命令创建ssh
ssh-keygen -t rsa -C "自己的邮箱地址@qq.com"

在这里插入图片描述

由于我之前没有创建过的,会要求确认路径和输入密码,我们在这里使用默认的一路回车就行。若安装过会出现"… already exists" "Overwrite(y/n)"输入n即可
在这里插入图片描述
成功的话会在~/下生成.ssh文件夹,进入文件夹,打开id_rsa.pub,复制里面的key。

  • 3、使用终端查看.ssh/id_rsa.pub文件
open .ssh/id_rsa.pub 

回车后,就会新弹出一个终端,然后复制里面的key.

⚠️若出现 No application knows how to open /Users/wht/.ssh/id_rsa.pub.可以使用 cat 命令操作进行查看。

cat .ssh/id_rsa.pub

在这里插入图片描述

4.2通过Github使用git命令

  • 1、我们首先登陆github,登录成功后,添加ssh key,点击Settings。
    在这里插入图片描述
  • 2、进入设置页面,首先点击 SSH and GPG keys ,再点击 new SSH key 添加key。

在这里插入图片描述

  • 3、接下来填写一个title,然后将刚才在终端复制的key 粘贴进来。
    在这里插入图片描述

  • 4、由于我此前使用过windows电脑使用过ssh,需要重新登录一次
    在这里插入图片描述

  • 5、接下来我们使用终端进行链接验证

ssh -T git@github.com

若提示 Are you sure you want to continue connecting (yes/no/[fingerprint])? 输入 yes
在这里插入图片描述

4.3将本地项目提交到GitHub

  • 1、点击Start a Project,在gtihub上创建一下新的项目
    在这里插入图片描述
  • 2.填写项目信息。点击Create repository,创好一个新的工程。

在这里插入图片描述

3、点击复制自己的"Use SSH",复制自己的ssh 地址。

在这里插入图片描述

  • 4、打开终端,这里进行测试
    • 1⃣️首先将路径切换到桌面
cd /Users/viviana/Desktop/
  • 2⃣️然后将github的项目克隆到本地,复制粘贴到终端

在这里插入图片描述

  • 3⃣️我们可以在桌面清楚的看到自己的项目

在这里插入图片描述

  • 4⃣️这时,我们可以通过pycharm进行测试,我们打开这个克隆的项目

在这里插入图片描述

  • 5⃣️新建一个.py进行验证

在这里插入图片描述

  • 6⃣️打开终端,将终端路径切换到该项目
cd /Users/viviana/Desktop/test/
  • 7⃣️接着输入:
git add .  # 文件添加到仓库(.代表提交所有文件)

git commit -m "First Commit"  # 把文件提交到仓库,""引号下的内容是标注的信息

git push  # 上传到github

在这里插入图片描述

  • 8⃣️这时,到github上进行查看
    在这里插入图片描述

五、Git常用命令速查表

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值