git rmコマンドを活用しよう|基本的な使い方から各種オプションの使い方について紹介

git rmでファイルを削除してみよう!
今回は、git rmコマンドの使い方について説明します。git rmコマンドを使用すると、git管理対象から削除できます。
基本的な使い方から、各種オプションの使い方について紹介します。また、直前のgit rmを取り消す方法についても紹介します。
興味のある方はぜひご覧ください。
ここでは「sample」リポジトリを使用します。
基本的な使い方
git rmコマンドの基本的な使い方を紹介します。ディレクトリが以下の状態とします。
1
2
|
~/sample$ ls
readme.txt test.txt
|
以下のように、git rmコマンドを使用します。
1
2
|
~/sample$ git rm test.txt
rm 'test.txt'
|
git statusで状態を確認します。「deleted」と表示されています。
1
2
3
4
5
6
7
8
|
~/sample$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
|
commit, pushします。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
~/sample$ git commit -m "delete test.txt"
[master 5c387d6] delete test.txt
1 file changed, 3 deletions(-)
delete mode 100644 test.txt
~/sample$ git push
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 487 bytes | 243.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
05937f9..5c387d6 master -> master
~/sample$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
~/sample$
|
ディレクトリの状態を確認します。test.txtが削除されていることが分かります。
1
2
3
|
~/sample$ ls
readme.txt
~/sample$
|
–cachedオプション
ファイルを残したままgitの管理対象から削除する、–cachedオプションについて紹介します。ディレクトリが以下の状態とします。
1
2
3
|
~/sample$ ls
readme.txt test2.txt
~/sample$
|
–cachedオプションは、ファイルを残したままgitの管理対象から削除します。
1
2
|
~/sample$ git rm --cached test2.txt
rm 'test2.txt'
|
git statusで状態を確認します。test2.txtがdeletedとUntracked filesに表示されています。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
~/sample$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test2.txt
~/sample$
|
ディレクトリの状態を確認します。test2.txtが削除されていないことが分かります。
1
2
3
|
~/sample$ ls
readme.txt test2.txt
~/sample$
|
-fオプション
gitの管理対象外でも削除する、-fオプションについて紹介します。ディレクトリが以下の状態とします。
1
2
3
|
~/sample$ ls
readme.txt test3.txt
~/sample$
|
git statusで状態を確認します。test3.txtは未コミットの状態です。
1
2
3
4
5
6
7
|
~/sample$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test3.txt
|
git rmコマンドを実行してみます。test3.txtはgitの管理対象になっていないので、エラーになります。
1
2
3
4
|
~/sample$ git rm test3.txt
error: the following file has changes staged in the index:
test3.txt
(use --cached to keep the file, or -f to force removal)
|
-fオプションを付与すると、gitの管理対象外でも削除します。
1
2
|
~/sample$ git rm -f test3.txt
rm 'test3.txt'
|
ディレクトリの状態を確認します。test3.txtが削除されていることが分かります。
1
2
3
|
~/sample$ ls
readme.txt
~/sample$
|
–quietオプション
実行時のメッセージを少なくする、–quietオプションについて紹介します。ディレクトリが以下の状態とします。
1
2
|
~/sample$ ls
readme.txt test4.txt
|
–quietオプションを付与して実行してみます。何も出力されません。
1
2
|
~/sample$ git rm --quiet test4.txt
~/sample$
|
git statusで状態を確認します。deletedになっています。
1
2
3
4
5
6
7
|
~/sample$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test4.txt
|
commit, pushしておきます。
–dry-runオプション
実際には実行せずに内容を表示する、–dry-runオプションについて紹介します。ディレクトリが以下の状態とします。
1
2
|
~/sample$ ls
readme.txt test5.txt
|
–dry-runオプションで実行します。内容が出力されます。
1
2
|
~/sample$ git rm --dry-run test5.txt
rm 'test5.txt'
|
ディレクトリの状態やgitの状態を確認します。test5.txtが削除されていないことが分かります。
1
2
3
4
5
6
7
8
|
~/sample$ ls
readme.txt test5.txt
~/sample$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
~/sample$
|
削除の取り消し
git rmで削除した状態を元に戻す方法について紹介します。ディレクトリが以下の状態とします。
1
2
3
|
~/sample$ ls
readme.txt
~/sample$
|
gitの履歴を確認します。test5.txtの削除をpushした直後とします。
1
2
3
|
~/sample$ git log --oneline -2
2be7240 (HEAD -> master, origin/master) delete test5.txt
fe20d03 add test5.txt
|
以下のコマンドで、直前のpushを取り消します。
1
2
3
4
5
6
7
8
|
~/sample$ git reset --hard HEAD^
HEAD is now at fe20d03 add test5.txt
~/sample$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
|
ディレクトリの状態を確認します。test5.txtが復活していることが分かります。
1
2
3
|
~/sample$ ls
readme.txt test5.txt
~/sample$
|
まとめ
いかがでしたでしょうか。git rmコマンドの使い方について説明しました。
基本的な使い方から、各種オプションの使い方について紹介しました。また、直前のgit rmを取り消す方法についても紹介しました。
ぜひご自身でコマンドを書いて、理解を深めてください。
ネプラス株式会社はサービス開始から10年以上
『エンジニアの生涯価値の向上』をミッションに掲げ、
多くのインフラエンジニア・ネットワークエンジニアの就業を支援してきました。
ネプラス株式会社はこんな会社です
秋葉原オフィスにはネプラス株式会社をはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
インフラ業界に特化
ネットワーク・サーバー・データベース等、ITインフラ業界に特化。Cisco Systemsプレミアパートナーをはじめ各種ベンダーのパートナー企業です。
業界を知り尽くしているからこそ大手の取引先企業、経験豊富なエンジニアに選ばれています。
-
正社員なのにフリーランスのような働き方
正社員の方でも希望を聞いたうえでプロジェクトをアサインさせていただいており、フリーランスのような働き方が可能。帰社日もありません。
プロジェクト終了後もすぐに次の案件をご紹介させていただきますのでご安心ください。
-
大手直取引の高額案件
案件のほとんどが大手SIerやエンドユーザーからの直取引のためエンジニアの皆様へに高く還元できています。
Ciscoをはじめ、Juniper、Azure、Linux、AWS等インフラに特化した常時300件以上の案件があります。
-
スキルアップ支援
不要なコストを削減し、その分エンジニアの方へのスキルアップ支援(ネットワーク機器貸出、合格時の受験費用支給など)や給与で還元しています。
受験費用例)CCNP,CCIE:6-20万円、JNCIS:3-4万円、AWS:1-3万円など
※業務に関連する一定の資格のみ。各種条件がありますので詳しくは担当者へにお尋ねください。
-
現給与を保証します!※
前職の給与保証しており、昨年度は100%の方が給与アップを実現。収入面の不安がある方でも安心して入社していただけます。
※適用にはインフラエンジニアの業務経験1年以上、等一定の条件がございます。
-
インセンティブ制度
ネットワーク機器の販売・レンタル事業等、売上に貢献いただいた方にはインセンティブをお支払いしています。
取引先企業とエンジニア側、双方にメリットがあり大変好評をいただいています。
-
社会保険・福利厚生
社員の方は、社会保険を完備。健康保険は業界内で最も評価の高い「関東ITソフトウェア健康保険組合」です。
さらに様々なサービスをお得に利用できるベネフィットステーションにも加入いただきます。
-
東証プライム上場企業グループ
ネプラスは東証プライム上場「株式会社オープンアップグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
ネプラス株式会社に興味を持った方へ
ネプラス株式会社では、インフラエンジニアを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
オンライン面接も随時受付中。ぜひお気軽にご応募ください。

