git – commit modified files from one branch to another branch

Scenario:
I have 2 branches: dev and sprint1

  1. I made some changes while checked out to branch sprint1;
  2. I don’t want to add/commit these changes in sprint1, I want to add/commit them on dev;
  3. I tried to checkout dev without committing changes in sprint1 first, but the checkout fails;

My question is: How can I bring those changes in sprint1 to dev without committing them to sprint1 ;

  • git push sporadically fails on Jenkins with “Device not configured” error
  • How to change the license for a project at Github?
  • Does “git stash apply” or “git stash pop” also adds the modified files for commit?
  • why is vc-git and magit in emacs ignoring my git config settings?
  • IDEA: How to import module from git?
  • Git adding multiple upstream servers and tracking their respective branches
  • In git, how can I merge features that were implemented in branch 'A' into branch 'B'?
  • How to delete everything inside GIT repository?
  • Re-doing a reverted merge in Git
  • Recreate git repository from .git folder from stash
  • How do I append one git history to another?
  • Can git be configured to not perform autocrlf on certain files?
  • 2 Solutions collect form web for “git – commit modified files from one branch to another branch”

    Stashing

    git stash
    git checkout dev
    git stash apply
    git commit
    

    You can merge your local modifications into the branch you want to checkout.

    On your sprint1 branch do:

    git checkout --merge dev
    

    This will merge the local modifications of the working directory into the dev branch and switch to it. The changes are just merged and not committed. So you can now go on as usual.

    git add ....
    git commit
    

    From the git checkout documentation

    –merge

    When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

    Git Baby is a git and github fan, let's start git clone.