1

I have branches master and devel.

I want to save devel somewhere for the (unlikely) case if I will need to restore the information in it and afterward to reset the head of devel to the head of master (without modifying master).

Is the following the right way to do this?

git checkout devel
git checkout -b devel-save
git push
git checkout devel
git reset --hard master
git push --force
porton
  • 5,214
  • 11
  • 47
  • 95
  • You can also just push the master branch commit to the remote with the “devel” name. Same can be done for saving the specific branch (although I might use a tag). Then update to the remote branch locally to sync as needed, and/or nuke the local if not useful currently. Ensure to communicate a force-update of a mainline branch. – user2864740 Feb 11 '20 at 00:02
  • @user2864740 But is my way right. Can I be sure of no data loss if I do this as I described in the question? – porton Feb 11 '20 at 00:23
  • Git will not "lose data" as long as there is a branch (or tag) that points to a commit - that commit and all ancestors will persist. This is fulfilled for *devel* via `devel-save`. – user2864740 Feb 11 '20 at 02:12
  • I might do it more like this: `git tag devel-save devel; git checkout devel; git reset --hard master; git push --force`. Don't forget to push the `devel-save` tag/commit if wishing it ensure it is available remotely. – user2864740 Feb 11 '20 at 02:15
  • Also, Git keeps around a large history (see the `reflog`), so even an unnamed commit will not be *really* lost until Git does a 'garbage compaction' and that commit is removed. By default even such orphaned commits will hang around for a month. Deleting a git repo, or having such corrupted by other means, is outside the control of git. – user2864740 Feb 11 '20 at 02:18

1 Answers1

1

Can I be sure of no data loss if I do this as I described in the question?

No data will be lost.

You can also directly push develop: git push develop:old-develop, then reset master and force push.
The reflog mentioned in the comment.
On GitHub side, you have a poor man reflog which allows you to get back past deleted history. But in your case, that history will be visible and reference (under a new branch name).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250