You have a git project, based on main. With mixed changes in your local copy of the project for two different features none of the changes are pushed to the origin git server. You want to create two branches and check the two branches to the origin git server, separating the two sets of changes.
This guide helps you separate mixed local changes into two different branches and push them individually to the origin Git server.
-
Stash Your Changes Temporarily
Use
git stash
to save your current mixed changes without committing them:git stash
This command saves your changes and resets your working directory to match the
main
branch. -
Create and Check Out the First Feature Branch
Create a new branch for the first feature and switch to it:
git checkout -b feature-branch-1
-
Apply the Stash and Stage Changes for the First Feature
-
Apply the stash without removing it from the stash list (to reuse it for the second branch):
git stash apply
-
Stage only the files or changes related to the first feature:
git add path/to/file1 path/to/file2
Or, stage changes interactively by selecting specific lines:
git add -p
-
-
Commit the First Feature
Commit the staged changes:
git commit -m "Add feature 1 changes"
-
Push the First Feature Branch to the Origin
Push the first feature branch to the origin Git server:
git push -u origin feature-branch-1
-
Switch Back to Main and Create the Second Feature Branch
-
Discard the unstaged changes and reapply the stash for the second feature:
git restore --staged . git stash apply
-
Create and switch to the second feature branch:
git checkout -b feature-branch-2
-
-
Stage, Commit, and Push the Second Feature
-
Stage and commit the remaining changes for the second feature:
git add . git commit -m "Add feature 2 changes"
-
Push the second feature branch to the origin:
git push -u origin feature-branch-2
-
This approach results in two separate branches, each containing the changes for a distinct feature, both pushed to the origin repository.