跳转至

9. git缓冲区

9. git缓存区

1.生成新的git仓库git04,进行一次版本提交

#1.生成新的git仓库git04
git init /cp_git/git04

#2.模拟添加代码
cd git04
touch chupeng.txt

#3.添加跟踪
git add chupeng.txt

#4.提交
git commit -m "提交"

#5.查看版本
git log --oneline
[root@chupeng cp_git]# git init /cp_git/git04
Initialized empty Git repository in /cp_git/git04/.git/
[root@chupeng cp_git]# cd git04
[root@chupeng git04]# touch chupeng.txt
[root@chupeng git04]# ls
chupeng.txt
[root@chupeng git04]# git add chupeng.txt
[root@chupeng git04]# git commit -m "首次提交"
[master (root-commit) db2fda5] 首次提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 chupeng.txt
[root@chupeng git04]# git log --oneline
db2fda5 首次提交

2.工作区新建新文件,提交到缓存区

#1.修改代码
echo "haha" >stash.txt

#2.跟踪
git add stash.txt

#3.查看状态
git status
[root@chupeng git04]# echo "hahaha" >stash.txt
[root@chupeng git04]# ls
chupeng.txt  stash.txt
[root@chupeng git04]# git add stash.txt
[root@chupeng git04]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   stash.txt
#

3.此时突然需要处理紧急的事情,可以将当前内容保存到stash缓存区

# 适用场景:手头上代码不算紧急,突然要处理紧急的事情时,可以先把手头的放到缓存区中!

#1.查看缓存区
git stash list

#2.添加到stash缓存区
git stash save "创建了一个stash文件"

#3.查看文件,发现刚才新建的代码文件不见了
ls

#4.查看stash缓存区列表
git stash list
[root@chupeng git04]# ls
chupeng.txt  stash.txt
[root@chupeng git04]# git stash list
[root@chupeng git04]# git stash save "创建了一个stash文件"
Saved working directory and index state On master: 创建了一个stash文件
HEAD is now at db2fda5 首次提交
[root@chupeng git04]# ls
chupeng.txt
[root@chupeng git04]# git stash list
stash@{0}: On master: 创建了一个stash文件

4.开始处理紧急业务,提交版本2

#1.模拟创建代码
touch cp_v2

#2.查看状态
git status

#3.添加追踪
git add cp_v2

#4.查看状态
git status

#5. 提交
git commit -am "second commit"

#6. 查看状态
git status

#7. 查看版本日志
git log --oneline
[root@chupeng git04]# touch cp_v2
[root@chupeng git04]# ls
chupeng.txt  cp_v2
[root@chupeng git04]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       cp_v2
nothing added to commit but untracked files present (use "git add" to track)
[root@chupeng git04]# git add cp_v2
[root@chupeng git04]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   cp_v2
#
[root@chupeng git04]# git commit -m "第二次提交"
[master e4c9c8a] 第二次提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 cp_v2
[root@chupeng git04]# git status
# On branch master
nothing to commit, working directory clean
[root@chupeng git04]# git log --oneline
e4c9c8a 第二次提交
db2fda5 首次提交

5.处理完紧急任务,找回缓存区数据,继续工作

#1.查看缓存区内容
git stash list

#2.查看本地文件
ls

#3.找回缓存区数据, 其中stash@{0}可以从第一步得到!
git stash pop stash@{0}

#4. 再次查看本地文件
[root@chupeng git04]# git stash list
stash@{0}: On master: 创建了一个stash文件
[root@chupeng git04]# ls
chupeng.txt  cp_v2

[root@chupeng git04]# git stash pop stash@{0}
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   stash.txt
#
Dropped stash@{0} (97fce1bfeeabafc67212609c98100fa1f9d01161)
[root@chupeng git04]# ls
chupeng.txt  cp_v2  stash.txt

最后更新: 2022-02-18 11:26:47