Version control of individual files using GitHub Gist

image

It often happens that a developer with-time accumulates some collection of code that he uses in his projects.

He uses some scripts in some projects, others in others.

These scripts are improved over time, bugs are removed, optimized. Therefore, the question arises how to synchronize new versions of scripts with those that are in projects .

There are several options:

First option:

Create one repository and put all the scripts there. Then this repository is connected as a sub-module to the project and used.

Minuses:

  1. All scripts including unnecessary ones are copied to the project.
  2. the submodule is not commit to the project repository, so if the remote repository of the submodule is unavailable, then we will not be able to deflate the entire project.

Second option:

Store each script separately on Github gist and connect the necessary as submodules
Minus the same as in the first option in the second paragraph.

The third option:

Use Git Subtree.

(This solution is an alternative to Git submodules)

Git subtree is another method of merging branches. His idea is that having two branches, git will understand that one branch is not a variation of the other, but an addition.

The general essence of the idea:

  1. - add the file to Github gist (a mini-repository is generated)
  2. - we bind a mini-repository to our project as a separate branch
  3. - assign folder for this branch
  4. - pumped out.
  5. - further we work as with a normal branch (merge, commit, fetch ...)

Now for details using Git-extensions.

1) We publish our code file at https://gist.github.com where we can immediately get a link to the “mini” repository:

image

Open the repository of our project in GitExtensions and select:

[Repository] -> [Remote repositories ...]

image

We connect as a separate branch.

To do this, press [+] . Enter [Name] , [Url] and save [Save changes] :

$git remote add"Util1""https://gist.github.com/cf056e792d3bd9c2fc5973b846efe3d3.git"

image

We see that connected to a remote repository.

Next, we need to associate this thread with a specific folder in our project so that the file is copied there.

To do this, open Git-bash (ctrl + G) and execute the command:

$git read-tree --prefix=Client/Assets/ -u Util1/master

where:

Client / Assets / is the path to the folder to which the
Util1 / master file will be copied — the name of the remote repository branch
(via UI did not find the way) The

gist branch becomes attached to our folder in the project. And the file is already there.

Now we can work as with a normal branch.

For example, if the file in the Gist changes, we can get a new version:

Make Fetch All and see all the changes:

$git fetch --progress "--all"

Then we merge to upload changes to our thread:

$git merge -s subtree --no-ff --allow-unrelated-histories Util1/master

image

image

Result:

image

Additional information:

https://git-scm.com/book/ru/v1/Instruments-Git-Deliting-
subtrees https://www.atlassian.com/blog/git/alternatives-to-git-submodule- git-subtree
https://www.nwcadence.com/blog/git-subtrees

Also popular now: