Mac下git安装

  • A+
所属分类:网络工具

下载和安装

1.下载git客户端,下载地址为:https://git-scm.com/download/mac
2、打开安装包,可以看到此时的界面为:
Mac下git安装
点击安装,下一步下一步即可。
Mac下git安装
检查是否安装成功,出现如下说明安装成功。
zhenguodeMacBook-Pro:.ssh zhenguo$ git version
git version 2.21.0
生成密钥,进入如下目录
zhenguodeMacBook-Pro:~ zhenguo$ cd ~/.ssh
生成和查看
zhenguodeMacBook-Pro:.ssh zhenguo$ ssh-keygen -t rsa -C "lucky@centoscn.vip"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/zhenguo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/zhenguo/.ssh/id_rsa.
Your public key has been saved in /Users/zhenguo/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:DQFOfcD3guEBPg6C2g3o6iOilrXofmqLz1DS+hXRS7Y lucky@centoscn.vip
The key's randomart image is:
+---[RSA 2048]----+
| +=o. |
| o = =.o |
|o o o B..* . |
|oo + * +oo. . |
|o.+ o E S .. |
|.+ . . |
|+ + o |
|=X + |
|@BO |
+----[SHA256]-----+
zhenguodeMacBook-Pro:.ssh zhenguo$ ls
id_rsa id_rsa.pub identity identity.pub known_hosts
zhenguodeMacBook-Pro:.ssh zhenguo$ cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5ZkSqwo2zTq1G2b/pXRlnA9Bn+bOkawrkeGxeWP7qKTxzO7yX0FKPu1522guxmcMaZv37QKSYDgNUNgNcGQi7EmnBryZMpGsmnppYIpwNw8CJKYF0rYGWmABZqpP9rGbrdT0e6mTqfYwjjeH59JA4XZTVzhbNSgqJvJL8YPjBSaEMUm3YF4E7w8Kyor9+/AfCkeQT5vfAtmllD3CExUpOgPibrpk1Wqyc+//+xNOj+MKtql76fuROLvhKjnJhFxMjVTjhSq56QxF5wVjJqf6fKoJR8nPLArZsQzk4eLZ2pxFQh5113PqirjvVA0UPkZqelioO16q1qnN7ni6Bu+kH lucky@centoscn.vip

关联GitHub

github:登录账号、点击头像、选择Settings、选择SSH and GPG keys,然后new一个ssh key,名称随便起。

Mac下git安装

创建远程仓库

Mac下git安装 Mac下git安装

克隆项目到本地

zhenguodeMacBook-Pro:~ zhenguo$ cd Downloads/
zhenguodeMacBook-Pro:Downloads zhenguo$ git clone https://github.com/cncentoscn/Mac.git
Cloning into 'Mac'...
warning: You appear to have cloned an empty repository.

提交内容到服务端

zhenguodeMacBook-Pro:Downloads zhenguo$ cd Mac/
zhenguodeMacBook-Pro:Mac zhenguo$ echo "# www.centoscn.cn" >> README.md
zhenguodeMacBook-Pro:Mac zhenguo$ git init
Reinitialized existing Git repository in /Users/zhenguo/Downloads/Mac/.git/
zhenguodeMacBook-Pro:Mac zhenguo$ git add README.md
zhenguodeMacBook-Pro:Mac zhenguo$ git config --global user.name "lucky"
zhenguodeMacBook-Pro:Mac zhenguo$ git config --global user.email lucky@centoscn.cn
zhenguodeMacBook-Pro:Mac zhenguo$ git commit -m "first commit"
zhenguodeMacBook-Pro:Mac zhenguo$ git push -u origin master
Username for 'https://github.com': cncentoscn
Password for 'https://cncentoscn@github.com': 
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 248 bytes | 248.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/cncentoscn/Mac.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

 

免密码验证

zhenguodeMacBook-Pro:Mac zhenguo$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi cncentoscn! You've successfully authenticated, but GitHub does not provide shell access.

再次提交就不需要密码了

zhenguodeMacBook-Pro:Mac zhenguo$ vi README.md 
zhenguodeMacBook-Pro:Mac zhenguo$ git add README.md
zhenguodeMacBook-Pro:Mac zhenguo$ git commit -m "first commit"
[master 336a926] first commit
1 file changed, 2 insertions(+)
zhenguodeMacBook-Pro:Mac zhenguo$ git push -u origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 259 bytes | 259.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/cncentoscn/Mac.git
dcc3d5d..336a926 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

查看分支

zhenguodeMacBook-Pro:Mac zhenguo$ git branch
* master
zhenguodeMacBook-Pro:Mac zhenguo$ git branch -a
* master
remotes/origin/master

切换分之

zhenguodeMacBook-Pro:Mac zhenguo$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.

查看所有分支及其最近一条提交记录

zhenguodeMacBook-Pro:Mac zhenguo$ git branch -av
* master 336a926 first commit
remotes/origin/master 336a926 first commit
zhenguodeMacBook-Pro:Mac zhenguo$

提交修改

git add a // 只暂存a文件,如果当本修改了好多文件但你只想提交一个,可以使用这个命令 
git add . // 暂存全部文件

当然,对应的,暂存后突然不想暂存了,或者说东西打包好了突然不想给人了,可以拆开嘛,也就是撤销add操作,对应如下两条命令

git reset a // 撤销add a文件
git reset . // 撤销add 所有文件

可以试验一下,暂存后再通过git status查看状态,结果是不一样的
执行如下命令,提交本次改动

git commit -m "本次修改描述信息"

后面的描述信息是提交时一般要备注的,提交时没必要加双引号。当然,若你有复杂的提交信息,可以通过以下命令,在vim里操作,执行后跟普通的vim操作一样了

git commit -s

到此,我们本地仓库已经提交完成,可以通过以下命令来查询本地提交记录

git log

commit af1a469deacd3092bb994458131ae82d5babf7ca (HEAD -> master)
Author: xxxxx <xxxxx@xxx.com>
Date: Sat May 12 23:32:55 2018 +0800

测试comit vim下的操作

commit 3e79c58a6722ca56687ce30b33e3be5c0aebeaeb
Author: xxxxx <xxxxx@xxx.com>
Date: Sat May 12 23:29:36 2018 +0800

测试commit操作

commit 99c36ffaafadae131ea81bt6fb90a199b365f6c9 (origin/master)
Author: xxxxx <xxxxx@xxx.com>
Date: Fri May 11 01:36:08 2018 +0800

管理分支

创建本地分支

git checkout -b v2 // 创建新分支v2,并将当前分支切换为v2

切换分支

git checkout v1 // 从当前分支切换为v1

创建远端分支

git push origin v1:remote_branch_v1 
// 以本地分支v1为基准,创建远程分支remote_branch_v1,中间":"两边不要有空格

删除远程分支

git push origin :remote_branch_v1 // push一个空的分支覆盖掉远端分支,就是删除

从远端拉取分支到本地,并建立关系

git checkout -b v4 origin/master // v4本地原来不存在,master远端原来存在

撤销操作

人生没有后悔药,但是git有,而且很强大

git checkout . // 代码改动后,撤销所有改动
git reset a // git add a 后,撤销对a的add
git reset . // git add . 后,撤销add所有
git reset HEAD~1 // 已经commit,在当前分支回退1条commit,当然2就是2条;回退后修改内容还在本地
git revert HEAD~1 //已经push,回滚这条记录,需要注意的是执行完后需要继续执行以下三行才可以,因为远端会保留revert记录
git add .
git commit -a
git push

发表评论

您必须才能发表评论!