Sobes.tech
Junior — Middle

Explain the difference between the stash and patch commands in the Git version control system.

sobes.tech AI

Answer from AI

The git stash command is used to temporarily save current changes in the working directory and index, so you can switch to another branch or perform other actions without committing. It's like "postponing" unfinished changes.

The git patch command does not exist directly, but "patch" usually refers to a patch file — a text file with differences between versions, which can be created using git diff or git format-patch and applied with git apply or git am.

In summary:

  • git stash temporarily saves changes locally within the repository.
  • A patch is a file with changes that can be passed to another developer or applied later.

Example of creating and applying a patch:

# Create a patch with changes between branches
git diff master feature_branch > changes.patch

# Apply the patch
git apply changes.patch