0. 命令速查


1. 创建版本库
# 进入一个准备用作版本库的文件夹内Ahang@Ahang MINGW64 ~$ cd Documents/Git/# 初始化创建该库Ahang@Ahang MINGW64 ~/Documents/Git$ git initInitialized empty Git repository in C:/Users/Ahang/Documents/Git/.git/
// 复制一个已创建的仓库:git clone ssh://user@domain.com/repo.git
2. 将文件添加到版本库中
- 先在版本库的文件夹里面添加文件如:README,可以写入些内容
- 再通过
git add README添加至库中 - 最后通过
git commit -m "wrote a README file"后面的" "内容可以根据实际情况填写
# 可以通过dir, ls 查看当前目录下文件Ahang@Ahang MINGW64 ~/Documents/Git (master)$ dirREADMEAhang@Ahang MINGW64 ~/Documents/Git (master)$ lsREADME# 添加到版本库Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git add README# 提交到库内,并加以注解,方便自己和他人查看Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git commit -m "wrote a README file"[master (root-commit) ae27363] wrote a README file1 file changed, 2 insertions(+)create mode 100644 README
对于同时添加多个文件时
# 创建3个文件Ahang@Ahang MINGW64 ~/Documents/Git (master)$ touch aAhang@Ahang MINGW64 ~/Documents/Git (master)$ touch cAhang@Ahang MINGW64 ~/Documents/Git (master)$ touch b# 报错示例:直接提交还没有添加时Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git commit -m "add 3 files"On branch masterUntracked files:(use "git add <file>..." to include in what will be committed)acbnothing added to commit but untracked files present (use "git add" to track)# 先添加,可以同时添加多个,空格隔开即可Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git add a b c# 再提交,会将目前添加还没有提交的全部提交Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git commit -m "add 3 files"[master 7602874] add 3 files3 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 acreate mode 100644 bcreate mode 100644 c
3. 对仓库文件修改并保存
- 查看仓库状态:
git status - 查看修改后文件的区别:
git diff README - 然后对文件保存通过
git add README和git commit -m <detail>
# 查看状态Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git statusOn branch masternothing to commit, working tree cleanAhang@Ahang MINGW64 ~/Documents/Git (master)$ git statusOn branch masterChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: READMEno changes added to commit (use "git add" and/or "git commit -a")# 查看区别Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git diff READMEdiff --git a/README b/READMEindex 75977d0..682c059 100644--- a/README+++ b/README@@ -1,2 +1,4 @@Hello world!!!-This is my frist example!\ No newline at end of file+This is my second examples!++add some word\ No newline at end of file# 文件保存Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git add READMEAhang@Ahang MINGW64 ~/Documents/Git (master)$ git statusOn branch masterChanges to be committed:(use "git restore --staged <file>..." to unstage)modified: READMEAhang@Ahang MINGW64 ~/Documents/Git (master)$ git commit -m "add some word"[master adde561] add some word1 file changed, 3 insertions(+), 1 deletion(-)Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git statusOn branch masternothing to commit, working tree clean
4. 对修改过的文件还原到修改前和修改后
修改前后跳转:
git reset --hard commit_id- 跳到修改前一次:
commit_id改为HEAD^ - 跳到修改前两次:
commit_id改为HEAD^^ - 跳到修改前10次:
commit_id改为HEAD~10 - 跳转到修改后某个点:
commit_id改为该版本对应得hash值前几个唯一的字母如a3798
- 跳到修改前一次:
查看提交的版本记录,对修改前后,查看每个版本对应得hash值:
git log或git log --pretty=oneline回到跳转前版本,此时查看log没有,需要查看历史记录:
git reflog原理:对多个版本的会记录下多个节点,通过
HEAD指针指向当前版本文档,对文档的版本指定修改实质上也就是修改了HEAD指针的位置
—-vision1—-vision2—-vision3 <—HEAD
# 查看提交的版本记录Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git logcommit a47393fb96dea41a21d9d674b6e12faff06b6edc (HEAD -> master)Author: ahang1598 <hcie_zpc@163.com>Date: Fri Jul 17 21:44:58 2020 +0800add GPLcommit adde56105ebed13fe13dd735343dff32d6ddb8a9Author: ahang1598 <hcie_zpc@163.com>Date: Fri Jul 17 21:34:55 2020 +0800add some word~# 查看提交的版本记录简易版Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git log --pretty=onelinea47393fb96dea41a21d9d674b6e12faff06b6edc (HEAD -> master) add GPLadde56105ebed13fe13dd735343dff32d6ddb8a9 add some wordae27363a081d748911a88b6bbf86054f2373baf3 wrote a README file# 还原上一个版本Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git reset --hard HEAD^HEAD is now at adde561 add some word# 查看内容Ahang@Ahang MINGW64 ~/Documents/Git (master)$ cat READMEHello world!!!This is my second examples!add some word# 此时再查看提交的版本记录,就没有了最后一次提交的了Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git logcommit adde56105ebed13fe13dd735343dff32d6ddb8a9 (HEAD -> master)Author: ahang1598 <hcie_zpc@163.com>Date: Fri Jul 17 21:34:55 2020 +0800add some word# 此时可以通过查看历史记录找到对应得commit_idAhang@Ahang MINGW64 ~/Documents/Git (master)$ git reflogadde561 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^a47393f HEAD@{1}: reset: moving to a4739adde561 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^a47393f HEAD@{3}: commit: add GPLadde561 (HEAD -> master) HEAD@{4}: commit: add some wordcbbddf8 HEAD@{5}: commit: add 6 files7602874 HEAD@{6}: commit: add 3 filesae27363 HEAD@{7}: commit (initial): wrote a README file# 跳转到指定的commit_idAhang@Ahang MINGW64 ~/Documents/Git (master)$ git reset --hard a4739HEAD is now at a47393f add GPL
5. 撤销修改
命令
git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:- 一种是
readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态; - 一种是
readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态
- 一种是
撤销修改☞回到最后一个状态
6. 撤销删除
- 对于已经在仓库/工作区里面的文件删除:
git rm README - 对于删除的文件恢复,先要从版本区恢复到暂存区:
git restore --staged README - 再从暂存区恢复到工作区和仓库中:
git checkout -- README
# 先删除Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git rm READMErm 'README'# 查看状态,已删除Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git statusOn branch masterChanges to be committed:(use "git restore --staged <file>..." to unstage)deleted: README# 此时还没有提交,文件仍在在仓库里面,但是工作区没有了Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git log --pretty=oneline492042c21840a7feb35984cd8a782c2cac7711f1 (HEAD -> master) remove a43ede64fbf10c501c8969c831aa92e53ef4bae07 add boss385781f7b45fab5e48f51addc205ac982b7542ae add a delete714b14f8c577a4b4dedc0b650c01f322bc127080 delete chinese character0f77a88b30fbfe27f50a2255cffb646abdfc82a4 add somea47393fb96dea41a21d9d674b6e12faff06b6edc add GPLadde56105ebed13fe13dd735343dff32d6ddb8a9 add some wordcbbddf8cf9394d2ed57523d87ca3a3546088bc5d add 6 files7602874655e60aed97b4b7f0c3e5eec1a5f97a1b add 3 filesae27363a081d748911a88b6bbf86054f2373baf3 wrote a README file# 工作区已被删除Ahang@Ahang MINGW64 ~/Documents/Git (master)$ lsa 'a[a-c]' b bb cc cc,dd dd# 直接从暂存区无法恢复Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git checkout -- READMEerror: pathspec 'README' did not match any file(s) known to git# 从仓库里面恢复到暂存区Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git restore --staged README# 再从暂存区恢复到工作区Ahang@Ahang MINGW64 ~/Documents/Git (master)$ git restore READMEAhang@Ahang MINGW64 ~/Documents/Git (master)$ lsa 'a[a-c]' b bb cc cc,dd dd README
如果删除后提交了的话需要git reset --hard commit_id去回退操作
7. 连接github操作
7.1 首次将本地库同步到远程库中
echo "# resume" >> README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M maingit remote add origin git@github.com:ahang1598/resume.gitgit push -u origin main
git remote add origin git@github.com:ahang1598/resume.gitgit branch -M maingit push -u origin main
7.2 将本地库同步到远程库中
要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git;
关联后,使用命令git push -u origin master第一次推送master分支的所有内容;
此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改
7.2 将远程库克隆到本地
$ git clone git@github.com:ahang1598/spider.git
7.3 修改部分本地代码后同步远程库
思路是将整个git上的代码都clone下来,然后手动把自己的文件拉进指定文件夹,再上传上去
一.创建一个文件夹”text”来右击点git bash
1.git init2.git clone [https].git
假设clone下来的文件名叫A
二.clone成功后我们打开A,将本地要上传的文件拖到指定文件夹内,然后git bash clone下来的文件A
1.git init 默认下载下来的文件中包含了初始化的文件,无需初始化2.git add . 注意add后面有一个空格和一个点3.git commit -m "提交的注释"git branch -M main 如果本地还是master4. git push origin main
7.4 获取远程仓库并更新到本地仓库
git pull:相当于是从远程获取最新版本并merge到本地
$ git checkout branch2$ git pull origin branch2
7.5在远程仓库和本地仓库都发生了变化后想要将本地修改提交到远程的流程:
git add .,然后git commit提交本地仓库git pull拉取远程仓库,提示不能自动合并- 手动解决矛盾,修改文件后再次
git add.,git commit git push成功
