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.
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 ;-))
5 Solutions collect form web for “Commit select changes to another branch then resume work on current branch?”
- Commit the changes that you want in
masteron your current branch.
- Stash your remaining changes
- Switch to
masterbranch and use
git cherry-pickto move the changes onto
- Move back to your feather branch, and
- 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?
- oh crap need to do somethning else now - git checkout -b ResumeLater - git add . - git commit - git checkout ImportantStuff - .... - git checkout ResumeLater