简介
Git 是一个强大的版本控制系统,它帮助开发者管理他们项目的文件历史记录。有时候,文件会被缓存在仓库中,而我们不再希望 Git 跟踪它们,但我们又想将它们保留在本地目录中。git rm --cached 命令允许我们从 Git 的跟踪系统中移除文件,同时保留它们在我们的工作目录中。本教程将教你如何有效地使用这个命令来清理你的仓库并优化你的工作流程。
Git 是一个强大的版本控制系统,它帮助开发者管理他们项目的文件历史记录。有时候,文件会被缓存在仓库中,而我们不再希望 Git 跟踪它们,但我们又想将它们保留在本地目录中。git rm --cached 命令允许我们从 Git 的跟踪系统中移除文件,同时保留它们在我们的工作目录中。本教程将教你如何有效地使用这个命令来清理你的仓库并优化你的工作流程。
为了理解如何从 Git 中移除缓存的文件,让我们首先设置一个包含一些文件的示例仓库。这将帮助我们实际了解 Git 缓存的工作方式。
当你使用 git add 命令将文件添加到 Git 时,Git 会将这些文件存储在其索引(也称为暂存区)中。这些文件现在被“缓存”或暂存,等待提交到仓库。有时,你可能希望取消暂存这些文件,或者从 Git 的跟踪中移除它们,而无需从你的本地文件系统中删除它们。
让我们创建一个简单的 Git 仓库来使用:
cd ~/project
mkdir git-cache-demo
cd git-cache-demo
git init
你应该看到类似于以下的输出:
Initialized empty Git repository in /home/labex/project/git-cache-demo/.git/
git config user.name "LabEx User"
git config user.email "labex@example.com"
现在,我们有了一个全新的 Git 仓库,准备好添加文件了。在下一步中,我们将创建一些文件并将它们添加到 Git 的跟踪系统中,这将允许我们稍后练习从缓存中移除它们。
现在我们已经设置好了 Git 仓库,让我们创建一些文件并将它们添加到 Git 的跟踪系统中。这将帮助我们理解文件在 Git 中被“缓存”的含义。
## Create a text file
echo "This is a sample text file" > sample.txt
## Create a config file
echo "debug=true" > config.ini
## Create a log file (which we typically don't want to track)
echo "2023-01-01: System started" > app.log
git status
你应该看到类似于以下的输出:
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
app.log
config.ini
sample.txt
nothing added to commit but untracked files present (use "git add" to track)
这表明我们有三个 Git 识别的文件,但它们尚未被跟踪。
git add .
git status
现在你应该看到:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: app.log
new file: config.ini
new file: sample.txt
请注意,Git 现在告诉我们可以使用 git rm --cached <file> 来取消暂存文件。这些文件现在缓存在 Git 的暂存区中,等待被提交。
git commit -m "Initial commit with sample files"
你现在已经成功地将文件添加到了 Git 的跟踪系统中。在下一步中,我们将学习如何从 Git 的缓存中移除特定的文件,同时将它们保留在我们的本地目录中。
现在我们已经有了 Git 跟踪的文件,让我们学习如何从 Git 的跟踪中移除一个特定的文件,同时将其保留在我们的本地目录中。当你意外提交了应该被排除的文件(例如日志文件、临时文件或包含敏感信息的文件)时,这是一种常见的需求。
你可能希望从 Git 的缓存中移除文件的原因有几个:
.gitignore 文件,并且需要移除已经被跟踪的文件让我们想象一下,我们已经意识到 app.log 文件不应该被 Git 跟踪:
git ls-files
你应该看到列出了所有三个文件:
app.log
config.ini
sample.txt
app.log,同时将其保留在我们的本地目录中:git rm --cached app.log
你将看到一条确认消息:
rm 'app.log'
git status
你将看到 app.log 现在被列为未跟踪的文件:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: app.log
Untracked files:
(use "git add <file>..." to include in what will be committed)
app.log
这意味着 Git 将在下一次提交中停止跟踪该文件,但该文件仍然存在于你的本地目录中。
ls -la
你应该看到 app.log 仍然在那里。
git commit -m "Remove app.log from Git tracking"
git ls-files
现在你应该只看到:
config.ini
sample.txt
但是 app.log 文件仍然存在于你的本地目录中:
cat app.log
输出:
2023-01-01: System started
恭喜你!你已经成功地从 Git 的缓存中移除了一个文件,同时将其保留在你的本地目录中。在下一步中,我们将学习如何处理多个文件,并使用 .gitignore 改进我们的工作流程。
现在我们知道了如何从 Git 的缓存中移除单个文件,让我们探索更复杂的场景,例如移除多个文件或整个目录。
让我们首先创建一些文件和一个目录结构来进行练习:
## Create a directory for temporary files
mkdir temp
## Create some files in the temp directory
echo "This is a temporary file" > temp/temp1.txt
echo "Another temporary file" > temp/temp2.txt
## Create another log file in the main directory
echo "2023-01-02: System updated" > system.log
git add .
git commit -m "Add temporary files and system log"
git ls-files
你应该看到:
app.log
config.ini
sample.txt
system.log
temp/temp1.txt
temp/temp2.txt
现在假设我们想要从 Git 的跟踪中移除所有日志文件和整个 temp 目录。
git rm --cached system.log
git rm --cached -r temp/
这里的 -r 标志很重要,因为它告诉 Git 递归地从其缓存中移除目录中的所有文件。
git status
你将看到日志文件和 temp 目录中的所有文件都已暂存,准备从 Git 的跟踪系统中删除:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: system.log
deleted: temp/temp1.txt
deleted: temp/temp2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
system.log
temp/
git commit -m "Remove logs and temp directory from Git tracking"
git ls-files
现在你应该只看到:
app.log
config.ini
sample.txt
但是,所有文件仍然存在于你的本地目录中:
ls -la
ls -la temp/
现在我们已经从 Git 的跟踪中移除了文件,让我们设置一个 .gitignore 文件,以防止它们被意外地再次添加:
.gitignore 文件:nano .gitignore
## Ignore log files
*.log
## Ignore temp directory
temp/
保存并退出(按 Ctrl+X,然后按 Y,然后按 Enter)
添加并提交 .gitignore 文件:
git add .gitignore
git commit -m "Add .gitignore file"
现在,即使你尝试将所有文件添加到 Git,它也会遵守你的 .gitignore 文件,并且不会跟踪指定的模式:
git add .
git status
你应该看到日志文件和 temp 目录没有被添加到 Git 的跟踪中。
你现在已经学习了如何从 Git 的缓存中移除多个文件和目录,以及如何使用 .gitignore 文件来防止将来跟踪特定的文件。
现在你已经了解了从 Git 缓存中移除文件的基础知识,让我们探索一些高级技术和最佳实践,以改进你的工作流程。
如果你有已经被 Git 跟踪的文件,并且你想同时从跟踪中移除它们并将它们添加到你的 .gitignore 文件中,你可以使用这种有效的方法:
## Create a build directory with some compiled files
mkdir build
echo "Compiled binary data" > build/app.bin
echo "Configuration for build" > build/build.conf
git add build/
git commit -m "Add build files temporarily"
.gitignore 文件:## First, edit the .gitignore file to add the build directory
echo "## Ignore build directory" >> .gitignore
echo "build/" >> .gitignore
## Now remove the tracked files from Git's cache
git rm --cached -r build/
## Commit both changes together
git add .gitignore
git commit -m "Remove build directory from tracking and add to .gitignore"
git ls-files
ls -la build/
如果你不小心提交了一个包含敏感信息(如密码或 API 密钥)的文件,从 Git 的缓存中移除它只是第一步。Git 维护所有提交的历史记录,因此敏感信息仍然存在于你的仓库的历史记录中。
对于敏感信息,你需要:
git filter-branch 或 BFG Repo-Cleaner 等工具从历史记录中删除敏感数据这超出了本教程的范围,但了解此限制很重要。
以下是一些要遵循的最佳实践:
在你的项目中尽早创建一个好的 .gitignore 文件: 这可以防止意外跟踪不需要的文件。
对常见模式使用全局 .gitignore 文件: 你可以设置一个全局 .gitignore 文件,该文件适用于你的所有仓库:
git config --global core.excludesfile ~/.gitignore_global
小心使用 git add .:此命令添加所有未跟踪的文件。尽可能使用更具体的命令,例如 git add <file>。
在提交之前查看更改:始终使用 git status 和 git diff --cached 来查看你即将提交的内容。
对常用操作使用别名:例如,你可以设置一个别名来移除缓存的文件:
git config --global alias.uncache 'rm --cached'
然后你可以使用:
git uncache <file>
通过这些技术和最佳实践,你现在对如何有效地管理 Git 的缓存有了全面的了解,以维护一个干净高效的仓库。
在本教程中,你学习了如何有效地使用 git rm --cached 命令从 Git 的跟踪系统中移除文件,同时将它们保留在你的本地目录中。以下是你完成的内容:
git rm --cached 从 Git 的缓存中移除了单个文件-r) 管理了多个文件和目录.gitignore 来防止跟踪不需要的文件这些技能将帮助你维护一个干净高效的 Git 仓库,防止跟踪不需要的文件,并保护敏感信息。通过正确管理 Git 跟踪哪些文件,你可以专注于重要的代码和配置文件,同时忽略临时文件、日志和构建产物。
请记住,从 Git 的缓存中移除文件并不会从你的本地文件系统中删除它们——它只是告诉 Git 停止跟踪它们。这是一个用于管理你的仓库内容并确保只有必要的文件包含在你的项目历史记录中的强大工具。