Mac下安装使用Git--学习笔记

Git安装配置

Git是一个分布式版本控制系统
Git作用:
1.方便维护版本(项目/时间维度)
2.恢复到某一版本的功能
3.在不同版本间,比较差异点
4.方便进行版本并行
5.项目合作能力
mac下安装 git
下载地址:https://git-scm.com/downloads
下载对应安装包,安装
安装好以后在终端输入 git --version 查看版本

user:test ss$ git --version
git version 2.10.1 (Apple Git-78)

配置用户名邮箱信息

git config --global user.name “name” #姓名
git config --global user.email “xxx@mail.com” #邮箱
git config --global core.autocrlf true #自动处理换行符


查看配置信息

git config --list

使用 Git

一、工作区
什么是工作区?
指定一个目录/myGit,执行 git init ,那么这个目录下可以直接修改的文件所在的空间就叫做工作区

进入/myGit ,并将当前目录初始化为git工作区 命令:git init

user:Documents ss$ cd myGit/
user:myGit ss$ git init
Initialized empty Git repository in /Users/ss/Documents/myGit/.git/
user:myGit ss$ 

执行完成后,在当前目录下会产生一个隐藏目录/.git,这个目录也叫版本库
二、缓存区
缓存区属于版本库中的一部分空间,使用 git add 命令可以将我们在工作区对文件的改动操作同步到缓存区

将文件添加到缓存区 命令:git add xxx.xxx
添加前先看看缓存区状态,缓存区目前里面没有要提交的文件信息

user:myGit ss$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store
    apiAutomation/
    test.py
    test_ss.py

nothing added to commit but untracked files present (use "git add" to track)

通过 git add test.py 将 test.py 添加到缓存区

user:myGit ss$ git add test.py 
warning: LF will be replaced by CRLF in test.py.
The file will have its original line endings in your working directory.

再次查看缓存区状态,里面有一个新加的文件 new file: test.py
缓存区可以接受多次工作区的改动操作,最后一起commit 到本地仓库


三、本地仓库
存放已经提交的信息,也可以理解为版本库

将缓存区的文件提交到本地仓库 命令:git commit -m 提交信息

四、远程仓库
像网络上的仓库 GitHub,GitLab,或者公司的 Git 就是远程仓库
如何将代码提交到远程仓库?以 github为例
1.在 GitHub 上创建一个仓库(没有账号先注册一个https://github.com/


2.获取远程仓库路径
点击创建的仓库名,进入如下页面

3.上传/myGit 目录下提交的代码到远程仓库
复制上图下方三行代码,依次执行

# 本地仓库与远端仓库建立一个链接,远程库的名字就是origin
git remote add origin git@github.com:xxx/xxx.git
# 重新命名新分支为 main
git branch -M main
# 将本地仓库的内容推送到远程仓库,实际上是把当前分支推送到远程 main 上
git push -u origin main

git push -u origin main 第一次上传可能会报如下错误

user:myGit ss$ git remote add origin git@github.com:xxx/xxx.git
user:myGit ss$ git branch -M main
use:myGit ss$ git push -u origin main
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARxxxxxxxxxx
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

解决方案:
网上随便找一下方案发现是RSA key问题
第一次同步仓库需要有一个密钥同步,本地需要生成一个公钥,然后把这个公钥添加Github的秘钥管理中,网上教程很多
参考:https://blog.csdn.net/smile0125/article/details/80885064
解决秘钥问题后,重新执行上述命令

user:myGit ss$ git push -u origin main
Warning: Permanently added the RSA host key for IP address '12.229.18.59' to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 488 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:xxx/xxx.git
 * [new branch]      main -> main
Branch main set up to track remote branch main from origin.

成功,到 GitHub 查看内容


第一次推送时,需要加上-u参数,Git不但会把本地的main分支内容推送到远程新的main分支,还会把本地的 main分支和远程的main分支关联,以后可以直接git push

user:myGit ss$ git add test_ss.py 
warning: LF will be replaced by CRLF in test_ss.py.
The file will have its original line endings in your working directory.
user:myGit ss$ git commit -m second-test
[main 4e00259] second-test
 1 file changed, 29 insertions(+)
 create mode 100644 test_ss.py
user:myGit ss$ git push
Warning: Permanently added the RSA host key for IP address '111.250.17.23' to the list of known hosts.
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 244 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:xxx/xxx.git
   4029098..4e00259  main -> main
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. 重要的参考资料 git干货系列 二. 常用命令汇总 git初始设置 git config --global ...
    午觉不眠Orz阅读 239评论 0 0
  • 打开终端,cd到你想要创建git仓库的目录下,通过git init命令就可以把这个目录变成git可以管理的仓库;这...
    zziazm阅读 95评论 0 0
  • 简书本身不支持生成目录,自己通过pandoc生成的目录在简书也变成了新页面打开。心好累,推荐大家用这个简书文章左侧...
    mkitclear阅读 404评论 0 0
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,753评论 0 5
  • 昨天,在回家的路上,坐在车里悠哉悠哉地看着三毛的《撒哈拉沙漠的故事》,我被里面的内容深深吸引住了,尽管上学时...
    夜阑晓语阅读 3,835评论 2 9