0

When I'm working on a project, sometimes my machine requires specific changes to a config file or two, or many. Currently, I have these changes stored in a local branch: local-settings.

Then, when I need to do a bug fix and test, I do a pull from remote and get latest. I create my new-branch and then I merge local-settings into new-branch and do a git reset HEAD~1 to "undo" the merge commit. From there I only stage and commit my bug fix files.

Is there any damage with doing things this way, and is there a better way of persisting local changes that I need long-term? I've seen other questions mention git stash, but that wipes out the stashed changes once they are popped, correct? That doesn't seem like a good solution for me.

Thanks!

Mike
  • 73
  • 5
  • 2
    IMO this kind of local history manipulation is normal and expected in git, though stash is easier for simple use cases (harder to make a mistake like reset something you didn't mean to). It's possible to pop a stash without deleting it: `git apply`. – Roman Starkov Dec 11 '18 at 16:12
  • 1
    Possible duplicate of [Can I 'git commit' a file and ignore its content changes?](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) – Sander Dec 11 '18 at 16:18

1 Answers1

0

I think you can do it the opposite way : instead of always merging your local-settings (which pollute your history) into new-branch, just rebase your local-settings and develop bugfix on it (assuming local-settings derives from new branch. Otherwise, cherry-pick could help). Then, when completed, stash your work, switch to new-branch and stash pop. After that, commit the bugfix into new branch.

That way you always develop on your local-settings branch, but commit changes in new-branch.

EDIT

If your local-settings branch have more than a single commit, you can use rebase --onto :

Let's say you've named your branches like so:

A -- B -- C -- D (new-branch)
 \-- E -- G -- H -- I -- J (current-parent)
      \-- K -- L (local-branch)

What you want to do is rebase my-branch onto the D commit like so:

git rebase current-parent local-branch --onto D
Nicolas Voron
  • 2,916
  • 1
  • 21
  • 35
  • This wouldn't work because `local-settings` is branched way before `new-branch` and typically only contains a few changes to a file or two. `cherry-pick` could work if I only keep one commit in `local-settings` though, but sometimes I have multiple commits there. Thanks for the suggestions! – Mike Dec 12 '18 at 17:05
  • Updated answer. – Nicolas Voron Dec 13 '18 at 08:15