
宠辱不惊,闲看庭前花开花落;
去留无意,漫观天外云卷云舒。
听雨
当口袋里有了金币,代码库里全是代码,我想去听雨,一个人去听雨。
一、创建远程仓库,也就是在git上创建仓库
首页登陆Github,并选择创建



复制这段代码:git remote add origin https://github.com/chriseleee/text006.git
后面会用到
这里注意用https,不用ssh
如果用ssh可能会报错

二、终端操作--第一次上传代码
第一步 初始化git
打开Mac电脑的终端,进入到要上传的文件夹,cd 到“目录”
cd
空格一次后 将文件夹拽入终端生成路径,enter一下进入
初始化git仓库 再输入命令
git init



注意如果没有.git文件,说明被隐藏了
显示隐藏:
第一步:打开「终端」应用程序。
第二步:输入如下命令:
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder
第三步:按下「Return」键确认。
第二步 关联本地与git库

1. 提交到暂缓区
git add .
2. 提交
git commit -m '第一次提交代码'
-m ' ' 引号内面填写详细描述
如果第一次使用,会提示输入github的账号密码,输入即可
3.关联远程github库
复制之前创建github仓库时复制的代码
git remote add origin https://github.com/chriseleee/text006.git
4.最精彩的一步:上传到github
git push -u origin master
上传会有一段时间

以上就算上传成功了
5.(优化)添加releases版本号

发现没有releases版本号
5.1本地创建tag
git tag -a '0.0.1' -m '第一个版本的tag'
5.2查看本地tag
git tag

5.3添加到远程仓库(只提交tags)
git push --tags



三、终端操作--第二次及以后代码更新上传
1 提交到暂缓区
先看状态
git status


提交到暂缓区
git add .
2 提交
git commit -m '第二次提交代码'

3.上传代码到github
git push -u origin master
4 添加tag号
git tag '0.0.2'

5 push 指定tag号
git push origin 0.0.2

四、终端操作--删除一个版本
1 本地删除
这里举例删除0.0.1的版本
git tag -d 0.0.1
2 远程删除
git push origin :0.0.1

五、忽略指定文件
cd desktop ///切换到桌面
touch .gitignore ///创建.gitignore
vim .gitignore ///进入编辑.gitignore

退出编辑模式的命令是:
先按esc退出编辑模式
然后输入 :wq 冒号也要输入进去,然后回车退出编辑。
这样你就生成了一个.gitignore文件,这个文件放在和.git这个隐藏文件夹同级的位置
1 下面是一些gitignore文件的写法分享:
(1)所有空行或者以注释符号 # 开头的行都会被 Git 忽略。
(2)可以使用标准的 glob 模式匹配。
(3)匹配模式最后跟反斜杠(/)说明要忽略的是目录。
(4)要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。星号(*)匹配零个或多个任意字符;[abc] 匹配任何一个列在方括号中的字符(这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c);问号(?)只匹配一个任意字符;如果在方括号中使用短划线分隔两个字符,表示所有在这两个字符范围内的都可以匹配(比如 [0-9] 表示匹配所有 0 到 9 的数字)。
# 此为注释 – 将被 Git 忽略
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
2 下面有些人会遇到加上这个文件,发现忽略的文件还是有上传。
原因:
在git库中已存在了这个文件,之前push提交过该文件。
.gitignore文件只对还没有加入版本管理的文件起作用,如果之前已经用git把这些文件纳入了版本库,就不起作用了
解决:
需要在git库中删除该文件,并更新。
然后再次git status查看状态,文件不再显示状态。
六、远程代码更新后同步到本地
//方法一
$ git fetch origin master //从远程的origin仓库的master分支下载代码到本地的origin master
$ git log -p master.. origin/master//比较本地的仓库和远程参考的区别
$ git merge origin/master//把远程下载下来的代码合并到本地仓库,远程的和本地的合并
//方法二
$ git fetch origin master:temp //从远程的origin仓库的master分支下载到本地并新建一个分支temp
$ git diff temp//比较master分支和temp分支的不同
$ git merge temp//合并temp分支到master分支
$ git branch -d temp//删除temp
七、更多
1.远程删除代码库



2.查看版本信息
git log

3.git的基本使用指令汇总
git init #初始化git仓库
git add . #添加当前文件夹下的所有文件
git status #显示状态
git commit #提交代码
git commit -m ‘注释’ #提交代码加注释
git log # 看提交记录
git push # 推送
git push origin master #推送到远程master分支
git push origin ‘版本号’ #按照版本号推送到远程
git remote add origin <URL> #关联远程仓库
git tag -a ’版本’ -m ‘描述’ #打标签
git push - -tags #提交到远程
写在最后:
希望这篇文章对您有帮助。当然如果您发现有可以优化的地方,希望您能慷慨的提出来。最后祝您工作愉快!
网友评论