git實戰10 – 使用git rm移除已追縱已提交的檔案


建立目錄

mkdir git_test_4
cd git_test_4
ll
total 0

建立 git 倉庫

git init
Initialized empty Git repository in /home/ben/git_test_4/.git/

建立 3 個檔案

echo 1 > 1.txt
echo 2 > 2.txt
echo 3 > 3.txt
ll
total 12
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 1.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 2.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 3.txt

查看目前狀態

git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       1.txt
#       2.txt
#       3.txt
nothing added to commit but untracked files present (use "git add" to track)

加入所有檔案並提交

git add .
git commit -m 'master 1'
[master (root-commit) d06fc64] master 1
3 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 1.txt
create mode 100644 2.txt
create mode 100644 3.txt
ll
total 12
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 1.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 2.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 3.txt

查看檔案 2.txt 的內容

cat 2.txt
2

現在移除檔案 2.txt

git rm 2.txt
rm '2.txt'
ll
total 8
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 1.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 3.txt

再一次查看狀態

git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    2.txt
#

提交

git commit -m 'master 2'
[master 199959a] master 2
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 2.txt

查看狀態

git status
# On branch master
nothing to commit (working directory clean)

查看目錄

ll
total 8
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 1.txt
-rw-rw-r-- 1 ben ben 2 Jul  4 10:29 3.txt