【Mac系统 + Git】之上传项目代码到github上以及删除某个文件夹
之前做开发的时候,用过一段时间git代码管理工具,用命令行操作感觉十分高大上,今天我想从头总结一篇Mac系统下如何利用git上传代码到github上的学习。
目录
- 一、安装Git
- 二、创建.ssh文件
- 三、Github账号中添加Key
- 四、创建版本库Repository
- 五、上传更新新的代码到github上
- 六、删除github中某个文件夹
- 附录:github常见错误及扩展
一、安装Git
参考文章:《Mac下使用Git上传代码到Github仓库》
下载地址:https://git-scm.com/download/mac
下载后为.dmg文件,解压后双击安装.pkg文件

输入指令:
test:~ zhan$ git --version
git version 2.18.0
二、创建.ssh文件 返回目录
打开终端,输入下面指令,查看.ssh是否存在
test:~ zhan$ cd ~/.ssh
test:.ssh zhan$
test:~ zhan$ cd .ssh/
test:.ssh zhan$ ls
known_hosts
查看文件夹下的文件,只有known_hosts,感觉少了点什么
如果没有.ssh文件夹,请参考《Mac如何添加生成ssh》 、《Mac生成添加ssh公钥》
假设你在Github注册账号为: xxxx@xxx.com
Terminal中运行
//默认直接按 回车 就可以了
test:.ssh zhan$ ssh-keygen -t rsa -C xxx@xxx.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/zhan/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/zhan/.ssh/id_rsa.
Your public key has been saved in /Users/zhan/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xCNUrcwiwpMVb2Se4zucMD0V9+SX+WeZ4/TdAdX1Spk xxx@xxx.com
The key's randomart image is:
+---[RSA 2048]----+
| ..+.o.. . .+|
| .* o o.+ .=o|
| . o Bo=. o.E .|
| = .+.=+. o.oo|
| oo.+.S .*+|
| + + o.B|
| = .+|
| . |
| |
+----[SHA256]-----+
输入命令:ls,查看.ssh下的文件
id_rsa id_rsa.pub known_hosts
多出两个文件,.pub是公钥,另一个是密钥。
三、Github账号中添加Key 返回目录
点击【头像】 ->Settings ->SSH and GPG keys ->

点击【New SSH key】按钮

输入Title、Key

执行下面的命令行,直接复制文件里的内容:
pbcopy < ~/.ssh/id_rsa.pub
或查看:
cat ~/.ssh/id_rsa.pub
复制到里面后,点击【Add SSH key】按钮。
下面继续确认登录github的密码:

如果添加Key成功的话,如下图所示,同时你也会在邮箱里收到一个提醒邮件,内容是你添加了一个Key.


四、创建版本库Repository 返回目录
首先,返回到主页,www.github.com

进入到了 “Create a New Repository”页面:

紧接着按照以下步骤进行本地仓库的创建及代码上传。打开终端,输入以下命令:
$ echo "TestRepository" >> README.md //新建一个README文档并添加内容,若上一步勾选了创建README.md,提交时导致冲突
$ git init //初始化本地仓库
$ git add README.md //添加刚刚创建的README文档
$ git commit -m "你的注释...." //提交到本地仓库,并写一些注释
$ git remote add origin git@github.com:yourname/xxxx.git
//连接远程仓库并建了一个名叫:origin的别名,当然可以为其他名字,但是origin一看就知道是别名,youname记得替换成你的用户名
$ git push -u origin master
//将本地仓库的文件提交到别名为origin的地址的master分支下,-u为第一次提交,需要创建master分支,下次就不需要了
或者:
//创建 README.md 文件, 并向里面写入`This Is My First Testing Description....`字符串。 echo "# This Is My First Testing Description...." >> README.md
git init
git add README.md
git commit -m "first commit" //commit备注
git remote add origin https://github.com/imthinktwice/TestRepository.git
git push -u origin master
但是执行git commit -m "first commit"报错:
test:Git zhan$ git commit -m "first commit"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'zhan@test.(none)')
参考:《git fatal: unable to auto-detect email address》
解决办法,输入指令:
git config --global user.email "you@example.com" #查看本地配置
git config --local -l
#编辑config文件
git config --local -e 如何在终端编辑文件,参照:《Mac Git 配置全局gitconfig》
再执行下面:
#再执行命令
git commit -m "first commit"
#显示结果
test:Git zhan$ git commit -m "first commit"
[master (root-commit) 4d3f7a6] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
commit成功!!
再继续执行命令:
git remote add origin git@github.com:yourname/xxxx.git git push -u origin master
=====================================================
#result:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 221 bytes | 221.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/Owen-ET/TestRepository/pull/new/master
remote:
To github.com:Owen-ET/TestRepository.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
github中创建成功!!!!
再返回github网址查看:

看看,上传github上成功!!
五、上传更新新的代码到github上 返回目录
首先在之前上传的项目中,新建一个子项目,如图

输入命令:git status
查看项目下文件状态,如下:
On branch master
Your branch is up to date with 'origin/master'. Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store
python_stu/ nothing added to commit but untracked files present (use "git add" to track)
其中“python_stu/”文件夹是我新建的,上传到github需要add添加
所以执行命令:
#添加文件夹
git add python_stu/ #提交文件夹,并注释
git commit -m "上传py文件2018-09-19" #继续查看状态,python_stu/文件夹已添加
git status On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits) Untracked files:
(use "git add <file>..." to include in what will be committed) .DS_Store nothing added to commit but untracked files present (use "git add" to track)
继续执行
test:Git zhan$ git pull #同步代码 Already up to date. test:Git zhan$ git push origin #把代码推到服务器上 Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 866 bytes | 866.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0) To github.com:Owen-ET/TestRepository.git 4d3f7a6..9110914 master -> master
上面可参考文章:《mac下如何把项目提交、更新到gitHub上》
返回到github上,如图:

