Git: copy version to play

When developing the application, I would like to have at some point a separate copy of my work for experimenting with a few changes. These changes are not intended to be made! He just plays to try different possibilities.

What is the β€œbest” way to do this? I found the following options, but would like to get advice from people who have more experience with git than me:

  • I could use git clone to get a copy of the repository in another directory and use it to play.
  • I could just cp -r set the current git working directory to a new one.

In case (1), git knows about the clone, and I could rebase later if I want, but it's unlikely that I will ever do it.

In this case, is there something I should know about that makes solution (1) or (2) preferable? Or is there an even better way to do this?

+5
source share
3 answers

TL DR

Using clone / copy

  • Pros: The main repo and its clone / copy are silos.
  • cons: space inefficiency

Using a branch

  • Pros: Space Efficiency (Git branches are cheap)
  • cons: relatively cumbersome workflow if you need a lot of clutches

More details

You write

I would like to have at some point a separate copy of my work for experimenting with a few changes. [...] What is the β€œbest” way to do this?

You also write in your comment that you are likely to switch between the original work and its β€œcopy” without making any changes made in the last.

Here you have different possible approaches; first consider your two sentences, and then explore the third possibility. To fix the ideas, I assume that your repository lives in a directory called main .

1 - use git clone to get a copy of the repository in another directory and use it for playback.

Do you think you could create a clone of main , say, in a directory called exp ,

 git clone <path-to-main> <path-to-exp> 

and do your crazy experiment in exp . The repo main would be exp "upstream", i.e. exp would display main as one of its remotes called origin . Therefore, if you made more commits in main and wanted to update exp , what happens in main , you can get from main to exp , and then merge or reinstall the latter.

The main problem with this approach is space inefficiency : since the clone carries the entire history of the original repo with it, there will be many duplicate files on your disk. This may or may not be a problem, depending on the size of your source repo, but this is something to consider.

2 - Just cp -r current current git working directory for the new

This is essentially like cloning main , but unless you manually add main as the remote exp device, you cannot get from main to exp . I see no advantage in this approach compared to the first (except, perhaps, that it does not imply the risk of accidentally clicking on things from exp to main ).

3 - Create a new branch directly in the source repo and experiment in it

The third approach is to create a new branch (call it exp ) directly into the main repository,

 git branch exp 

verify,

 git checkout exp 

and do an experiment there. The main advantage of this approach compared to the other two is space efficiency: git branches are cheap because creating a new one is not related to copying files.

However, if you make changes and exp unloaded, but don’t commit them, git will consider your working directory β€œnot clean” and will not allow you to return to the master branch until you correct the situation. One way to clean up your repository is to hide your uncommitted changes by running

 # while on exp, if there are uncommitted changes git stash save 

Then you can go back to master , make some changes, commit them, go back to exp and get your hidden changes by running

 git stash pop 

If a lot of ticks are involved ( save and pop ), you may find that this workflow is too tedious and cumbersome ...

+3
source

The best way is to create your own branch from the current state, to do your work in this branch (with commit, if you want). In the end, if you want your work with the main branch, you can run git merge .

To create a branch from the current state, simply run git checkout -b my_branch

+4
source

The simplest solution would be to create a git checkout -b your-branch-name branch git checkout -b your-branch-name and then delete your branch using git branch -d your-branch-name when you are done, or commit and merge to the original

You can also stay in one project, do your experiments, and then use git stash to clean up all your work, but it's not very clean.

+2
source

Source: https://habr.com/ru/post/1201469/


All Articles