1

I have a project tracked in git that has a master branch that becomes the release for my code. I have some code in that branch that is causing problems because an external library that it relies on is currently broken. However, I expect that the external library will be "fixed" at some point in the future (weeks, so stash is not really what I want here) and that I will want to resurrect the problematic code.

So, the question:

How can I save code from a branch for future use? If I simply create a branch called saveme, make changes on the master branch, and then try to merge saveme back into master to get back the "saved" code, this seems to result in "already up-to-date".

seandavi
  • 2,818
  • 4
  • 25
  • 52

1 Answers1

2

When you create the saveme branch, your history looks like this:

 A -- B -- C (saveme,master)

.... then when you work more on master, to cope with the broken library, your history looks like this:

 A -- B -- C (saveme) -- D -- E (master)

So if you try to then merge saveme into master, it says "Already up-to-date" because the complete history of saveme is already contained in master. It sounds as if what you want is to then create a commit which has exactly the same tree as saveme, but is just another commit on top of master. You could do this by using git revert for commit E and then D, but if you want to do it in one go, you can do the following:

# Check that "git status" is clean:
git status

# Set the index (staging area) to be as it was at the tip of the branch saveme:
git read-tree saveme

# Create a commit based on that index:
git commit -m "Going back to the state of the branch 'saveme'"

# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
git reset --hard

(This is a trivial variation of the recipe I suggested in this answer.) Then your history will look like:

 A -- B -- C (saveme) -- D -- E -- F (master)

... but the tree of commit F will be the same as that of commit C.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327