网络知识 娱乐 macos系统如何安装与使用git

macos系统如何安装与使用git

1.安装

MAC 上安装Git主要有两种方式

首先查看电脑是否安装Git,终端输入:

git

安装会输出:

WMBdeMacBook-Pro:~ WENBO$ gitnusage: git [--version] [--help] [-C <path>] [-c name=value]n [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]n [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]n [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]n <command> [<args>]nnThese are common Git commands used in various situations:nnstart a working area (see also: git help tutorial)n clone Clone a repository into a new directoryn init Create an empty Git repository or reinitialize an existing onennwork on the current change (see also: git help everyday)n add Add file contents to the indexn mv Move or rename a file, a directory, or a symlinkn reset Reset current HEAD to the specified staten rm Remove files from the working tree and from the indexnnexamine the history and state (see also: git help revisions)n bisect Use binary search to find the commit that introduced a bugn grep Print lines matching a patternn log Show commit logsn show Show various types of objectsn status Show the working tree statusnngrow, mark and tweak your common historyn branch List, create, or delete branchesn checkout Switch branches or restore working tree filesn commit Record changes to the repositoryn diff Show changes between commits, commit and working tree, etcn merge Join two or more development histories togethern rebase Reapply commits on top of another base tipn tag Create, list, delete or verify a tag object signed with GPGnncollaborate (see also: git help workflows)n fetch Download objects and refs from another repositoryn pull Fetch from and integrate with another repository or a local branchn push Update remote refs along with associated objectsnn'git help -a' and 'git help -g' list available subcommands and somenconcept guides. See 'git help <command>' or 'git help <concept>'nto read about a specific subcommand or concept.

1.1通过homebrew安装Git

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

2、通过Xcode安装

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


二、创建ssh key、配置git

  • 1、设置username和email(github每次commit都会记录他们)
  • git config --global user.name "wenbo" git config --global user.email "1050794513@qq.com"
  • 2、通过终端命令创建ssh key
  • ssh-keygen -t rsa -C "1050794513@qq.com"
  • 1050794513@qq.com是我的邮件名,回车会有以下输出
  • Last login: Sat Jan 6 14:12:16 on ttys000 WMBdeMacBook-Pro:~ WENBO$ ssh-keygen -t rsa -C "1050794513@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/WENBO/.ssh/id_rsa): /Users/WENBO/.ssh/id_rsa already exists. Overwrite (y/n)? n WMBdeMacBook-Pro:~ WENBO$
  • 由于这里我原来已经创建过,这里我选n,没有创建过的,会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。
    终端查看.ssh/id_rsa.pub文件

  • open .ssh/id_rsa.pub
  • 回车后,就会新弹出一个终端,然后复制里面的key。
    或者用cat命令查看
  • cat .ssh/id_rsa.pub
  • 3、登录GitHub(默认你已经注册了GitHub账号),添加ssh key,点击Settings,

点击New SSH key,

添加key,如下图

  • 4、链接验证
  • ssh -T git@github.com

终端输出结果

Last login: Sat Jan 6 14:42:55 on ttys000nWMBdeMacBook-Pro:~ WENBO$ ssh -T git@github.com nHi wenmobo! You've successfully authenticated, but GitHub does not provide shell access.nWMBdeMacBook-Pro:~ WENBO$

说明已经链接成功。

三、提交本地项目到GitHub

1、在GitHub上新创建一个 repository或者Start a Project,如图:

2、填写项目信息,如下图所示:

  • 点击Create repository,就创好一个工程了。
  • 3、Clone工程到本地,首先复制ssh 地址

打开终端,这里只是测试,我想把工程克隆在桌面,首先在终端中切换路径到桌面,输入以下命令:

cd /Users/WENBO/Desktop/

然后克隆项目,终端输入

git clone git@github.com:wenmobo/LearnGit.git

git@github.com:wenmobo/LearnGit.git是刚刚复制的ssh路径。
终端完整输出如下:

Last login: Sat Jan 6 15:17:17 on ttys000nWMBdeMacBook-Pro:~ WENBO$ cd /Users/WENBO/Desktop/nWMBdeMacBook-Pro:Desktop WENBO$ git clone git@github.com:wenmobo/LearnGit.gitnCloning into 'LearnGit'...nremote: Counting objects: 5, done.nremote: Compressing objects: 100% (4/4), done.nremote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0nReceiving objects: 100% (5/5), 5.2

这时,工程已经被克隆到桌面了,如下图:

4、在Xcode中新创建一个工程,保存的路径为刚刚克隆下来的LearnGit文件夹下,如下图所示:

  • 5、提交修改,首先切换到LearnGit文件路径:

cd /Users/WENBO/Desktop/LearnGit

然后输入:

//文件添加到仓库(.代表提交所有文件)ngit add .n//把文件提交到仓库ngit commit -m "First Commit"n//上传到githubngit push

终端完整输出如下:

Last login: Sat Jan 6 15:49:54 on ttys000nWMBdeMacBook-Pro:~ WENBO$ cd /Users/WENBO/Desktop/LearnGit nWMBdeMacBook-Pro:LearnGit WENBO$ git add .nWMBdeMacBook-Pro:LearnGit WENBO$ git commit -m "First Commit"n[master ae3bbe9] First Commitn 11 files changed, 649 insertions(+)n create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.pbxprojn create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedatan create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.hn create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.mn create mode 100644 LearnGitDemo/LearnGitDemo/Assets.xcassets/AppIcon.appiconset/Contents.jsonn create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/LaunchScreen.storyboardn create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/Main.storyboardn create mode 100644 LearnGitDemo/LearnGitDemo/Info.plistn create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.hn create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.mn create mode 100644 LearnGitDemo/LearnGitDemo/main.mnWMBdeMacBook-Pro:LearnGit WENBO$ git pushnWarning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.nCounting objects: 20, done.nDelta compression using up to 4 threads.nCompressing objects: 100% (18/18), done.nWriting objects: 100% (20/20), 6.80 KiB | 0 bytes/s, done.nTotal 20 (delta 2), reused 0 (delta 0)nremote: Resolving deltas: 100% (2/2), done.nTo github.com:wenmobo/LearnGit.gitn 1000218..ae3bbe9 master -> masternWMBdeMacBook-Pro:LearnGit WENBO$

查看GitHub上的项目,LearnGit已经上传成功啦,如下图所示: