mac系统github如何clone到本地与上传

时间:2024-03-13 13:42:21

一、安装Git

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

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

git

安装过则会输出:

usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --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.
  1. 通过homebrew安装Git

  2. 未安装homebrew,需安装homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. 安装git
brew install git
  1. 通过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 "[email protected]"
  1. 通过终端命令创建ssh key
ssh-****** -t rsa -C "[email protected]"

[email protected]是邮件名,回车会有以下输出

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

由于这里我原来已经创建过,这里我选n,没有创建过的,会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。
终端查看.ssh/id_rsa.pub文件

open .ssh/id_rsa.pub`

回车后,就会新弹出一个终端,然后复制里面的key。
或者用cat命令查看

cat .ssh/id_rsa.pub
  1. 登录GitHub(默认你已经注册了GitHub账号),添加ssh key,点击Settings,如图
    mac系统github如何clone到本地与上传

点击New SSH key,如图
mac系统github如何clone到本地与上传

添加key,如图

mac系统github如何clone到本地与上传

  1. 链接验证
ssh -T [email protected] 

终端输出结果

$ ssh -T [email protected] 
Hi xxxxx You've successfully authenticated, but GitHub does not provide shell access.

说明已经链接成功。

三、提交本地项目到GitHub

  1. 在GitHub上新创建一个 repository或者Start a Project,如图:
    mac系统github如何clone到本地与上传

  2. 填写项目信息,如下图所示:
    mac系统github如何clone到本地与上传

点击Create repository,就创好一个工程了。

  1. Clone工程到本地,首先复制ssh 地址
    mac系统github如何clone到本地与上传

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

cd /Users/xxxx/Desktop/

(xxxx是自己mac的名字)

然后克隆项目,终端输入

git clone (这里是复制的SSH链接,直接command+v就可以了)

下载完成了之后,工程已经被克隆到桌面了。

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

mac系统github如何clone到本地与上传

  1. 提交修改,首先切换到LearnGit文件路径:
cd /Users/xxxx/Desktop/LearnGit 

这里xxxx是mac的名字

然后输入:

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

终端完整输出如下:

[master ae3bbe9] First Commit
 11 files changed, 649 insertions(+)
 create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.pbxproj
 create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata
 create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.h
 create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.m
 create mode 100644 LearnGitDemo/LearnGitDemo/Assets.xcassets/AppIcon.appiconset/Contents.json
 create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/LaunchScreen.storyboard
 create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/Main.storyboard
 create mode 100644 LearnGitDemo/LearnGitDemo/Info.plist
 create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.h
 create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.m
 create mode 100644 LearnGitDemo/LearnGitDemo/main.m
WMBdeMacBook-Pro:LearnGit WENBO$ git push
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (20/20), 6.80 KiB | 0 bytes/s, done.
Total 20 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:wenmobo/LearnGit.git
   1000218..ae3bbe9  master -> master

查看GitHub上的项目,LearnGit已经上传成功啦。

参考文章:
https://www.jianshu.com/p/7edb6b838a2e