1

I've got several separate, unrelated changes that have been made to a code base since the last commit. Ideally each change should have been made into its own branch, but this did not happen. Now, I would like to move each separate change into a separate branch so that I can release one of them and merge the remaining changes into a second release branch at some future time.

I see a similar question which has an answer involving cherry picking. However, none of the changes have been committed yet, and I would like to do this in the simplest, most straight forward way possible.

At worst, I could probably make a note of each of the changes separately, record these separately, then discard all changes, create the separate branches, and make each change in a separate branch. Is there an easier way to do this that involves some git commands?

Michael
  • 9,060
  • 14
  • 61
  • 123
  • You can find answer here : [some old question which may give You an answer](https://stackoverflow.com/questions/7239333/how-do-i-commit-only-some-files) – Wiciaq123 Feb 06 '18 at 21:03
  • I don't really like using `git stash`. What I would do here is create a new branch name such as `combined` and commit. You now have a commit you can cherry-pick. Go back to the branch(es) you want to work on, run `git cherry-pick -n combined` to obtain the committed change without commiting, then `git reset ` to de-stage the uncomitted change too; then use `git add -p` to stage just part of the change, and commit that part (and then `git reset --hard` to toss the remaining part). Repeat for each additional branch. – torek Feb 06 '18 at 21:57

1 Answers1

1

You can use git stash. Source -

Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.

What you can is stash, go on your branch and stash the changes you want , then for every branch you want those changes on, simply use git stash apply. If you use pop instead it removes the state from the stash.

To list the entries or the part you want to keep etc, use

  1. git stash list - List the stash entries that you currently have. Each stash entry is listed with its name (e.g. stash@{0} is the latest entry, stash@{1} is the one before, etc.)
  2. git stash drop - Remove a single stash entry from the list of stash entries. When no is given, it removes the latest one. i.e. stash@{0}

With those commands coupled you can definitely manipulate the changes you want at a finer level. You can use the man pages or the official documentation for specific commands options. Also,Recovering stash entries that were cleared/dropped erroneously.

  • If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms

A stash entry is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the entry was created. The tree of the second parent records the state of the index when the entry is made, and it is made a child of the HEAD commit. The ancestry graph looks like this:

   .----W
  /    /
-----H----I

where H is the HEAD commit, I is a commit that records the state of the index, and W is a commit that records the state of the working tree.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • ok, so i can stash all the changes since the last commit, and then apply the changes to other branches. i'm looking at `git stash apply` but it's not clear to me how i would pick what parts of the changes to apply. – Michael Feb 06 '18 at 20:57
  • define parts to pick? – Remario Feb 06 '18 at 20:58
  • right... i want to actually create three separate branches, each to hold a separate fix / feature made change to the code base since the last commit. – Michael Feb 06 '18 at 21:00
  • re-reading the last paragraph, are you saying to apply the whole of the changes to each branch, then manually undo the changes I didn't want in that branch? – Michael Feb 06 '18 at 21:02
  • yes what i normally do is create a test branch apply the changes there, stash it and apply to other branches then delete test branch. In your case apply it to the three branches and manually resolve and fix the issues. Make sure the last apply is a pop. – Remario Feb 06 '18 at 21:06
  • Do some experimentation first, on a dummy git repo – Remario Feb 06 '18 at 21:07
  • Ok, I've got it working, but it's rather tedious because one of the changes is rather complicated and I basically have to undo it for the other two branches, whereas the other two are fairly simple. is there some way to select which hunks in the stash I want to apply instead of the whole thing? – Michael Feb 06 '18 at 21:23
  • Yes,the drop option. Reread the answer use list option to list your changes and drop to apply hunk – Remario Feb 06 '18 at 22:32