蜘蛛seo超级外链工具,手机优化设置,图片网站如何优化,外卖网站开发能多少钱文章目录 前言一、场景11.1 删除分支1.2 修改message信息1.2.1 最新一次commit的message1.2.2 过去commit的message 1.3 合并commit1.3.1 多个连续commit合并1.3.2 不连续commit合并 二、场景22.1 比较暂存区和HEAD所含文件的差异2.2 比较工作区和暂存区所含文件的差异2.3 将暂… 文章目录 前言一、场景11.1 删除分支1.2 修改message信息1.2.1 最新一次commit的message1.2.2 过去commit的message 1.3 合并commit1.3.1 多个连续commit合并1.3.2 不连续commit合并 二、场景22.1 比较暂存区和HEAD所含文件的差异2.2 比较工作区和暂存区所含文件的差异2.3 将暂存区恢复成和HEAD的一样2.4 将工作区文件恢复为和暂存区一样2.5 取消暂存区部分文件的更改 三、场景33.1 消除最近几次提交3.2 查看不同commit的指定文件的差异3.3 删除文件3.4 临时存放 四、场景44.1 指定不需要Git管理的文件4.2 备份4.2.1 Git备份4.2.2 传输协议4.2.3 备份操作4.2.4 同步 五、总结 前言 一、场景1
1.1 删除分支
git branch -av查看所有分支详细信息 git branch -d branch_name 删除指定分支
git branch -av
* detach_branch 32b1bb0 add file1master 11067be add file2temp 1395813 add readme# 此时处于detach_branch分支中所以未能删除成功
git branch -d detach_branch
error: Cannot delete branch detach_branch checked out at C:/Users/asdfv/Desktop/testgit checkout master
Switched to branch master
# 切换到master分支后就能成功删除detach_branch分支
git branch -d detach_branch
Deleted branch detach_branch (was 32b1bb0).git branch -av
* master 11067be add file2temp 1395813 add readme1.2 修改message信息
1.2.1 最新一次commit的message
通过git commit --amend命令修改最新一次commit的message信息即HEAD指向的commit的message。
git log
commit 11067be5b6012377a079c3a9635f5f7af56f7de4 (HEAD - master)
Author: tom tom123.com
Date: Thu Nov 9 22:09:51 2023 0800add file2commit 32b1bb0bac4ccac8665c998fbd7cf1491bbeeda5
Author: tom tom123.com
Date: Thu Nov 9 22:09:03 2023 0800add file1commit 1395813160b9b8f42f8a42f84eb7845fdfaf0424 (temp)
Author: xxxx163.com xxx163.com
Date: Wed Nov 8 15:29:01 2023 0800add readmegit commit --amend # 会弹出信息直接编辑修改首行的内容并保存即可首行内容即message信息
[master cb27ad9] add file2 second editDate: Thu Nov 9 22:09:51 2023 08001 file changed, 0 insertions(), 0 deletions(-)create mode 100644 file2git log
commit cb27ad90d2aeac53968b4d74a0b5cf17f92ab06e (HEAD - master)
Author: tom tom123.com
Date: Thu Nov 9 22:09:51 2023 0800add file2 second edit # message信息已发生变化commit 32b1bb0bac4ccac8665c998fbd7cf1491bbeeda5
Author: tom tom123.com
Date: Thu Nov 9 22:09:03 2023 0800add file1commit 1395813160b9b8f42f8a42f84eb7845fdfaf0424 (temp)
Author: xxx163.com xxx163.com
Date: Wed Nov 8 15:29:01 2023 0800add readme
1.2.2 过去commit的message
修改过去commit的message信息分为两步骤 1rebase到目标commit的父亲 查看下当前版本历史
git log --graph --all
* commit 69cde788edb4184538f2155ea5e062f5649e8781 (detach_branch)
| Author: xxxxxx163.com
| Date: Thu Nov 9 15:56:42 2023 0800
|
| detach modify file
|
| * commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
| | Author: xxxxxx163.com
| | Date: Thu Nov 9 10:22:51 2023 0800
| |
| | add temp_test
| |
| * commit 01df9fd5e046f104312468746168b027f4285c5c (HEAD - master)
|/ Author: xxxxxx163.com
| Date: Thu Nov 9 10:22:04 2023 0800
|
| add file1
|
* commit db2d096bf27e4e8f4ca42e8b185e973b05e186dfAuthor: xxxxxx163.comDate: Thu Nov 9 10:21:31 2023 0800add readme拟修改detach_branch分支对应的commit的message信息此时就需要先进入到该commit的父亲commit中
git rebase -i db2d096bf进入到如下界面
2修改command 将上图中的command命令从pick修改为reword或r保存退出后会自动进入目标commit的内容编辑页面直接编辑修改首行的message信息保存退出即可。
git rebase -i db2d096bf
[detached HEAD f554f2d] add file1 rebaseDate: Thu Nov 9 10:22:04 2023 08001 file changed, 0 insertions(), 0 deletions(-)create mode 100644 file1
Successfully rebased and updated refs/heads/master.1.3 合并commit
1.3.1 多个连续commit合并
git log
commit ccad7ee6c3065a3bac0deaa4df8cb65f36802ea6 (HEAD - master)
Author: xxxxxx163.com
Date: Mon Aug 1 14:53:09 2022 0800add newnewnew css# 合并下面两个commit
commit 46d4543880a2864511fa7d1cef3419b5bdc87367
Author: xxxxxx163.com
Date: Mon Aug 1 14:52:27 2022 0800add aaa index.htmlcommit 7d1a828cc8c3e45cb98605dc9032de372e1d0c4c
Author: xxxxxx163.com
Date: Mon Aug 1 14:51:21 2022 0800add cpp# 父亲commit
commit b36e3b75d6129902a7251ec74b5a14b521c16839
Author: xxxxxx163.com
Date: Mon Aug 1 13:17:59 2022 0800add readme当要合并多个连续的commit时需要找到这几个commit的parent父亲并进入rebase
git rebase -i b36e3b75d # 进入到父亲commit的rebase环境因为是将多个连续的commit合并所以实际上是将这些commit都整合到这些commit中的第一个commit时间线最远的那个。 此时该第一个commit的command保持不变即pick后面需要整合的commit它们的command就要改为ssquash合并保存退出后进入到第一个commit中。
进入到第一个commit的信息编辑中添加上相关message信息即可保存退出。
commit合并退出。
git rebase -i b36e3b75d6
[detached HEAD 15b3bed] this a combination of cpp_aaa_indexhtml
Author: xxxxxx163.com
Date: Mon Aug 1 14:51:21 2022 0800
2 files changed, 0 insertions(), 0 deletions(-)
create mode 100644 index.html
create mode 100644 test1.cpp
Successfully rebased and updated refs/heads/master.git log
commit 5333266fd52079006ff91ce038e93555cd8d9805 (HEAD - master)
Author: xxxxxx163.com
Date: Mon Aug 1 14:53:09 2022 0800add newnewnew csscommit 15b3bed924f8670f71fb878401f9e93ed52b85b6
Author: xxxxxx163.com
Date: Mon Aug 1 14:51:21 2022 0800this a combination of cpp_aaa_indexhtmladd cppadd aaa index.htmlcommit b36e3b75d6129902a7251ec74b5a14b521c16839
Author: xxx xxx163.com
Date: Mon Aug 1 13:17:59 2022 0800add readme1.3.2 不连续commit合并
现欲将两个不连续的commit整合成一个commit
选择时间线最远git树中最靠前的commit来作为base进行合并
git rebase -i 弹出内容编辑页面初始内容如下
但目标是要合并自身和不连续的另一个commit这边就在首行插入内容
pick
s修改完保存并返回界面跳出告警
执行continue即可表示仍按照间隔commit合并的操作来
弹出内容框添加message即可
保存退出后会看到成功信息
二、场景2
这个场景主要是对暂存区和工作区两者之间的相关处理。
2.1 比较暂存区和HEAD所含文件的差异
git add head_filegit status
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_filegit diff --cached
diff --git a/head_file b/head_file
new file mode 100644
index 0000000..e69de292.2 比较工作区和暂存区所含文件的差异
git diff # 默认比较工作区所有文件
diff --git a/index.css b/index.css
index e69de29..dac138d 100644
--- a/index.cssb/index.css-0,0 1
css
diff --git a/index.html b/index.html
index e69de29..dd6a11e 100644
--- a/index.htmlb/index.html-0,0 1,4
index.htmlgit diff -- index.html # -- 指定文件与暂存区比较可指定多个文件
diff --git a/index.html b/index.html
index e69de29..dd6a11e 100644
--- a/index.htmlb/index.html-0,0 1,4
index.html2.3 将暂存区恢复成和HEAD的一样
git status # 此时暂存区内容
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_filegit restore --staged head_file # 将暂存区内容恢复成和HEAD指针指向的内容一致即撤销暂存区内的改动git diff --cached # 恢复完成后此时暂存区和HEAD应该无差异输出为空
2.4 将工作区文件恢复为和暂存区一样
在将文件添加至暂存区后又对工作区内该文件进行了修改。 若此时修改的内容不符合预期现在想将暂存区内该文件恢复到工作区覆盖修改即撤销工作区内文件的改动。
git add head_file # 添加至暂存区
warning: LF will be replaced by CRLF in head_file.
The file will have its original line endings in your working directorygit status # 此时git提示有个head_file文件需要commit
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_filevi head_file # 修改head_file内容git status
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_fileChanges 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: head_filecat head_file # 文件内容添加了第二行但此时感觉这个内容不满意需要回退成暂存区时文件内容
test test test
second editgit restore head_file # 将工作区指定文件恢复成和暂存区一样即删除之前修改的部分cat head_file
test test testgit restore -- head_file index.css # 可同时对多个文件进行恢复若暂存区没有某一文件而却执行了该文件的恢复操作则会清空工作区内该文件的内容。 2.5 取消暂存区部分文件的更改
git status
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_fileChanges 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: head_filegit restore --staged head_file # 将暂存区指定文件的修改撤销git status
On branch master
Untracked files:(use git add file... to include in what will be committed)head_filenothing added to commit but untracked files present (use git add to track)
三、场景3
3.1 消除最近几次提交
首先将分支切换到temp上用于测试演示。可以看到目前有下列几个commit提交。
git checkout temp
Switched to branch tempgit log
commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (HEAD - temp)
Author: xxxxxx163.com
Date: Thu Nov 9 10:22:51 2023 0800add temp_testcommit 01df9fd5e046f104312468746168b027f4285c5c
Author: xxxxxx163.com
Date: Thu Nov 9 10:22:04 2023 0800add file1commit db2d096bf27e4e8f4ca42e8b185e973b05e186df # 拟删除该commit之后的所有提交
Author: xxxxxx163.com
Date: Thu Nov 9 10:21:31 2023 0800add readme删除temp分支的最近几次提交也就是将HEAD指针指向前面某次commit删除该commit后的所有提交。
git reset --hard db2d096bf27e # 删除该commit之后的所有commit同时HEAD指向该commit
HEAD is now at db2d096 add readmegit log
commit db2d096bf27e4e8f4ca42e8b185e973b05e186df (HEAD - temp)
Author: xxx xxx163.com
Date: Thu Nov 9 10:21:31 2023 0800add readme
3.2 查看不同commit的指定文件的差异
git diff后跟两个commit的哈希ID或者是两个分支名称最后是指定需要比较查看的文件名称。
git diff temp master -- file1
diff --git a/file1 b/file1
new file mode 100644
index 0000000..e69de293.3 删除文件
git rm 删除指定文件并将该操作同步到暂存区待后续commit完成后便会在仓库内删除。
git rm readme
rm readmegit status
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_filedeleted: readme
3.4 临时存放
比如说在开发过程中突然加塞进一个紧急任务此时就可将现有编辑操作现有工作区存入stash区当在工作区的操作结束之后可直接将stash区内的内容恢复回来。
git stash
Saved working directory and index state WIP on master: f554f2d add file1 rebasegit stash list
stash{0}: WIP on master: f554f2d add file1 rebase当紧急任务修改文件、添加至暂存区、commit提交完成后将stash区恢复
git stash apply 不会删除stash区内信息 git stash pop 会删除stash内容
git stash apply
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_filegit stash list
stash{0}: WIP on master: f554f2d add file1 rebasegit reset --hard HEAD # 清一下暂存区恢复暂存区状态
HEAD is now at f554f2d add file1 rebasegit status
On branch master
nothing to commit, working tree cleangit log
commit f554f2dd10181c867f08c6c5ae2e90157ba8919e (HEAD - master)
Author: xxxxxx163.com
Date: Thu Nov 9 10:22:04 2023 0800add file1 rebasecommit db2d096bf27e4e8f4ca42e8b185e973b05e186df (temp)
Author: xxxxxx163.com
Date: Thu Nov 9 10:21:31 2023 0800add readmegit stash list
stash{0}: WIP on master: f554f2d add file1 rebasegit stash pop # 将stash区恢复pop是同时会删除stash区内容的
On branch master
Changes to be committed:(use git restore --staged file... to unstage)new file: head_fileDropped refs/stash{0} (099ac55e8e11781ad41d9b2112cf0c8d9573eeef)git stash list # 再次查看stash区此时已无内容四、场景4
4.1 指定不需要Git管理的文件
.gitignore文件设定项目中哪些文件不需要被纳入Git管理。
git status
On branch master
Untracked files:(use git add file... to include in what will be committed)head_filenothing added to commit but untracked files present (use git add to track)vi .gitignore
head_filegit status
On branch master
Untracked files:(use git add file... to include in what will be committed).gitignorenothing added to commit but untracked files present (use git add to track)若存在目录以及同名文件例如都为web此时在.gitignore文件中的内容应为 web # 表示包括web文件以及web目录及其里面的文件
web/ # 只表示web目录及其里面的文件不包含web文件4.2 备份
4.2.1 Git备份
Git备份是将原有的Git仓库从一个地方传输到另一个地方做备份需要用到传输协议。
4.2.2 传输协议
常用协议语法格式说明本地协议1/path/to/repo.git哑协议本地协议2file:///path/to/repo.git智能协议http/https协议http://server.com:port/path/to/repo.git https://server.com:port/path/to/repo.git智能协议ssh协议usergit-server.com:path/to/repo.git工作中常接触的智能协议 除了哑协议其他都是智能协议。两者的直观区别 1哑协议传输进度不可见智能协议传输进度可见 2智能协议比哑协议传输速度要快 4.2.3 备份操作
进入要备份的仓库目录通过命令git clone --bare [file://]仓库源路径/.git 目标备份仓库名.git来实现项目仓库的备份。
# 哑协议
/g/test (master)
git clone --bare G:\\test\\.git bck.git
Cloning into bare repository bck.git...
done.# 智能协议有进度提示
/g/test (master)
git clone --bare file://G:\\test\\.git zhineng.git
Cloning into bare repository zhineng.git...
remote: Enumerating objects: 69, done.
remote: Counting objects: 100% (69/69), done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 69 (delta 28), reused 21 (delta 7), pack-reused 0
Receiving objects: 100% (69/69), 6.50 KiB | 2.17 MiB/s, done.
Resolving deltas: 100% (28/28), done./g/test (master)
git clone file://G:\\test\\.git zhineng2.git
Cloning into zhineng2.git...
remote: Enumerating objects: 69, done.
remote: Counting objects: 100% (69/69), done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 69 (delta 28), reused 21 (delta 7), pack-reused 0
Receiving objects: 100% (69/69), 6.50 KiB | 739.00 KiB/s, done.
Resolving deltas: 100% (28/28), done.--bare 表示只拷贝 .git文件夹及其内容不拷贝项目工程文件 不加--bare的话就会拷贝包括.git目录以及工程文件的所有文件。 4.2.4 同步
仓库备份之后后续对于源仓库修改的内容也要及时地同步到备份仓库那里。 要想同步首先需要把源仓库和备份关联起来通过git remote add添加远程库。
/g/test (master)
git remote add zhineng file://G:\\test\\zhineng2.git/g/test (master)
git remote -v
zhineng file://G:/test/zhineng2.git (fetch)
zhineng file://G:/test/zhineng2.git (push)不管是备份到本地还是备份到云端在Git看来都是远程仓库。 远程仓库用什么协议哑协议、智能协议创建的后续在进行关联添加远程库的时候就要指定同样的协议。 后续源仓库有变动时提交push到对应远程库即可更新到备份库中。
/g/test (master)
git checkout -b test
Switched to a new branch test/g/test (test)
git push zhineng
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To file://G:/test/zhineng2.git* [new branch] test - test五、总结
本节有很多在使用Git时遇到的场景得熟悉熟用。