Mac Git 安装与使用教程

1. Git 安装

由于之前为了安装 jupyter notebook提前安装过了Xcode 及其Command Line Tools,而Xcode其实集成了Git,所以电脑本来就有git的命令,具体安装方式可详见 Xcode中Command Line Tools安装方法
我们可以通过在Terminal上输入git指令来检验是否安装成功,如果安装成功,Termial会return如下信息。

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.

2. 创建ssh keys

首先在Terminal上设置username和email,这两样是用作识别账户以及记录修改的信息

git config --global user.name "yourname" #在yourname处写登录名
git config --global email "youremail" #在youremail处写登陆邮箱

然后通过teminal创建你的ssh key

ssh-keygen -t rsa -C "youremail" #在youremail处写登陆邮箱

如果是第一次创建ssh key,界面会是

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yourlocation/.ssh/id_rsa): 

此处你可以直接enter表示同意将ssh key保存在给定的路径,或者你也可以修改路径。此处我直接保存在当前路径。成功会弹出:

Created directory '/Users/yourlocation/.ssh'

然后让你输入passphrase,用于后续每次修改时的密码一般。输入的时候可能并不会根据你输入的字符而现实密码,不要认为是电脑卡了哦。

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/yourlocation/.ssh/id_rsa.
Your public key has been saved in /Users/yourlocation/.ssh/id_rsa.pub.
The key fingerprint is:
......
The key's randomart image is:
......

3. 配置Git

我们需要将key里面的内容复制出来,可以去当前路径找,也可以在Terminal里面输入:

cat .ssh/id_rsa.pub #需要输入当前路径

或者

open .ssh/id_rsa.pub #需要输入当前路径

copy之后登陆Github官网,然后创建或者登陆自己的账户,在“settings”里面找到ssh key, 然后创建新的ssh key(create new ssh key), 将copy的内容paste到相应位置,并给该key取个能识别的名字,方便分辨,创建保存即可。

4. 验证是否链接

再打开Terminal,输入

ssh -T git@github.com

Terminal显示链接成功。
或者Terminal 如果 return

The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is XXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

输入yes,terminal会return

Hi XXX! You've successfully authenticated, but GitHub does not provide shell access.

证明已经链接成功。

5. Clone 项目

打开终端,想把工程克隆到某一路径,需要先切换到该路径

  1. 直接创建并进入该路径,每次文件夹只能创建一层
mkdir /Users/XXX/Documents/Github -p
  1. 若是切换到已有路径
cd /Users/XXX/Documents/Github
  1. Clone Github项目到当前路径
git clone git@github.com:Jason24-Zeng/Leetcode.git
  1. 电脑自动开始Git
Cloning into 'Leetcode'...
remote: Enumerating objects: 220, done.
remote: Counting objects: 100% (220/220), done.
remote: Compressing objects: 100% (118/118), done.
Receiving objects: 100% (220/220), 448.52 KiB | 3.00 KiB/s, done.
remote: Total 220 (delta 133), reused 183 (delta 99), pack-reused 0
Resolving deltas: 100% (133/133), done.

6. 修改并上传项目

  1. 同样需要切换至已经本地修改项目的路径
cd /Users/XXX/Documents/Github/Leetcode
  1. 将文件添加至仓库(如果提交所有文件,则可以直接用.,这表示该文件夹下的所有文件)
git add .
  1. 提交给仓库,需要用commit操作
git commit -m "Readme" #Readme处表示你对你上传文件的注释,个人有时喜欢用日期
  1. 上传
git push

或者强制合并

git push -f origin master

Terminal 最终会 return

(base) zijianzeng@Zijians-MacBook-Pro Leetcode % git add .
(base) zijianzeng@Zijians-MacBook-Pro Leetcode % git commit -m "Updata Readme"
[master 90dd067] Updata Readme
 1 file changed, 3 insertions(+), 1 deletion(-)
(base) zijianzeng@Zijians-MacBook-Pro Leetcode % git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 490 bytes | 490.00 KiB/s, done.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值