一.检查Git

使用Git前先检查本机上是否安装了Git,Mac上默认都是安装了Git的

1.打开终端,输入指令,如果已经安装了Git就会显示版本号



git version



 

2.如果尚未安装Git,可以通过Xcode的Command Line Tools并使用如下命令安装Git



xcode-select --install



 

 

二.新建项目并上传

1.在远程第三方托管网站上新建一个远程仓库(Github、码云等)

苹果电脑gitlab工具 mac的git工具_git

 

2.本地创建一个新的项目,以Xcode iOS为例,新建项目时要注意项目内是否由一个.git的Git文件,作为新项目上传时需要先删除该文件

苹果电脑gitlab工具 mac的git工具_苹果电脑gitlab工具_02

 

3.打开终端,通过cd指令进入该文件夹下,初始化一个新的Git



git init 



 

4.添加当前文件夹目录下所有文件到Git



git add .



 

5.先提交项目到本地仓库



git commit -m "project"



 

6.然后推送本地仓库到远程仓库,其中http://xxxxxxxx为远程仓库地址,通常需要在该地址后添加.git链接到Git仓库,如果是首次推送,还需要输出远程仓库的Git用户名和密码来进行验证



git remote add origin http://XXXXXXX.git



git push -u origin master



注:该步常见错误为远程仓库在创建时通常会新建一个readme文件,导致因远程仓库存在本地不存在的文件而上传失败

To https://gitee.com/leisurezxy/iostest.git
        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/leisurezxy/iostest.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法为先执行pull命令,合并本地不存在的readme文件,再执行git push -u origin master命令即可



git pull --rebase origin master



 

 

  

三.从Git上获取已有的项目,修改后并提交

1.根据已有项目的Git地址,使用命令行工具从远程克隆一个仓库到本地



git clone https://xxxxxxx.git



  

2.修改完项目内容后,使用命令行工具进入项目文件夹,先执行如下两条命令将项目提交到本地仓库



git add .



git commit -m "project"



 

3.然后执行push命令将项目同步到远程Git仓库



git push 



注:该步常见问题为mac git设置的全局用户名和密码与该远程仓库使用的用户名密码不相符,需要通过如下命令进行修改,使用后系统会提示输入密码



git config --global user.name "用户名"



git config --global user.email "邮箱"