文章目录
使用
学习资料: https://git-scm.com/book/zh/v2
初始化git
查看配置
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=D:/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge --skip -- %f
filter.lfs.process=git-lfs filter-process --skip
filter.lfs.required=true
credential.helper=manager
user.name=uesrname
user.email=57611732+username@users.noreply.github.com
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
修改配置
Administrator@PC-20190909FNCP MINGW64 /e
$ git config --global user.name 'userName'
Administrator@PC-20190909FNCP MINGW64 /e
$ git config --global user.email 'user.email'
核心概念
工作区:
工作目录
暂存区:
英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)
版本库:
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
基本操作
创建版本库
git init
# 在需要存放项目的文件夹路径下执行,进行初始化
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka
$ git init
Initialized empty Git repository in E:/IDEASpace/kafka/.git/
add
# 将工作去的文件添加到版本库的暂存区
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
$ git add README.md
git status
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 展示git的工作状态
$ git status
On branch master
No commits yet
# 已经添加到暂存区的文件
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .idea/vcs.xml
new file: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
.idea/compiler.xml
.idea/encodings.xml
.idea/misc.xml
.idea/workspace.xml
kafka.iml
pom.xml
commit
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 将暂存区的文件提交到版本库 -m 本次提交的信息
$ git commit -m 'add README.md'
[master (root-commit) 7dfe32f] add README.md
2 files changed, 6 insertions(+)
create mode 100644 .idea/vcs.xml
create mode 100644 README.md
checkout
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
$ git checkout
D README.md
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 进行撤销操作,将误删的文件进行恢复
$ git checkout -- README.md
diff
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 工作区与暂存区文件的区的区别
$ git diff README.md
diff --git a/README.md b/README.md
index c6556d1..8326e6d 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
# 减少的代码
-修改到暂存区
# 增加的代码
+修改到工作区
reset
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 历史版本展示
$ git reflog
ef4735e (HEAD -> master) HEAD@{0}: commit: delete README1.md
7128b6f HEAD@{1}: commit: delete test
b7721ec HEAD@{2}: commit: README modification
6a5abd3 HEAD@{3}: commit: README modification for the first time
7dfe32f HEAD@{4}: commit (initial): add README.md
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 回退到某一版本
$ git reset --hard 7128b6f
HEAD is now at 7128b6f delete test
git远程仓库github
- 注册&登录
- 配置SSH免密登录
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
# 生成密钥
$ ssh-keygen -t rsa -C "123@163.com" # github注册时使用的邮箱
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ThinkPad/.ssh/id_rsa):
# 这里我没有选择覆盖
/c/Users/ThinkPad/.ssh/id_rsa already exists.
Overwrite (y/n)?
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
$ cd ~
ThinkPad@LAPTOP-M7HNV6VG MINGW64 ~
$ cd .ssh/
ThinkPad@LAPTOP-M7HNV6VG MINGW64 ~/.ssh
$ ll
total 5
-rw-r--r-- 1 ThinkPad 197121 2610 3月 21 20:00 id_rsa
-rw-r--r-- 1 ThinkPad 197121 573 3月 21 20:00 id_rsa.pub
ThinkPad@LAPTOP-M7HNV6VG MINGW64 ~/.ssh
# 将该文件中的内容复制到GitHub中的配置中
$ vim id_rsa.pub
# 测试连接是否可用
ThinkPad@LAPTOP-M7HNV6VG MINGW64 ~
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '[18.138.202.180]:443' to the list of known hosts.
Hi YueCang! You've successfully authenticated, but GitHub does not provide shell access.
- 创建git远程仓库
创建远程仓库
两种连接方式:由于HTTPS比较慢,所以推荐使用SSH
项目中首先初始化本地git,然后运行 git remote add origin 关联远程仓库:
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka
$ git init
Initialized empty Git repository in E:/IDEASpace/kafka/.git/
ThinkPad@LAPTOP-M7HNV6VG MINGW64 /e/IDEASpace/kafka (master)
$ git remote add origin git@github.com:/kafka-cases.git
常用命令
$ git init //初始化项目
$ touch README
$ git add README //更新README文件
$ git commit -m 'commit text' //提交更新,并注释信息“committext”
$ git remote add origin https://github.com/test.git //连接远程github项目
$ git push -u origin master //将本地项目更新到github项目上去
$ git pull origin master //拉取远程项目代码