欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mac上Git安装与配置

程序员文章站 2024-03-23 20:27:40
...

目录

前言
本文章所展示的如何Git安装与配置的具体步骤,第一次在CNDS上发布处女文章 (有点小激动),如有做的不足的地方还请大佬们指正。

一、Git安装 – Mac OS


Git是一个分布式版本控制软件,最初由林纳斯·托瓦兹创作,于2005年以GPL发布。

Mac上 Git下载有两种方式

1. 直接安装

  • 官方下载安装包https://git-scm.com/download/mac
    Mac上Git安装与配置
  • 请单击此处click here to download manually跳转到下载页面,等待五秒钟后就会跳出下载文件目录,如果没反应的话直接复制该网址https://sourceforge.net/projects/git-osx-installer/单击Download跳转到下载页面
    Mac上Git安装与配置
  • 下载完成后双击.dmg文件打开。由于是在网站上下载的,需要获取本地的信任~~(疯狂吐槽)~~ 单击左上角苹果图标,选择系统偏好设置,进入安全与个人隐私
    Mac上Git安装与配置
  • 安装步骤 ,没有什么需求的话,一直默认继续
    Mac上Git安装与配置
  • 安装完成后 打开Terminal终端,输入git命令,如果出现一下内容即安装成功
$ git 
MacBookdeMBP:~ macbookpro$ 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
 reset      Reset current HEAD to the specified state
 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
 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
 checkout   Switch branches or restore working tree files
 commit     Record changes to the repository
 diff       Show changes between commits, commit and working tree, etc
 merge      Join two or more development histories together
 rebase     Reapply commits on top of another base tip
 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.

2. 用homebrew指令下载1**

  • 配置个人的用户名称和电子邮件地址:
  • 首先需要安装homebrew
  • 然后在终端执行后面指令安装,homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 完成后,使用以后指令安装Git,brew install git

二、Git 配置

1. 用户账号信息

  • 配置个人的用户名称和电子邮件地址,以后每次与Git的交互都会使用该信息
$ git config --global user.name "Your Name" //其中Your Name即是你的GitHub账号
$ git config --global user.email "aaa@qq.com" //其中aaa@qq.com即是你GitHub账号绑定的邮箱
  • 配置信息可以更改,以后想要更改使用上面指令就可以。同时可以使用git config --list指令查看Git的配置信息。
  • Git默认是大小写不敏感的,也就是说,将一个文件名某个字母做了大小写转换的修改Git是忽略这个改动的,导致在同步代码时候会出现错误,所以建议大小把Git设置成大小写敏感。
$ git config core.ignorecase false

2. 生成公钥/**(ssh key)

  • Git关联远端仓库时候需要提供公钥,本地保存私钥,每次与远端仓库交互时候,远端仓库会用公钥来验证交互者身份。使用以下指令生成**。
$ ssh-****** -t rsa -C "aaa@qq.com"
  • 回车会有以下输出
MacBookdeMBP:~ macbookpro$ ssh-****** -t rsa -C "aaa@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/macbookpro/.ssh/id_rsa): 
/Users/macbookpro/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/macbookpro/.ssh/id_rsa.
Your public key has been saved in /Users/macbookpro/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:sjlsPFZ4TngGjUwio/cnRm1xLrzZzpSgbTjTxBCgtd0 1127551356@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| 不用复制我这个哈   |
| 单纯给你们看一下   |
|o o o E o        |
| . o B X .       |
|    O @ S        |
|   . O #         |
|      X +        |
|     o o         |
|                 |
+----[SHA256]-----+

如果没有创建过的,会要求确认路径和输入密码,使用默认的路回车就行

  • 成功的话会在~/下生成.ssh文件夹
$ cat .ssh/id_rsa.pub//使用cat命令查看生成的**

Mac上Git安装与配置
或者用vim命令查看

$ vim .ssh/id_rsa.pub 

复制完好后输入Shift + zz 退出vim模式

部分Mac系统没有安装编辑器,输入open .ssh/id_rsa.pub命令可能打不开该文件。

3. 远程存储库添加**

  • 登录GitHub(如果没有的话要去注册一个喔),添加ssh key;先点击Settings–>再次单击SHH and GPG keys–>最后单击New SSH Key,在Title输入你的标题,在Key中输入刚刚复制的**
    Mac上Git安装与配置

4. 链接认证

 $ ssh -T aaa@qq.com.com 
  • 终端输出结果
MacBookdeMBP:~ macbookpro$ ssh -T aaa@qq.com.com 
Enter passphrase for key '/Users/macbookpro/.ssh/id_rsa': 
Hi chang-dian! You've successfully authenticated, but GitHub does not provide shell access.

  • 如果出现上面显示的说明已经链接成功。

三、提交本地项目到GitHub

1. 在GitHub上新建一个 Start a Project 项目
Mac上Git安装与配置

  • 填写项目信息,如下图所示:
    Mac上Git安装与配置
    点击Create repository,就创建一个项目了。

2. 远程下载项目到本地

  • 复制SHHaaa@qq.com:用户名/项目名称.git
    Mac上Git安装与配置
  • 打开终端,把克隆在桌面,先在终端中切换路径到桌面,输入以下命令:
$ cd /Users/主机账户名/Desktop/   //主机账户名也就是你现在用的电脑的用户名
  • 然后克隆项目,终端输入
$ git clone aaa@qq.com.com:用户名/项目名称.git //粘贴刚刚的创建项目的链接

本次演示的项目链接:aaa@qq.com:chang-dian/Hello-World.git

回车,提示输入密码,认证成功后自动下载文件,终端完整输入如下

MacBookdeMBP:Hello-World macbookpro$ git clone aaa@qq.com.com:chang-dian/Hello-World.git
Cloning into 'Hello-World'...
Enter passphrase for key '/Users/macbookpro/.ssh/id_rsa': 
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 13 (delta 1), reused 8 (delta 1), pack-reused 0
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (1/1), done.

这时项目文件已经下载到桌面了。
3. 新建一个文件(我用的是VS code),保存的路径到克隆下来的Hello-World文件夹下
Mac上Git安装与配置
4. 在终端切换到你要提交的项目的目录(请切换到你的项目实际目录位置)

$ cd /Users/macbookpro/Desktop/Hello-World/

5. 把项目提交到git上

  • 在当前项目的目录中依次执行以下命令
1.$ git init				//git初始化,创建一个empty Git repository 
2.$ git add .				//添加当前目录及其中所有子目录及文件(.代表提交所有文件)
3.$ git commit -m "新建项目"	//提交到本地仓库
4.$ ggit push	//推送到远程仓库
  • 终端完整输出如下:
MacBookdeMBP:Hello-World macbookpro$ git commit -m '新建READMD'
[master cf89df4] 新建READMD
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
MacBookdeMBP:Hello-World macbookpro$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 374 bytes | 374.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/chang-dian/Hello-World.git
   6bf3278..cf89df4  master -> master

  • 查看 GitHub 项目,Hello- World 中的项目已经上传成功,如下图所示
    Mac上Git安装与配置

  1. 内容出处:https://blog.csdn.net/xiaohanluo/article/details/53214933 ↩︎

相关标签: GitHub 教程