4

I have a branch I want to delete but possibly be able to restore it with full history -is there a way to do this ? So want this:

a - b - c - d - e    master
     \
      f - g    tmp

to become:

a - b - c - d - e

and have a - b - f - g stored somehow that I can reapply it - is it possible ?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • You've already got A-B-F-G on a branch somewhere. If you need that back in, you simply need to merge tmp into master. I'd recommend against deleting it since you think you want to bring it back at some point. – Makoto Jul 09 '14 at 23:04
  • @Makoto: it's just out of curiosity - plus there are reasons for it to be deleted (binary files and the like). Is there a way to achieve what I say ? – Mr_and_Mrs_D Jul 09 '14 at 23:10
  • You *really* should avoid storing binary files in a VCS. – Makoto Jul 09 '14 at 23:15
  • @Makoto: I know it is not my tree - that's why I want to backup and be able to reexamine it – Mr_and_Mrs_D Jul 09 '14 at 23:33

3 Answers3

5

Perhaps you could use git fast-export to save the branch to a file. Then if you need it later you can use git fast-import to get it back.

To accomplish saving the single branch tmp (that is, commits f and g), you could use the triple dot syntax, as follows:

git fast-export master...tmp
Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Aha! - and how would I go exporting a single branch ? – Mr_and_Mrs_D Jul 09 '14 at 23:35
  • See linked documentation from my answer , it has an example of selecting revisions to export. – M.M Jul 09 '14 at 23:54
  • Namelly - should I specify f..g ? Would that be enough ? And >> to a file ? Would then this file be used with fast-import ? – Mr_and_Mrs_D Jul 10 '14 at 00:12
  • I haven't actually used it except for to export a whole repo, but if the doc is accurate then that sounds about right. (where `f` would be excluded, and `g` included). Yes, redirect to file and fast-import from the file. – M.M Jul 10 '14 at 00:13
  • Thanks @thirtythree for handy syntax :) - to think that people actually rejected your edit that only adds value... – Mr_and_Mrs_D Jul 10 '14 at 00:23
  • Sure thing; I didn't think it got rejected though. +1 to @Matt, too. I'm a longtime Git user and this command was news to me... Putting a branch in cryosleep might have its uses! – George Hilliard Jul 10 '14 at 00:25
  • Having problems to reimport - see http://stackoverflow.com/questions/24738037/how-do-i-import-a-range-of-commits-i-git-fast-exported-to-a-file-with-fast-impor – Mr_and_Mrs_D Jul 15 '14 at 12:39
1

Just clone the repository to another location and then delete the branch from the original repository. You will be able to examine it in the cone and if needed pull it back in from there.

A branch in a clone is basically nothing more than a branch that lives somewhere distinct from your current repository. You can always move the branch back to your old repository at a later point.

It's even easier to push back the branch to your original repository from your clone.

So, in your clone just do:

git push origin that_branch 

Ever pushed a branch to github? Yep, that's exactly the same thing.

Christoph
  • 26,519
  • 28
  • 95
  • 133
  • Sure thing. It's an exact clone. Welcome to distributed version control :) – Christoph Jul 09 '14 at 23:54
  • _I know git_ - what I want is export a single branch - clearly @MattMcNabb's answer is more relevant. How would I move the branch back - in one go - from a clone? – Mr_and_Mrs_D Jul 09 '14 at 23:59
  • The repo is huge - I'd rather avoid cloning it and rather have just the blobs I am interested in exported – Mr_and_Mrs_D Jul 10 '14 at 00:10
0

It seems that you've already got what you want in order to preserve the changes of tmp. You should keep the branch around until it no longer makes sense to in order to apply the changes from tmp into master.

That can be accomplished with something as simple as:

git checkout master
git merge tmp

This will apply your changes from tmp onto master.

If you were to back up your git history to some other server or somewhere else, you'd be doing pretty much the same thing; the history graph would still appear the same, and you'd be resurrecting the changes in the same fashion.

Makoto
  • 104,088
  • 27
  • 192
  • 230