学习资料地址 王爵的技术小黑屋 https://www.youtube.com/watch?v=29q6zwRGywk
01.什么是Git
Git是一个分布式的版本管理系统
每次修改后必须commit
本地数据库
![图片名称](https://img2018.cnblogs.com/blog/1520631/201812/1520631-20181211221354980-478628324.png)
暂存区
- 查看隐藏文件:ll -la
- cd .git
- ll
- 提交文件到服务器:git commit -m "add readme.md"
提交到远程github上
git remote add origin https://github.com/JcOpenSource/git.git
git push -u origin master
*与远端的服务器先建立连接: git remote add origin https://github.com/JcOpenSource/git.git
origin 远端服务器的名字
-
查看远程连接:git remote -v
-
将本地的文件提交到服务器上:git push -u origin master
-u 以后直接输入 git push 不用加远端的连接和分支名字 -
ssh免密校验【没有讲解】
-
将远程代码克隆到本地:git clone https://github.com/JcOpenSource/git.git git_demo
后面加新的文件名称
-
将远程的代码拉到本地:git pull
git pull origin(远端的链接) master(分之的名字)
03.Git 中的分支
branch 分支
HRAD 当前在使用的分支中的最后一次更新
-
创建分支:git branch feature1
-
查看分支:git branch
按q退出 -
跳转分支名:git checkout 'feature1'
-
可以在feature1下开分支feature2,feature2有feature1添加的文件
-
git checkout -b feature3 等价于 git branch feature3 以及 git checkout feature2
-
删除分支:git branch -d feature2
在命令行中大写表示非常慎重的操作
feature3 提交了内容但是没有合并到master 主分支上所以删除分支feature3 会出现错误
-
将分支feature3里面的东西合并到分支master上
Fast-forward 模式
将master 的head 指向 feature3的head 做一个相互合并 -
冲突解决【没有讲】
远程的分支只有一个
-
将分支提交到远程
注意origin 是远端的连接 -
在本地删除远端的分支 删除:
-
创建远程分支并且重新命名成f1:git push origin feature1:f1
04 Git 中的合并
- 查看日志: git log
git log --oneline
git log --oneline -3
日志地址:https://stackoverflow.com/questions/1057564/pretty-git-branch-graphs - 配置文件:git log --all --decorate --oneline --graph
跳转到master分支下查看
但是我们不知道fb是哪个分支提交的
默认保存
--no-ff
查看新的分支结构提交结果
把master 和 f1 分支提交到远程
在远程修改后f1必须merge master 才能与远程同步
在master 做修改
将f1移动到master最后一次提交,然后把master 的所有提交并入过来: git rebase master
绝对不要在公共的分支上使用 git rebase
解决冲突:
使用冲突解决工具
05 Git中的回滚撤销
回到当前版本:git reset master^
^代表上一次、前一次
回退两次可以加两个这个符号^
一次类推
git reset master~5 往后倒退5步
绝对撤回
git reset 回到某一个版本
06 gitignore 和 fork 同步
代码中的.setting .ide 等配置文件如何忽略
- 创建.gitignore 文件
- 编写文件:
【1】忽略系统生成的文件,如IDE的配置
【2】忽略编译生成的中间文件、可执行文件等
【3】忽略敏感的配置文件和本地不想提交的脚本
*.sh
.settings/
!*.txt
/a/*.class
!*.txt 所有的txt文件不被忽略
自动生成忽略文件:https://www.gitignore.io/
lijuncheng@lijunchengdeMBP ~/workspace/git (master*) $ git add -A .
lijuncheng@lijunchengdeMBP ~/workspace/git (master*) $ git commit -m "add gitignore"
[master 2c89780] add gitignore
4 files changed, 17 insertions(+), 6 deletions(-)
create mode 100644 .gitignore
rename m1.txt => Hello.java (100%)
create mode 100644 a.txt.orig
lijuncheng@lijunchengdeMBP ~/workspace/git (master) $ git push
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 4 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (15/15), 1.50 KiB | 1.50 MiB/s, done.
Total 15 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8), completed with 2 local objects.
To https://github.com/JcOpenSource/git.git
c7e0996..2c89780 master -> master
lijuncheng@lijunchengdeMBP ~/workspace/git (master) $
- git fetch 将上游仓库拉下来
07 实现 Github 免密推送
ssh免密提交
gitn 中有4个协议:
- 本地协议
- HTTP协议
- ssh协议
*git协议
生成ssh公钥:
https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
密码不用输入为了方便
- 后台开启ssh代理:eval "$(ssh-agent -s)"
自己下载上传项目失败!!!
08 Git 中的工作流
https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md#23-gitflow
gitflow 大型项目
09 Git 常用图形化工具
三大工具:
- sourcetree
- vscode
- ide
视频中的 git emoji 插件地址:https://plugins.jetbrains.com/plugin/...
- Git Community Book 中文版:http://gitbook.liuhui998.com/
- Git Tutorial:http://www.vogella.com/tutorials/Git/...
- 猴子都能懂的 Git 指南:https://backlog.com/git-tutorial/cn/
- Git 简明指南:http://rogerdudler.github.io/git-guid...
自己提交的git 文档过 Cher 呢
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git init
Reinitialized existing Git repository in /Users/lijuncheng/Desktop/python-data-visualization/.git/
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git add -A
移除多余的 remote
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git remote add origin https://github.com/JcOpenSource/python-data-visualization.git
fatal: remote origin already exists.
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git remote -v
origin https://github.com/JcOpenSource/python-.git (fetch)
origin https://github.com/JcOpenSource/python-.git (push)
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git remote remove origin
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git remote add origin https://github.com/JcOpenSource/python-data-visualization.git
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git remote -v
origin https://github.com/JcOpenSource/python-data-visualization.git (fetch)
origin https://github.com/JcOpenSource/python-data-visualization.git (push)
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master*) $ git commit -m "add python file"
[master (root-commit) 7c13cce] add python file
11 files changed, 250 insertions(+)
create mode 100644 1.py
create mode 100644 10.py
create mode 100644 2.py
create mode 100644 3.py
create mode 100644 4.py
create mode 100644 5.py
create mode 100644 6.py
create mode 100644 7.py
create mode 100644 8.py
create mode 100644 9.py
create mode 100644 README.md
lijuncheng@lijunchengdeMBP ~/Desktop/python-data-visualization (master) $ git push -u origin master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 4.36 KiB | 4.36 MiB/s, done.
Total 13 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/JcOpenSource/python-data-visualization.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
- 初始化 git init
- 添加本地 git add
- 提交本地管理 git commit -m "
- 获得链接 git remote add origin https://github.com/JcOpenSource/p
- 提交云端 git push -u origin master