本篇文章以macos为基础(windows系统同样有参考意义),介绍了git的卸载,homebrew的安装,git的安装以及利用git工具将项目代码放置github仓库。
(1)git的卸载
本人电脑很久没有用了,发现以前安装的git版本有点低了,所以决定重新安装最新的git(建议:自己学习某些工具或软件的时候可以选择它的稳定版本,因为稳定版本多数企业可能在用。或者下载最新版本试用,长江后浪推前浪,随着版本的更新功能可能会越来越完善)。
192:~ huayu$ git --version
git version 2.15.0
192:~ huayu$ which -a git
/usr/local/bin/git
/usr/bin/git
192:~ huayu$ cd /usr/local/
192:local huayu$ cd git
192:git huayu$ ls
Library contrib lib share
bin etc libexec uninstall.sh
192:git huayu$ sh uninstall.sh
This will uninstall git by removing /usr/local/git/, and symlinks
Type 'yes' if you are sure you wish to continue: yes
Password:
Forgot package 'com.git.pkg' on '/'.
Uninstalled
192:git huayu$ git --version
-bash: /usr/local/bin/git: No such file or directory
(2)homebrew的安装
homebrew是macos下的软件管理工具,类似redhat的yum,可以使用homebrew对软件安装、更新等操作。
//执行brew命令报如下错误,说明本机并没有安装homebrew
192:git huayu$ brew search wget
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Error: The current working directory doesn't exist, cannot proceed.
homebrew官网:https://brew.sh/index_zh-cn
在homebrew官网有对它详细的介绍,下面开始安装homebrew
//在终端键入以下命令对homebrew安装,期间会让输入一次密码(你的机器密码),输入完毕后等待安装完成
192:git huayu$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
等待的时间可能会很长(不确定是不是本人住所网络的原因),看到如下的图说明brew安装成功了。
(3)git工具的安装
git工具有两种安装方式:
第一种,登陆git官网https://git-scm.com/下载.dmg 文件安装(windows同理)
如果点击后仍未自动下载,点击下面的链接手动下载
点击保留dmg文件,开始下载 ,等待下载完毕
不是在apple appstore下载的软件都会提示这个东西,需要在隐私里设置一下
打开后根据后续的提示一步一步安装就可以了,在这不再赘述。
第二种,采用homebrew安装git工具
经过漫长的等待安装完毕homebrew后,使用brew安装git开始。
//输入安装命令后发现brew又有更新了,需要等待一段时间
192:~ huayu$ brew install git
Updating Homebrew...
==> Installing dependencies for git: gettext and pcre2
==> Installing git dependency: gettext
==> Downloading https://homebrew.bintray.com/bottles/gettext-0.20.1.catalina.bot
==> Downloading from https://akamai.bintray.com/10/107d7f386fbeea6979f9376cdbbcf
######################## 33.9%######################## ######################## ######################### 35.5%
....
//brew 更新完毕后会自动进入git的下载
==> Installing git
==> Downloading https://homebrew.bintray.com/bottles/git-2.23.0_1.catalina.bottl
==> Downloading from https://akamai.bintray.com/cb/cb74c5f3a0e89f7415e9846f96e2d
//下载完毕后查看git版本 使用brew安装的git路径在/usr/local/Cellar/git/2.23.0_1
//由查看的git版本可以看出brew下载的也是当前官网发布的最新版本
192:~ huayu$ git --version
git version 2.23.0
(4)使用git工具上传代码到github
1)设置用户名与邮箱
192:bin huayu$ git config --global user.name "yuhua"
192:bin huayu$ git config --global user.email "17610835230@163.com"
2)设置免密
//密匙我以前已经设置过,这次再重新覆盖一次(以前如没生存则创建,生成过则覆盖)
192:bin huayu$ ssh-keygen -t rsa -C "17610835230@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/huayu/.ssh/id_rsa):
/Users/huayu/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/huayu/.ssh/id_rsa.
Your public key has been saved in /Users/huayu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5VoKkQi6QbTYLgfGdm3zcDMwf2WaKHbhazrZe2YfYmw 17610835230@163.com
The key's randomart image is:
+---[RSA 2048]----+
|.o. o . o |
|+o....=.o = |
|==...BoO +. |
|o=. o B.=o |
|o o .+S o |
| o =..+ |
| + .oE . |
| . ++. . |
| .+ .. |
+----[SHA256]-----+
查看公钥
192:bin huayu$ cd ~/.ssh/
192:.ssh huayu$ ls
id_rsa id_rsa.pub known_hosts known_hosts.old
192:.ssh huayu$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRE1M0vB2xU6GdPGLzB3Z4x6PuFVlm3zYcfvfFtq+NcEIIycEGmg9DOglHD1mKlQPtf7eq+pd41m3kVRqy/sSgfNHy5yWX2NzxxaHaUlQ1w2QzKyURShXq7l/L8M6/vy9LFWqV/eEkdOofbj74LyscT0iR8KL8KagBgeSSthMp/UQeqwiCa4Uh6sqPfWKybcg+ShtGpXUprQHIMwqEApvpcvyHGh2hHBqAGmamgfQcHMzy4095tM4/0h/SSLA6UMPyVFsZ9MjP7gqL1xmoGJEwundwQoKxuF36jU3OD2zhXTuIdl2xV3dJwhwiLT9F+8xLppQ7PTZnT5gCaSLyg21l 17610835230@163.com
登陆自己的github账号,将生成的公钥复制到github设置中
title自定义输入,将刚刚生成的公钥内容粘贴到key的位置,添加一下。
验证设置是否成功
192:.ssh huayu$ ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' 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,52.74.223.119' (RSA) to the list of known hosts.
Enter passphrase for key '/Users/huayu/.ssh/id_rsa':
Hi Myuhua! You've successfully authenticated, but GitHub does not provide shell access.
3)在github仓库创建代码库,下图两处都可以创建新仓库,任意选择即可
public是公开,private不公开,可自行选择,填写完毕后确认创建。
到这里项目仓库就创建成功了
4)将自己的本地项目上传到github远程仓库
对于新创建的远程仓库开始使用操作一般有两种情况:
第一种是先创建新的远程仓库,将创建的仓库克隆到本地机器,编写代码后再通过一系列git的操作将代码提交到远程仓库。
这一种我将通过借助idea代码编写工具进行演示。打开idea,克隆刚创建的github远端仓库
登陆github,可通过token的形式也可通过用户名密码的形式,本处通过用户名密码进行登陆。
登陆成功后进行远程仓库链接测试
点击test,测试通过后,点击clone,将远端仓库克隆到本地机器
一直next到finish就可以,因为此处仅做演示,不必考虑项目结构。
项目clone完毕后就在idea中打开了。
修改readme文件进行代码上传远程github仓库演示
快捷键ctrl+k,将代码提交到本地仓库
快捷键ctrl+shift+k,将代码push到远端仓库。(注意:如果快捷键不生效可能是热键冲突导致,查看一下是不是与其他软件冲突,比如输入法设置的快捷键)
点击push按钮后到github仓库查看,发现代码提交成功了。第一种情况演示到此结束。
第二种情况,本地项目已经创建好,提交到远端新创建的github仓库。在第一种情况演示的时候是通过借助idea完成,第二种情况我将采用git命令的形式做演示。
//初始化仓库(重要提示:在执行以下命令前先cd到要提交项目的目录下再进行操作)
192:ailive huayu$ git init
已初始化空的 Git 仓库于 /Users/huayu/Documents/project/ailive/.git/
//提交未跟踪和修改文件,但是不处理删除文件。将文件添加到暂存区
192:ailive huayu$ git add .
//将代码提交到本地库
192:ailive huayu$ git commit -m "first commit"
[master(根提交) 5aad17e] first commit
27 files changed, 2057 insertions(+)
create mode 100644 .gitignore
create mode 100644 .mvn/wrapper/MavenWrapperDownloader.java
create mode 100644 .mvn/wrapper/maven-wrapper.jar
create mode 100644 .mvn/wrapper/maven-wrapper.properties
create mode 100644 ailive_server/.gitignore
create mode 100644 ailive_server/.mvn/wrapper/MavenWrapperDownloader.java
create mode 100644 ailive_server/.mvn/wrapper/maven-wrapper.jar
create mode 100644 ailive_server/.mvn/wrapper/maven-wrapper.properties
create mode 100644 ailive_server/mvnw
create mode 100644 ailive_server/mvnw.cmd
create mode 100644 ailive_server/pom.xml
create mode 100644 ailive_server/src/main/java/AiliveCommonApplication.java
create mode 100644 ailive_server/src/main/resources/application.properties
create mode 100644 ailive_server/src/test/java/com/example/common/AiliveCommonApplicationTests.java
create mode 100644 ailive_web/.gitignore
create mode 100644 ailive_web/.mvn/wrapper/MavenWrapperDownloader.java
create mode 100644 ailive_web/.mvn/wrapper/maven-wrapper.jar
create mode 100644 ailive_web/.mvn/wrapper/maven-wrapper.properties
create mode 100644 ailive_web/mvnw
create mode 100644 ailive_web/mvnw.cmd
create mode 100644 ailive_web/pom.xml
create mode 100644 ailive_web/src/main/java/com/example/ailive_web/AiliveWebApplication.java
create mode 100644 ailive_web/src/main/resources/application.properties
create mode 100644 ailive_web/src/test/java/com/example/ailive_web/AiliveWebApplicationTests.java
create mode 100644 mvnw
create mode 100644 mvnw.cmd
create mode 100644 pom.xml
//添加一个新的远程 Git 仓库
192:ailive huayu$ git remote add origin https://github.com/Myuhua/ailive.git
//将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git
push了。git push 是默认只推送当前分支。
192:ailive huayu$ git push -u origin master
To https://github.com/Myuhua/ailive.git
! [rejected] master -> master (fetch first)
error: 推送一些引用到 'https://github.com/Myuhua/ailive.git' 失败
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。
//用上一种方式演示的仓库再用于本次属于更新了,这样就导致了两本部分的冲突,报了以上的错误,删除旧仓库重新建一个再提交就可以了
192:ailive huayu$ git push -u origin master
枚举对象: 40, 完成.
对象计数中: 100% (40/40), 完成.
使用 8 个线程进行压缩
压缩对象中: 100% (27/27), 完成.
写入对象中: 100% (40/40), 51.26 KiB | 10.25 MiB/s, 完成.
总共 40 (差异 3),复用 0 (差异 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/Myuhua/ailive.git
* [new branch] master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'。
到此位置,代码就上传到github远程仓库了,可登上github仓库到项目下查看确认一下。
特别提示:如果项目包全都创建完毕不要忘记在各个包内创建临时文件,否则上库后包会不显示。