LinuxCommandLibrary

git-update-index

Manage files in the Git index

TLDR

Pretend that a modified file is unchanged (git status will not show this as changed)

$ git update-index --skip-worktree [path/to/modified_file]
copy

SYNOPSIS

git update-index [options] [--] path...

PARAMETERS

--add
    Add specified file(s) to the index. This option is required when adding new paths that are not already in the index.

--remove
    Remove specified file(s) from the index. This option is required when removing paths.

--replace
    Combines the functionality of --add and --remove. It allows replacing existing entries in the index with new ones from the command line.

--refresh
    Checks the working tree against the index. Updates index entries with current metadata (e.g., permissions, size) from the worktree. It also identifies entries that are unmerged or missing from the worktree.

--really-refresh
    Similar to --refresh, but also checks the object contents (SHA-1) in addition to the metadata, which can be slower.

--chmod=(+|-)x
    Sets (+x) or unsets (-x) the executable bit for the specified path(s) in the index. This only affects the index entry, not the working directory file.

--skip-worktree
    Sets the 'skip-worktree' bit for the specified path(s). Git will assume the worktree file is unchanged and ignore its modifications, useful for local configuration files.

--no-skip-worktree
    Unsets the 'skip-worktree' bit, allowing Git to track worktree changes for the specified path(s) again.

--assume-unchanged
    Sets the 'assume-unchanged' bit. Git will assume the file's contents have not changed, primarily for performance in large repositories. Modifying the file in the worktree while this bit is set can lead to issues.

--no-assume-unchanged
    Unsets the 'assume-unchanged' bit, forcing Git to check the file's contents again.

--cacheinfo <mode>,<object>,<path>
    Adds a specific entry to the index directly, without checking the working tree. Requires a file mode (e.g., 100644), an object hash (SHA-1), and the path.

--stdin
    Reads paths from standard input, one per line, instead of from command line arguments.

DESCRIPTION

The git update-index command is a low-level Git utility used to directly manipulate the Git index (also known as the staging area). It allows you to register the current content of specific files in the index, stage changes for the next commit, or modify file metadata like permissions.

While commands like git add are typically used for staging changes, git update-index provides finer control over what gets stored in the index. It's often used by scripts or advanced users who need to bypass some of the checks or functionalities of higher-level commands. For example, it can be used to add the contents of a file without the file being present in the working directory, or to change the executable bit of a file in the index without affecting the working copy.

CAVEATS

git update-index is a low-level command. For most common use cases, git add is the preferred and safer option as it handles many details automatically. Incorrect use of git update-index can lead to inconsistencies in your Git repository's index, potentially causing unexpected behavior in future commits or merges. Use it with caution and only when you fully understand its implications.

THE GIT INDEX

The Git index, often called the 'staging area', is a crucial component that acts as a temporary buffer between your working directory and your Git repository. It holds a snapshot of what your next commit will look like. git update-index is the command that allows Git to populate and manage this snapshot.

LOW-LEVEL USAGE

Unlike user-friendly commands like git add which infer actions (add, modify, delete) based on file status, git update-index requires explicit instructions (e.g., --add, --remove). This makes it powerful for scripting but more error-prone for manual use. It's often used when you need to stage changes that are not present in the working directory, or when managing specific file flags.

SKIP-WORKTREE VS. ASSUME-UNCHANGED

These two bits, manipulated via --skip-worktree and --assume-unchanged, control how Git handles files. Skip-worktree is typically used for user intent, marking files that should not be automatically updated or checked by Git (e.g., local configuration files). Assume-unchanged is a performance hint for Git, telling it to assume a file hasn't changed unless its mtime or ctime suggests otherwise, reducing the need for costly stat calls in large repositories. Modifying a file with assume-unchanged set can lead to unexpected behavior.

HISTORY

git update-index is one of the foundational commands in Git, existing since the very early days of its development. It directly reflects Git's core design philosophy of maintaining a 'staging area' (the index) as an intermediate step between the working directory and the repository. While high-level commands like git add were later introduced to simplify user interaction, git update-index remains the underlying mechanism for interacting with the index programmatically and for advanced staging operations.

SEE ALSO

git add(1), git rm(1), git commit(1), git status(1), git ls-files(1), git write-tree(1)

Copied to clipboard