六、删除github中某个文件夹 返回目录
只需要一下几步就可以完成删除
# 删除文件夹
git rm -r --cached python_stu/ # 提交,添加操作说明
git commit -m '删除stu文件夹' # 将本次更改更新到github项目上去
git push -u origin master
参考文章:《删除github中某个文件夹》
汇总:
GitHub上传项目
126,(66) 先cd到git文件夹下,把新建的项目复制到git下
再查看状态:git status #添加文件夹
git add python_stu/ #提交文件夹,并注释
git commit -m "上传py文件2018-09-19" #继续查看状态,python_stu/文件夹已添加
git status test:Git zhan$ git pull #同步代码 test:Git zhan$ git push origin #把代码推到服务器上 ==================================== 删除github上的文件 # 删除target文件夹
git rm -r --cached python_stu/ # 提交,添加操作说明
git commit -m '删除stu文件夹' # 将本次更改更新到github项目上去
git push -u origin master
七、附录: 返回目录
================扩展:==================================
.git目录看不到怎么办,参考:《Mac上如果看不到.git目录的解决方法》
.git路径为就是自己初始化init创建git时的路径!
=======================================================
【Mac系统 + Git】之上传项目代码到github上以及删除某个文件夹的更多相关文章
- 使用Git上传项目代码到github
github是一个基于Git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说公共仓库就已经足够了. 注册账户以及创建仓库 要想使用gi ...
- git上传项目代码到github
参考: git学习——上传项目代码到github github上传时出现error: src refspec master does not match any解决办法 git 上传本地文件到gith ...
- 如何使用Git上传项目代码到github
这是我第一次应用git,以下仅供git的初学者参考. github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说 ...
- TortoiseGit上传项目代码到github方法(超简单)
Github是咱广大开发者用的非常多的项目代码版本管理网站,项目托管可以是私人的(private)或者公开的(public),私人的收费,一个月7美金.咱这里就只说我们个人使用的,一般都是代码对外开放 ...
- Mac下,如何把项目托管到Github上(Github Desktop的使用)
在上一篇中,详细讲解了使用X-code和终端配合上传代码的方法,这种方法比较传统,中间会有坑,英文看起来也费劲,不过Github官方提供了一个Mac版的客户端,如下图:
- 解决使用maven clean项目的时候报错,删除target文件夹失败
背景:jdk1.8 + maven3.5.2 问题描述: 我在使用maven clean项目的时候,celan 失败,报错的原因是删除项目下的target文件夹下面的文件失败 解决方法: 打开任务管理 ...
- git将本地项目添加到github上
git init git add . git commit -m '添加备注' git add remote origin originUrl git push -u origin master 注意 ...
- Mac系统Git生成ssh公钥
Mac系统Git生成ssh公钥 在使用Git仓库进行代码管理时,新的电脑上往往需要生成ssh公钥进行匹配,Mac系统生成Git公钥过程如下: 1.检查本机是否已有公钥 在终端中输入如下命令: ? 1 ...
- 在Eclipse上使用egit插件通过ssh协议方式上传项目代码的具体步骤
在Eclipse上使用egit插件通过ssh协议方式上传项目代码 前戏: 使用ssh方式可以不通过https协议,避免直接提供账号密码的方式上传项目到git在线服务器,如Bitbucket.GitHu ...
随机推荐
- db2字符串截取方法及常用函数
select substr(index_code, 1, locate('-', index_code)-1) from report_data substr(str,m,n)表示从str中的m个字符 ...
- shell 调用 sqlplus
一.最简单的shell里调用sqlplus. $ vi test1.sh #!/bin/bashsqlplus -S /nolog > result.log <<EOFset hea ...
- Linux系统入门命令100条 转
https://www.howtoforge.com/linux-commands/ 2017-04-27 RiboseYim 睿哥杂货铺 Author : Himanshu Arora 原文:htt ...
- linux的file指令
显示文件的类型,用命令 file 可以使你知道某个文件究竟是ELF格式的可执行文件, 还是shell script文 件或是其他的什么格式 例如:#file startx 语 法:file [-beL ...
- XCTest(二)
New tool sets are making it easier and easier to engage in genuine agile development on iOS. In part ...
- JAVA之接口与实现
/** * * 功能:接口与实现 * 接口也体现了多态性 */package com.test; public class test5 { /** * @param args */ ...
- 用word2vec对语料进行训练
在Linux上安装好word2vec, 进入trunk文件夹,把分词后的语料文件放在trunk文件夹内,执行:./word2vec -train tt.txt -output vectors.bin ...
- sass的高级语法
1. 变量 sass允许使用变量,所有变量以$开头 2.引用父元素 & 这里 "&" 就代表是 a 3.继承 这样 class2 就 拥有了class1的所有属性 ...
- ITIL,是否已是昨日黄花
首先声明自己不是ITIL方面的专家,特别是具体的规范细节,后面论述如有不当,请指正.但我为什么会提起它?主要是因为它和运维(IT服务管理)相关性太大了.早起的运维完全就是以ITIL来蓝本构建的,在当时 ...
- POJ 1144 Network(无向图连通分量求割点)
题目地址:id=1144">POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u ...