Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sandbox feature in Git, which let's you save the entire state of your repo to play around safely?

Tags:

git

So, you are at a point where you know you need to do some cleanup in your git repo, potentially using rebase or other stuff that could make it really messy to undo if you do it wrong.

Is there a way to tell Git "save this state of the entire local git repo, so that I can rollback everything to this point, in case things go wrong?" Something like a "sandbox mode", which lets you play around with your repo safely, performing potentialy disasterous operations, without the risk of messing anything up.

I'm not talking about adding a tag or something to a specific point in history, but about something that would reset all the changes made in the git repo on all branches from that particular state in time. The potential to roll back everything (all the git history/state). Something similar to the OSX Time Machine.

Update 2019-02-19: I should clarify that by "potentially disasterous operations", I don't mean to the code itself, but actually messing around with the repo structure in git, using for instance git reset --hard or similar commands.

like image 357
Magne Avatar asked Jan 04 '17 09:01

Magne


People also ask

What is a sandbox in git?

Sandbox Git Repository Git is a version control system that we use to track changes to our source code and documentation. You can find a sandbox Git repository where you can practice the Git commands before you put together a patch to resolve a bug or implement a new functionality.

What is sandbox repository?

A sandbox is a local copy of a project's files, used to make changes and test work. Each developer should work from her own sandbox on a computer that is convenient to her. The CVS repository contains the master copies of a project's files and their full histories.

How do you commit in the sandbox?

Committing to organizationsClick on the CodeSandbox application and you will be taken to the settings. In here you can check what organizations CodeSandbox has access to and grant it access to the organization you want to deploy to. After this you will be able to commit from CodeSandbox to that organization.


3 Answers

The Git repository itself is a sandbox.

Clone your repository and play in the cloned repo. You can do anything in it, you can even completely destroy it (remove it). As long as you don't git push, the original remains untouched.

If you want to start over just remove the clone and re-create it again. Or run:

git checkout master
git reset --hard origin/master

The checkout command makes master the current branch on the local repo. The reset command restores the local current branch and the working tree to the state of branch master on the origin remote (assuming you started playing on the master branch).

like image 60
axiac Avatar answered Oct 12 '22 10:10

axiac


You can just copy your folder with Git repository somewhere else and do anything you want there (assuming only local operations). This will not affect your original local repo and if you want to discard changes in your copy - just remove it and copy again.

like image 4
Oleksandr Kobylianskyi Avatar answered Oct 12 '22 09:10

Oleksandr Kobylianskyi


Git tries very hard to not lose or corrupt data and already provides tools to help you recover work. Its basic build blocks are persistent immutable data structures.

For example, instead of rebasing branch foo directly, create a new branch named foo/rebase where foo currently is, check it out and rebase foo/rebase instead. You will see that none of the commits reachable from foo will be modified, so you are safe to work on the new branch. Just like a transaction, you can rollback the work (simply remove foo/rebase) or commit that operation (make foo points to foo/rebase with reset --hard, or maybe merge).

In any case, you still have the reflog which keeps references to objects not currently reachable by any branch or tag. You could also clone your repository, but this is a bit overkill in most situations. In your case it seems that you are going to change branches in mass, so I must admit that cloning the repository might be the best approach.

like image 2
coredump Avatar answered Oct 12 '22 09:10

coredump