创建本地分支并拉取远程同名分支?
git clone -b topic https://github.com/llbrh001/gittest.git // 拉取远程topic分支文件
从缓存区撤销一个文件?
git restore --staged "upload4.txt"
撤销一个缓存区文件 / 清空working tree?
git rm --cached "upload4.txt" // 撤销文件缓存
git reset // 清空缓存区
回滚到指定版本?
A. 本地回滚
# 提交一个文件
git add "upload4.txt"
git commit -m "version 1"
type upload4.txt # foo
# 修改文件内容并更新文件
git add "upload4.txt"
git commit -m "version 2"
type upload4.txt # foo smart
# 查看最近3次提交
git log -3
commit ba701550bcd86f37466c4ef8be11e54ceb23c622 (HEAD -> topic)
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 15:45:53 2020 +0800
version 2
commit 9e6d5e42512325af3c52d78fe0b2d09f5be26323
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 15:45:27 2020 +0800
version 1
commit 61ca247c38f98f6046165d317fcce20d750504b0
Author: llbrh001 <435040083@QQ.COM>
Date: Sun Apr 12 16:25:30 2020 +0800
version 0
# 回退head到version1
git reset --hard 9e6d5e42512325af3c52d78fe0b2d09f5be26323
# git reset --hard HEAD~1 回退1个版本
# HEAD is now at 9e6d5e4 version 1
git log -1
#git log --pretty=oneline
commit 9e6d5e42512325af3c52d78fe0b2d09f5be26323 (HEAD -> topic)
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 15:45:27 2020 +0800
version 1
type upload4.txt # foo
B. 远程回滚
文件对比
git diff [filename可选]
A. 对比本地&本地仓库文件内容修改?
type upload5.txt # 查看本地文件, 输出 888 999
git add & commit ...
echo 888 000 > upload5.txt
type upload5.txt # 再次查看本地文件, 输出 888 000
git diff upload5.txt # 输出本地文件和本地仓库的区别
diff --git a/upload5.txt b/upload3.txt
index a46ed29..c349552 100644
--- a/upload5.txt
+++ b/upload5.txt
@@ -1,2 +1,2 @@
888 # 没有改变的数据
-999 # 删除的数据
+000 # 新增的数据
B. 查看发生更改的本地文件
git status
# 返回当前分支,
On branch topic
Your branch and 'origin/topic' have diverged,
and have 5 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
Changes 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: upload3.txt
modified: upload4.txt
C. 对比本地仓库和远程仓库的区别
git fetch origin master
git log master..origin/master # 提交差异
commit 1477998cbbf0b7ed5c02a4cd76ce4cc6bf270a86 (origin/master, origin/HEAD)
Author: llbrh001 <435040083@QQ.COM>
Date: Tue Apr 14 16:57:57 2020 +0800
Create 1.txt
commit a11d4830de9cc353550ef55a0595dc6fb2752ffa
Author: llbrh001 <435040083@QQ.COM>
Date: Tue Apr 14 16:57:39 2020 +0800
Update upload5.txt
git diff --stat master origin/master # 文件差异
1/1.txt | 1 +
upload2.txt | 3 ++-
upload3.txt | 1 -
upload5.txt | 2 ++
4 files changed, 5 insertions(+), 2 deletions(-)
git reset soft / hard?
git reset( soft ) : 跳转头指针, hard 跳转指针并刷新工作树
A. 返回过去节点
git reset --hard HEAD~1 # 重置头指针到上一个版本, 清空stage, 修改working tree
git reset --soft HEAD~1 # 重置头指针到上一个版本, working tree保持当前状态, 所有影响的文件在stage保存
git checkout "upload4.txt" # 将stage文件覆盖到working tree
B. 跳转到head之前的节点
适用于曾经回退到head之前接着又要撤销操作的时候
git reflog -2
9e6d5e4 HEAD@{2}: reset: moving to HEAD~1
20f766a HEAD@{3}: commit: version 2
git reset --hard 20f766a # 本地文件已经改变
操作前 : 9e6d5e4(head) -> 20f766a
操作后 : 9e6d5e4 -> 20f766a (head)
git stash / git stash pop?
说明: 互为逆运算
- 将暂存区的内容存到临时暂存区, 清空暂存区 ;
- 恢复暂存区到最后一次存储的状态, 并刷新工作树
目标: 保存当前状态, 获得干净的工作树
使用场景: 当我做了一部分修改到暂存区但是没有完成, 不准备commit 又不想丢弃stage的缓存数据
- 当我拉取远程文件但是和当前本地修改有冲突
- 当我需要临时切换到其他分支工作
- 当我临时需要使用干净的stage
# 测试1
git add file
git status
Changes to be committed:
new file: upload5.txt
git stash # 暂存区快照
git status
nothing to commit, working tree clean
git stash pop # 恢复快照
git status
new file: upload5.txt
# 测试2
git checkout upload4.txt # 当前文件888
edit upload4.txt & git add ... # 当前文件修改为 888 999
git stash
git status # nothing to commit, working tree clean, 当前文件回到888
git stash pop
git status # modified: upload4.txt, 当前文件回到888 999
git merge使用说明?
将目标分支合并到当前分支, 并在前面新增一个头节点(fast-forward策略), 使用三路合并算法
image.png
git checkout master # 当前为master
git merge origin/topic # 将topic分支合并到当前master分支
Updating 71619e9..84d3bad
Fast-forward
upload2.txt | 2 +-
upload3.txt | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
create mode 100644 upload3.txt
git revert ?
撤销到某次提交并为此创建最新一个节点
git restore --staged filename?
将指定文件从暂存区撤销
git revert?
还原到指定一个提交, 并在最前方创建一个新节点
git log -2
commit fb2861594deca5cf71236c23c1b3ccdaf1bd61a7 (HEAD -> topic)
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 23:06:03 2020 +0800
revert test
commit 8f226825cb2357116e1ea1eb710cc62322552572
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 22:22:54 2020 +0800
edit2
git revert HEAD
git log -2
commit 7ea5d42c6fdc308943c117fb92666e51ad3c1799 (HEAD -> topic)
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 23:16:05 2020 +0800
Revert "revert test"
This reverts commit fb2861594deca5cf71236c23c1b3ccdaf1bd61a7.
commit fb2861594deca5cf71236c23c1b3ccdaf1bd61a7
Author: llbrh001 <435040083@QQ.COM>
Date: Mon Apr 13 23:06:03 2020 +0800
revert test
git checkout 两种用法:
1. git checkout topic // 切换分支
2. git checkout "upload4.txt" // 拉取本地库的文件
切换分支并提交文件?
# 假设已经在可提交的环境下
git branch topic // 创建分支topic
git checkout topic // 切换到topic分支
git add "upload3.txt"
git commit -m "first commit to topic"
git push origin topic
本地文件夹绑定远程仓库?
- 直接clone 远程文件夹
- init初始化本地仓库, remote add 关联指定远程仓库, pull 拉取最新文件
# 无本地仓库
git clone https://github.com/llbrh001/gittest.git // 拉取远程文件
# 有本地仓库
git init
git remote add origin https://github.com/llbrh001/gittest.git // 关联远程仓库
拉取远程文件 ?
git pull origin master # 拉取主分支
git pull origin topic:topic # 拉取远程分支到当前分支
提交到远程文件 ?
git push origin master
如何处理冲突?
获取最新远程文件并不更新本地 --> 对比文件差异 --> 手动合并版本 --> 提交往前推送一个版本 --> 完成
git fetch origin master
git diff --stat master origin/master
# 手动处理冲突
git add & commit & pull
网友评论