撤销 Git 缓存

John Wachira 2024年2月15日
  1. 使用 git add 命令收集 Git 目录中的更改
  2. 使用 git commit 命令在 Git 目录中保存暂存更改
  3. 使用 git stash 命令在 Git 目录中保存暂存和未暂存的更改
  4. 在 Git 中使用 git stash pop 命令撤消 git stash
撤销 Git 缓存

本文介绍如何对仓库进行更改和保存。Git 允许你在本地保存更改并在需要时将它们推送到服务器。

在 Git 中,我们不使用术语 save,而是使用 commit。我们使用 git addgit commitgit stash 命令来保存更改。

它还显示了使用 git stash pop 命令撤消 git stash

使用 git add 命令收集 Git 目录中的更改

git add 命令收集项目目录中的所有更改并将它们带到暂存区域。实际上,你指示 Git 在下一次提交中更新你的文件。

你必须运行 git commit 来保存更新。

这就是你如何使用 git add 命令为下一次提交暂存文件或文件夹的方法。

git add Tutorial.txt

对于目录:

git add -Delft

在我们的例子中,我们的工作目录中有一个 Tutorial.txt 文件。如果我们对文件进行了一些更改,我们将使用 git add Tutorial.txt 命令来暂存提交的更改。

使用 git commit 命令在 Git 目录中保存暂存更改

我们使用 git commit 命令在我们的工作目录中保存任何暂存的更改。我们将此命令与 git add 一起使用。

这些提交就像项目当前状态的快照。

Git 允许你在将更改推送到中央服务器之前收集你认为已收集的更改。这样,只有正在工作并达成一致的内容才能移动到中央服务器。

早些时候,我们使用这个命令暂存了一个文件。

git add Tutorial.txt

我们使用 git status 命令检查下面的输出。

pc@JOHN MINGW64 ~/Git (main)
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Tutorial.txt

要保存更改,请运行 git commit

pc@JOHN MINGW64 ~/Git (main)
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   Tutorial.txt
$git commit

输出:

git 提交

总结第一行的提交。将其留空将结束提交。

使用 git stash 命令在 Git 目录中保存暂存和未暂存的更改

我们使用 git stash 命令来保存暂存和未暂存的更改。使用 git status 命令检查你的脏工作区。

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   Tutorial.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    text.txt.txt
        modified:   text.txt.txt.bak

我们可以使用 git stash 命令来保存上述更改以处理其他事情。

pc@JOHN MINGW64 ~/Git (main)
$ git stash
Saved working directory and index state WIP on main: 195e5c3 $git status

运行 git status

pc@JOHN MINGW64 ~/Git (main)
$ git status
On branch main
nothing to commit, working tree clean

在 Git 中使用 git stash pop 命令撤消 git stash

要撤消 git stash,请使用 git stash pop 命令。它会将你的存储重新应用于你的工作副本。

pc@JOHN MINGW64 ~/Git (main)
$ git stash pop
Removing text.txt.txt
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Tutorial.txt
        deleted:    text.txt.txt
        modified:   text.txt.txt.bak

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (e1fdba2aaecc32e7ad546de1586a2381f812a5dd)
作者: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

相关文章 - Git Stash