Keywords: Git | branch management | uncommitted changes
Abstract: This paper comprehensively examines three core methods for safely migrating uncommitted local modifications from the current branch to another branch in the Git version control system. By analyzing basic git stash operations, differences between git stash pop and apply, and advanced usage of git stash branch, along with code examples and practical scenarios, it helps developers understand the applicability and potential risks of each approach. The article also discusses handling untracked files and resolving potential conflicts, providing practical guidance for optimizing Git workflows.
In the Git version control system, developers frequently encounter scenarios requiring migration of uncommitted local modifications from the current branch to another branch. This need typically arises from incorrect initial branch selection, parallel development requirements, or branch adjustments during code refactoring. This paper systematically introduces three methods to achieve this objective, with in-depth analysis of their working principles, applicable scenarios, and considerations.
Basic Method: git stash Combined with Branch Switching
The most straightforward approach involves using the git stash command to temporarily save modifications from the current branch, then switching to the target branch and restoring these changes. The specific workflow is as follows:
git stash
git checkout branch2
git stash pop
In this workflow, the git stash command saves modifications from the working directory and staging area to a temporary storage area (stash), while restoring the working directory to the state of the most recent commit. Subsequently, git checkout branch2 switches to the target branch, and finally git stash pop restores the previously saved modifications from stash and automatically deletes that stash record.
Precise Control: git stash apply and Stash Management
When finer control over the stash application process is required, git stash apply can be used instead of git stash pop. This method allows developers to first view all stash records, then selectively apply specific ones:
git stash
git checkout branch2
git stash list
git stash apply stash@{0}
git stash list displays all stash records, each with an identifier like stash@{0}. The main difference between git stash apply and git stash pop is that the apply command does not automatically delete the stash record, which is particularly useful when needing to apply the same stash multiple times or preserve stash history.
Advanced Technique: git stash branch Command
Git provides a more elegant solution: the git stash branch command. This command combines three steps—creating stash, creating a new branch, and applying stash—into one atomic operation:
git stash branch branch2
This command works by first creating a new branch named branch2 based on the commit where the stash was originally created, then automatically switching to that branch, and finally applying modifications from the stash to the new branch's working directory and staging area. If successful, the original stash record is automatically deleted. This method is particularly suitable when the original branch has changed significantly, and directly applying stash might cause conflicts.
Handling Untracked Files
By default, git stash only saves modifications to tracked files. If newly created untracked files also need to be saved, the -u option can be used:
git stash -u
This option ensures all modifications, including new files, are correctly saved and migrated. This is especially important for development scenarios involving new resource files or configuration files.
Conflict Resolution and Best Practices
Conflicts may arise when applying stash, particularly when the target branch differs significantly from the original branch. If conflicts occur during git stash pop or git stash apply, Git pauses the application process, allowing developers to manually resolve conflicts. After resolution, git stash drop (for pop) needs to be executed or other operations continued.
Best practices suggest: always consider branching strategy before significant modifications; frequent commits can reduce dependency on stash; use descriptive stash messages (git stash save "description") for easier subsequent management; in team collaboration environments, ensure understanding of stash's local nature to avoid mistakenly assuming stash is shared.