git && github
git、github、gitlab
- 学习日期:2020年7月17日 - 7月19日
- 学习内容:git操作、github注册及使用、gitlab搭建
- 注明:本文仅为学习笔记,如有错误请多指教。
git本地库初始化
- 初始化git仓库命令 :
git init
- git代码片:
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat
$ git init
Initialized empty Git repository in C:/Users/xujiajun/Desktop/Wechat/.git/
本地库设置签名
- 形式:
- 用户名 以tom为例
- 邮箱 以goofMorning@163.com为例
- 作用:区分不同开发人员的身份
- 辨析:设置签名和登录远程库的账号密码没有任何关系
- 设置签名命令
- 项目/仓库级别:当前本地库范围内有效
git config user.name tom_pro
git config user.email goodMorning@163.com
- 系统级别:登陆当前操作系统的用户范围
git config --global user.name tom_pro
git config --globa user.email goodMorning@163.com
- 级别优先级:
- 就近原则:项目级别优先于系统级别。
- 只有系统级别,就以系统用户级别的签名为准
- 必须要有一个级别的签名
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git config user.name tom_pro
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git config user.email goodMorning@163.com
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = tom_pro
email = goodMorning@163.com
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git config --global user.name tom_glb
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git config --global user.email goofMorning_glb@163.com
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ cd ~
xujiajun@XUJIAJUN MINGW64 ~
$ pwd
/c/Users/xujiajun
xujiajun@XUJIAJUN MINGW64 ~
$ cat .gitconfig
[user]
name = tom_glb
eamil = goodMorning_glb@atguigu.com
email = goofMorning_glb@163.com
添加提交&&查看状态
- 查看状态
git status
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
- 创建一个 good.txt 文件
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ vim good.txt
- 添加到暂存区:
git add good.txt
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
good.txt
nothing added to commit but untracked files present (use "git add" to track
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
- 查看状态
git status
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: good.txt
- 撤回暂存区文件:
git rm -- cached good.txt
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git rm --cached good.txt
rm 'good.txt'
- 提交文件
git commit good.txt
# my first commit file good.txt
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i or -o; assuming --only paths...
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: good.txt
#
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git commit good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
[master (root-commit) 8691de9] my first commit new file goog.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
1 file changed, 3 insertions(+)
create mode 100644 good.txt
- 提交文件时直接加上描述 :
git commit -m "my second commit,modify good.txt" good.txt
查看历史记录
- 查看日志命令:
git log
- 翻页显示命令:
git log | less
- space:向下翻页
- b:向上翻页
- q:退出
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git log
commit 28813419fad9bf11a02518740c9d3a20dc8e5737
Author: tom_pro <goodMorning@163.com>
Date: Fri Jul 17 15:52:39 2020 +0800
my second commit file modify
commit 8691de9fa38659852aae83583b1c2e8d2a9272e0
Author: tom_pro <goodMorning@163.com>
Date: Fri Jul 17 15:41:17 2020 +0800
my first commit new file goog.txt
git log --pretty=oneline
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git log --pretty=oneline
d281a5791b25b52bbfc32ffdbd2f8b918e5d9de8 asdasd
28813419fad9bf11a02518740c9d3a20dc8e5737 my second commit file modify
8691de9fa38659852aae83583b1c2e8d2a9272e0 my first commit new file goog.txt
git log --oneline
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git log --oneline
d281a57 asdasd
2881341 my second commit file modify
8691de9 my first commit new file goog.txt
git reflog
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git reflog
d281a57 HEAD@{0}: commit: asdasd
2881341 HEAD@{1}: commit: my second commit file modify
8691de9 HEAD@{2}: commit (initial): my first commit new file goog.txt
版本前进&&后退
- 本质:操作
{head}
- 推荐基于索引值操作:
git reset --hard [索引值]
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git reset --hard 8691de9
HEAD is now at 8691de9 my first commit new file goog.txt
xujiajun@XUJIAJUN MINGW64 ~/Desktop/Wechat (master)
$ git reflog
8691de9 HEAD@{0}: reset: moving to 8691de9
d281a57 HEAD@{1}: commit: asdasd
2881341 HEAD@{2}: commit: my second commit file modify
8691de9 HEAD@{3}: commit (initial): my first commit new file goog.txt
- 使用
^
符号:- 只能后退,不能后退
git reset --hard HEAD^
- 一个
^
代表后退一步
- 只能后退,不能后退
- 使用
~
符号- 只能后退,可以直接推多步
git reset --hard HEAD~n
reset的三个参数
- soft参数
- 仅仅在本地库移动
HEAD
指针
- 仅仅在本地库移动
- mixer参数
- 在本地库移动
HEAD
指针 - 重置暂存区
- 在本地库移动
- hard参数
- 在本地库移动
HEAD
指针 - 重置暂存区
- 重置工作区
- 在本地库移动
删除文件并找回
- 前提:删除前,文件存在时的状态提交到了本地库
- 操作:
git reset --hard [指针位置]
- 删除操作已经提交到本地库:指针位置指向文件存在的历史记录。
- 删除操作未提交到本地库:指针位置使用
HEAD
比较文件差异
命令:git diff [文件]
与本地库文件比较:git diff HEAD [文件]
分支管理
好处:同时并行推进多个功能开发,提高开发效率
各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响,失败的分支删除重新开始即可
命令:
查看分支:git branch -v
创建分支:git branch [分支名]
切换分支:git checkout [分支名]
合并分支:git merge [有新内容的分支名]
(先切换需要接受修改的分支上)
解决冲突:手动合并,修改文件至满意,然后add,commit(带日志信息,不需要带文件名称)
哈希原理(略)
github注册、创建仓库(略)
push推送至远程库
- 复制http链接
- 终端准备初始化新的本地库
- 输入
git remote add origin https://github.com/18626428291/huashan.git
git remote -v
查看
[xujiajun@mac] ~/Desktop/huashan$ git remote -v
origin https://github.com/18626428291/huashan.git (fetch) #取回
origin https://github.com/18626428291/huashan.git (push) #推送
- 推送
git push [别名] [分支]
举例:git push orgin master
clone远程库
克隆命令:git clone [http地址]
克隆的效果:
- 完整把远程库下载到本地
- 创建远程地址别名
- 初始化本地库
github邀请加入团队
仓库内
setting > collaborators > 被邀请人账号(github)
pull拉取操作和fetch抓取操作(读)
pull = fetch + marge
git fetch orgin master
把远程内容下载
git marge [远程库别名]/[远程分支名]
解决冲突
要点:如果不是基于远程库的最新版做出的修改,不能推送,必须先拉取
拉取下来之后如果存在冲突,咋按照“分支冲突解决”即可
跨团队合作
Fork > pull requests >