git

git add: stage/unstageの便利コマンド紹介

How to use git command 7/N

公開日: 2021-01-06
更新日: 2024-04-23

  Table of Contents

How to stage an updated git file

  • updated済みファイルのみをステージングへ上げたい(= untrackedファイルはそのまま)

Solution 1: delete, modified両方を対象

1
% git add -u

オプション -u, --updateの効果

  • delete, renameを含む修正済みファイルをステージングエリアへあげる
  • path を指定するとそのフォルダ以下のファイルに対して動作する
  • path を指定しないときは, working tree全体のtracked filesについて動作する

Solution 2: delete, modifiedいずれかのみを対象

1
2
% git ls-files --modified | xargs git add -i 
% git ls-files --deleted | xargs git add -i 
  • modified, deletedされたファイルのみをステージングへ上げたいときに有効
  • 基本はgit add -uで足りる

How to unstage a git file

  • ステージングエリアへ上がってしまったファイルを取り消したい
  • gitの追跡対象にはしておきたい

Solution: Unstage a file using git restore

1
% git restore --staged <file name>
  • git resetを用いる方法もありますが, resetは歴史を修正してしまう強力なコマンドなのでrestoreを推奨
  • 自分は以下のように, aliasを.gitconfigに設定して使用している
1
2
[alias]
	unstage = "restore --staged"

WARNING!: Unstage a file and remove it from the git index

1
% git rm --cached <file name>
  • 効果として, ステージングエリアやindexからファイルが削除されるだけでなく, リポジトリから完全に削除される
  • 機密情報を含んだuntracked fileにたいしてcommit直前に, ステージングエリアから除去したいときにしかこの方法は取らないと思う

References



Share Buttons
Share on:

Feature Tags
Leave a Comment
(注意:GitHub Accountが必要となります)