Commit select changes to another branch then resume work on current branch?

This happens often in my work flow: I’m working on a feature in a separate branch, and while doing this I’ll come across small things that need fixing but are from higher up in the framework or site layout for example.

I want to switch back to the main develop branch and commit changes from select files there, then go back to a feature branch, and rebase so that I can continue there with the unrelated tweaks/bugfixes out of the way.

  • Determine whether two Git branches have diverged
  • What are the core concepts of git, github, fork & branch. How does git compare to SVN?
  • Practical way to commit changes in source control to multiple branches
  • How do I delete a local git branch when it can't look up commit object in 'refs/heads'?
  • Changing branches in git results in changed files
  • How do I make git-svn use a particular svn branch as the remote repository?
  • I’m using git stash and git stash pop to do this, but I’m getting a lot of conflicts from a bunch of files I modifed but don’t need to be committed to the parent branch anyway.

    Is there another way to avoid conflicts or somehow save the current state, and only pull select working tree changes to another branch for commiting ? (sort of like git-stash-cherry-pick ;-))

  • Recover from incorrect rebase or merge
  • Is it possible to have a git repository as a “vendor branch” in subversion?
  • Eclipse Git self signed certificate error
  • How to merge nested git repo into parent repo, retaining history?
  • Only master branch is visible after cloning a Git repo
  • How to prevent Jenkins SCM polling from triggering a build on branch creation
  • 5 Solutions collect form web for “Commit select changes to another branch then resume work on current branch?”

    1. Commit the changes that you want in master on your current branch.
    2. Stash your remaining changes
    3. Switch to master branch and use git cherry-pick to move the changes onto master
    4. Move back to your feather branch, and rebase (optional)
    5. Unstash the original feature changes

    I usually do it the other way around. I continue working in my feature branch until I’m ready to make a commit there. As soon as I am, I add all changes that belong into the new commit for the branch to the index, but not those that belong into master. git add -p et.al make that really easy. Once all relevant changes in the index, I commit to the branch. All remaining remaining dirty changes belong to master and will be carried along just fine once I switch to that so I can commit it there.

    Try switching back to master branch using --merge option. It will try to do a three way merge between the two branches. The git documentation has a good example:

    2. After working in the wrong branch, switching to the correct
       branch would be done using:
    
          $ git checkout mytopic
    
       However, your "wrong" branch and correct "mytopic" branch
       may differ in files that you have modified locally, in which 
       case the above checkout would fail like this:
    
          $ git checkout mytopic
          error: You have local changes to 'frotz'; not switching branches.
    
       You can give the -m flag to the command, which would try a
       three-way merge:
    
          $ git checkout -m mytopic
          Auto-merging frotz
    
       After this three-way merge, the local modifications are not
       registered in your index file, so git diff would show you what
       changes you made since the tip of the new branch.
    

    On MacOS, GitX makes it very easy to do the kind of selective committing rafl describes, so that’s a good way to approach it if that’s the environment you’re in.

    It’s also possible/practical to commit branch-y changes and master-y changes in separate commits, then use git format-patch to export the commits from the branch as files and git am to pull them in to the master.

    The danger here is if the files around the changes are too different, in which case there may be conflicts when pulling the commits in to the master.

    What about creating a temporary branch?

    Something like:

    - oh crap need to do somethning else now
    - git checkout -b ResumeLater
    - git add .
    - git commit
    - git checkout ImportantStuff
    - ....
    - git checkout ResumeLater
    
    Git Baby is a git and github fan, let's start git clone.