教程来自 廖雪峰-Git教程,因为我有不太懂的点,并且为了方便自己查找信息,所以整理了学习笔记。教程请去原博看,本文仅做个人学习用途..侵删,谢谢…
0. 小结
- 常用指令
- 修改文件: add, commit
- 换版本, git log/reflog, git reset
管理修改
4.1 撤销修改
4.2 删除文件添加远程库
5.1 创建远程仓库
5.2 添加远程库
5.3 从远程库克隆
1. 常用指令跟linux差不多
#创建文件夹
$ mkdir learngit
#转到某个路径
$ cd learngit
#查看当前路径
$ pwd
/Users/michael/learngit
#初始化仓库
$ git init
#添加文件,提交文件
$ git add <file>
$ git commit
#查看仓库目前状况(有没有什么文件修改了)
$ git status
#查看具体某个文件的修改前后内容
$ git diff readme.txt
2. 修改文件
因为需要先add,然后再commit。
#重新提交修改,需要先add file,然后在commit。
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add readme.txt
$ git commit -m "add jasmine"
[master 5d916c4] add jasmine
1 file changed, 1 insertion(+), 1 deletion(-)
$ git status
On branch master
nothing to commit, working tree clean
3. 换版本
#查看版本,里面的信息就是每次commit前加的字符串备注。[commit -m "字符串"]
# git log 查看提交历史,git reflog 查看修改历史(CLI历史)
$ git log
commit a60d7720d837a87661bacdae4ac18e1eec1f0bf2 (HEAD -> master)
Author: zhanglimin <zhanglimin2016@foxmail.com>
Date: Fri Mar 23 15:36:44 2018 +0800
appened GLPPP
commit 5d916c451e4d30d92295a7acd3c322b41a115e13
Author: zhanglimin <zhanglimin2016@foxmail.com>
Date: Fri Mar 23 15:21:17 2018 +0800
add jasmine
commit b4532ded4ca467892400a19b1f6df687eea1ddad
Author: zhanglimin <zhanglimin2016@foxmail.com>
Date: Fri Mar 23 14:51:46 2018 +0800
wrote a readme file
$ git reflog
b4532de (HEAD -> master) HEAD@{0}: reset: moving to b4532ded4ca467892400a19b1f6df687eea1ddad
5d916c4 HEAD@{1}: reset: moving to HEAD^
a60d772 HEAD@{2}: commit: appened GLPPP
5d916c4 HEAD@{3}: commit: add jasmine
b4532de (HEAD -> master) HEAD@{4}: commit (initial): wrote a readme file
#版本表示
当前版本 HEAD
前一个版本 HEAD^
前前一个版本 HEAD^^
#回到前一个版本
$ git reset --hard HEAD^
#回到某个版本,加上那个版本号
$ git reset --hard <commit hash key>
4. 管理修改
4.1 撤销修改
- 撤销工作区本次修改,回到上一次commit/add的状态
git checkout -- file
- 丢弃暂存区的修改
git reset HEAD file
git checkout -- readme.txt
4.2 删除文件
- 删除在工作区的文件(未add/commit)
rm <file>
- 删除在暂存区的文件(已add,未commit)
git checkout -- test.txt
git rm
5. 远程仓库
5.1 创建远程仓库
- 检查git有无创建ssh key,一路回车就是创建了
$ ssh-keygen -t rsa -C "youremail@example.com"
- 转到以上显示的
.ssh/
目录,需要有这两个文件id_rsa
和id_rsa.pub
- 打开github,个人这是页面–>settings–>SSH and GPG keys–>SSH keys–>new ssh keys
- title 写一下,先在git中的
.ssh/
目录下把key拿出来cat id_rsa.pub
;然后把内容放到SSH key里面。
5.2 添加远程库
1.在github上创建一个新的“Create repository
,在SSH页面会有命令行提示怎么操作。
$ git remote add origin <github name>
## 5.3 第一次推送master分支的所有内容
$ git push -u origin master
#5.4 推送修改
$ git push origin master
2.敲下指令后会弹出界面,输入账号密码,成功时会显示如下信息。
$ git push -u origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 871 bytes | 435.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/LeeMin-Z/learngit.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
3gg在github的页面上也有相关文件和信息
5.3 从远程库克隆
假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆。
- 创建一个新仓库,取名
gitskills
,勾选Initialize this repository with a README
- 理论上是世界复制一个远程库来本地
$ git clone git@github.com:username/gitskills.git
- 实际上可能会报错,要根据提示错误做点调整…
$ git clone git@github.com:LeeMin-Z/gitskills.git
Cloning into 'gitskills'...
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is xxxxxxxxx
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,xxxx' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
参考资料
1. 廖雪峰-Git教程
2. 网上零碎教程