We have a git repository with N folders.
Repo
|-Folder1
|-Folder2
|- ...
|-FolderN
With different collaborators we would like to share different folders. Each collaborator should have access only to his allowed subset of folders. What is the "good" way to achieve this using git?
An answer was to use git submodules
. But after I read this article:
https://codingkilledthecat.wordpress.com/2012/04/28/why-your-company-shouldnt-use-git-submodules/
I understood that you need to have a good mastery of git (which is not the case of our collaborators) in order to not have problems when using git submodules
.
I read about some possible alternatives like gitslave
, and git subtree
. gitslave
seemed to be a good solution but still a complex solution in my opinion.
Here is my simple solution and I would like to know if it can have some very bad drawbacks :
-Having a simple repository for each Folder and a repository for Repo. Then add all the files in Folder1, ..., FolderN in the main Repo.
-globalpush script:
function globalpush(){
REPOS="$HOME/Repo/
$HOME/Repo/Folder1
$HOME/Repo/Folder2
$HOME/Repo/Folder3
# ...
$HOME/Repo/FolderN"
#do not show untracked files
git config status.showuntrackedfiles no
read -p "Commit description: " description
for repo in ${REPOS}
do
# if the repo folder exists
if [ -d $repo ]
then
# Go inside the repo
cd $repo
echo "-----PUSHING REPO : "$repo"-----"
#add all modified all deleted TRACKED files
git add -u .
git commit --allow-empty -m "$description"
git push
else
echo "-----COULD NOT FIND : "$repo"-----"
fi
done
#show untracked files again
git config status.showuntrackedfiles normal
}
-globalpull script:
function globalpull(){
REPOS="$HOME/Repo/
$HOME/Repo/Folder1
$HOME/Repo/Folder2
$HOME/Repo/Folder3
# ...
$HOME/Repo/FolderN"
for repo in ${REPOS}
do
# if the repo folder exists
if [ -d $repo ]
then
# Go inside the repo
cd $repo
# pull the modifs.
echo "-----PULLING REPO : "$repo"-----"
git pull
else
echo "-----COULD NOT FIND : "$repo"-----"
fi
done
}
The advantages of this solution are:
1 - Simple solution that everyone can understand.
2 - Possibility to give the access rights for each Folder independently.
3 - For the main developers (who have access to Repo) the repository Repo is self-contained, and contains all the history (in case something goes wrong with the repositories of Folder1, ..., FolderN).
4 - when a main developer makes a commit with a given description, a commit with the same description will be created for all the Folders repositories, even those without a modification (--allow-empty), which of course isn't perfect but helps tracking versions that are submitted by main developers.
EDIT :
There seems to be a new command I was not aware of git subrepo
...
Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.
Gitslave is a value added supplement designed to accelerate performing identical git actions over all linked repositories and aside from one new file in the superproject, adjustments to . gitignore, and perhaps a few private config variables, does not otherwise affect your repositories.
Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular snapshot in time. Git submodules enable a Git repository to incorporate and track version history of external code.
In most cases, Git submodules are used when your project becomes more complex, and while your project depends on the main Git repository, you might want to keep their change history separate. Using the above as an example, the Room repository depends on the House repository, but they operate separately.
git subtree lets you nest one repository inside another as a sub-directory. It is one of several ways Git projects can manage project dependencies. Management of a simple workflow is easy.
A Git subtree isn't a direct alternative to a Git submodule. There are certain caveats that guide where each can be used. If there is an external repository you own and are likely to push code back to, use Git submodule since it is easier to push.
There is another way to do this which is a little simpler, however. If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.
You don’t clone or integrate any of the actual code in your new repository when you use many submodules, it’s better to say that you include links to the forest repository on GitHub. These pointers lead to a submodule commit in a different repository. Git submodules enable you to preserve one git repository as a sub directory of another.
git-subtree was the winning solution and in fact it does what I was doing with my scripts and much more, in a better "git-native" way.
Here is a link to the tutorial that we've followed to setup our environment.
https://hpc.uni.lu/blog/2014/understanding-git-subtree/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With