git checkout android,05 git checkout 理解

本文详细介绍了Git的各种操作,包括查看库状态、diff、log、reflog等,以及如何处理分离头指针状态。同时,探讨了git checkout的不同用法,如切换分支、恢复工作区和暂存区的内容,以及从特定提交中检出文件。通过实例展示了如何避免丢失提交,并理解各命令对HEAD和master的影响。
摘要由CSDN通过智能技术生成

1,git 库状态:

ll

cat newFile.txt

cat welcome.txt

git status -s

git diff

git diff HEAD

git diff --cached

git log --oneline

git reflog

git reflog -l

git reflog show HEAD

git reflog show master

9af9c60a14a1b85d082c13c807026029.png

2,分离头指针状态与恢复:

cat .git/HEAD

git branch -v

git checkout e5bbef2^

cat .git/HEAD

git reflog -l

git rev-parse HEAD master

023685379952e576ccfb7f6592dab219.png

touch detached-commit.txt

git add detached-commit.txt

git status

git commit -m "commit in detached HEAD mode."

cat .git/HEAD

git log --graph --oneline

git reflog

git diff

git diff HEAD

git diff --cached

git status

141a4319f534e2a36dff412184ce10ac.png

ll

git checkout master

git .git/HEAD

ll

git log --graph --oneline

git show 4c2ec75

14398a27e94a3af9ee366666e7559fc9.png

3,挽救分离头指针状态的提交损失:

ll

git status -s

git branch -v

git merge 4c2ec75

ll

git status -s

git branch -v

git log --graph --oneline

cd089622d62f9aded2bcb7a9ed301824.png

git reflog show master

git reflog show HEAD

1e8922fcf9e6a1d2e185d1f9cd4b238e.png

git cat-file -p HEAD

8923ac64281e9efb234787c6591435dd.png

4, git checkout 的几种用法

4.1,初始化git库

echo "newFile a1" >> newFile_a1.txt

git add newFile_a1.txt

git commit -m "newFile a1 is added"

echo "newFile a2" >> newFile_a2.txt

git add newFile_a2.txt

git commit -m "newFile a2 is added"

echo "newFile a3" >> newFile_a3.txt

git add newFile_a3.txt

git commit -m "newFile a3 is added"

git log --oneline --graph

675b740179c273cad4c3202cb7a0e77f.png

4.2,git checkout 与 git checkout HEAD

汇总显示工作区、暂存区与HEAD的差异

git status -s

echo "Hello a3." >> newFile_a3.txt

git add newFile_a3.txt

git status -s

echo "Nice to meet you a3." >> newFile_a3.txt

git status -s

872d47e32af94a9ffb3194b68da49d17.png

git status -s

git checkout

git status -s

git checkout HEAD

git status -s

echo "new file a4" >> newFile_a4.txt

git add newFile_a4.txt

git status -s

git checkout

git checkout HEAD

echo "Nice to meet you a4." >> newFile_a4.txt

git status -s

git checkout

git checkout HEAD

aca7a601e057882af0006a57c0169a8f.png

git diff

git diff HEAD

d3b3498fc563ff7de80e8507f970e24a.png

git diff --cached

853cefacc56c00465afde858f42c8576.png

4.3 git checkout [commit] [--]

从指定commit中checkout指定文件覆盖到工作区,但不影响暂存区、HEAD、master指向。

若缺省commit,则默认从暂存区checkout指定文件的上一次add的版本覆盖到工作区,同样不影响暂存区、HEAD、master指向。

git log --oneline --graph

git status -s

git checkout -- newFile_a3.txt

git status -s

git checkout HEAD -- newFile_a3.txt

git status -s

8f19e785ca1919bbc9330177eff897d3.png

缺省[commit]参数的 newFile_a3.txt 的 checkout ,从暂存区覆盖到工作区。

指定[commit]参数为 HEAD 的 newFile_a3.txt 的 checkout ,从HEAD指向的版本覆盖暂存区、工作区。

git reflog show HEAD | head -3

git reflog show master | head -3

a5692de214bb94cd305d5b5673d283dd.png

由此可见 此用法没有改变 HEAD 、master 的指向。

git diff

git diff HEAD

git diff --cached

88336d143a725453c478489f72936138.png

4.4, "git checkout -- ." 与 "git checkout ."

把暂存区目录树checkout覆盖工作区目录树。

git status -s

cat newFile_a3.txt

echo "Hello a3." >> newFile_a3.txt

git add newFile_a3.txt

echo "Nice to meet you ." >> newFile_a3.txt

git status -s

git checkout -- .

git status -s

cat newFile_a3.txt

echo "Nice to meet you." >> newFile_a3.txt

git status -s

git checkout .

git status -s

94b6d2ad9910fce5957d9a7bfbcbbc67.png

4.5,"git checkout [branch]"

实现branch切换,更新 HEAD 指向至 branch,用 HEAD 指向更新暂存区、工作区。

git status -s

git diff --cached

echo "Nice to meet you a3." >> newFile_a3.txt

git status -s

git checkout master

git status -s

5c61e68494018df31b85a496d4a9060b.png

由于只有一个 master branch,所以提示我们already on 'master',并且不做任何改动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值