Backup options for a project under version control with Git - python-3.x

I just arrived in a company which does not have access to GitHub or similar.
I was wondering about ways to backup my code using the private OneDrive which is given to each employee.
I just thought of moving my folder from the "document" directory to the "OneDrive" directory. But I was wondering whether changing the path will break my code.
Will it preserve the .git directory? Should I rerun git init (loosing the past history)?

You can move a Git repository from one path to another without affecting it. A Git repo is a self-contained entity (with the exception of global Git config) and the .git folder containing all Git files is just like any other folder (so it can be moved, deleted, etc.).
If you want to use OneDrive as a backup, there are better options than simply copying your project over. Tools like restic or similar would allow you to keep your backup up to date in an efficient way.
Note that you can also set OneDrive as a Git remote. Since it sounds like you are missing options such as GitHub or Bitbucket for your company, this is probably your best option.
To add a remote called onedrive on OneDrive, you can run:
git init --bare ~/OneDrive/<project>.git
git remote add onedrive ~/OneDrive/<project>.git
And then you can use onedrive as you would any other remote. For instance:
git push --set-upstream onedrive master

Related

How can I add a .git directory to a git repository?

I am doing unit tests on a python program who, for QA purposes, gets the repository name and the current commit hash from the .git in the directory
For my unit tests on that program I would like to have a dummy .git directory in the tests directory. That .git repository would have a single initialization commit and a remote that would not be used
When attempting to add a .git to my tool's repository, git seems to ignore it and indicates that there are no differences in the status and commit
How can I add the .git directory to my project repository ? Something like tests/.git
You can't do that. It's inherently forbidden by Git.
You can store a tar or ZIP archive that contains the repository, and then have your test routine extract it to a temporary location. If you go that route, I recommend to use an uncompressed archive format, because it allows Git's own compression algorithms to work more efficient.
I think we would need more details about what you want to achieve to provide like the best answer... but I think you should look at git bundle. You can track a bundle file and then use it to regenerate a git repo.

how to sync a "shadow" git repository with a main repository "secretly"

I have a client that I do work for via a git repository. I do not want for him to know that I am partly using another developer. I have therefore created a "shadow" repository, that my developer is using.
How to sync the "shadow" repository with the main repository so that the main repository does not know that this is happening?
I cannot merge with a git command, since this will reveal what I really have done.
I could copy and paste all files, but that would visible in git that I have deleted all files and replaced with new files with the same name. That does not look good.
My client is using bitbucket. I could give the other developer my bitbucket login, but I would want to avoid to do that.
What actually work, is that I can open a file in the main repository, delete the content and then paste in the updated code. Then I could commit and it would look is if I had done the work directly in the main repository. But this is time-consuming (and time is something I do not have too much of).
Is there a different way that I could "secretly" copy the content of files from the shadow repository to the main repository? Is there a linux/osx-command I could use that replaces the content of a file without deleting and recreating the file (which I then could use recursively)? Is there a git-command that make this possible without leaving a trail of evidence of what I have actually done?
you can have the same set of local files revisioned by different git repositories, using the command line. Just use the GIT_DIR environment variable. The Git repositories are completely independent, and can track files from the same directory.
rem set Git to use the sub-developer's git repository
set GIT_DIR=/path/to/sub/repository.com
rem verify which repository that you are on
git rev-parse --git-dir
rem pull the sub-developer's latest
git pull
rem merge, commit, change, push, change branches, whatever
rem ... when the code is the way you want it
rem switch to client repository
set GIT_DIR=/path/to/client/repository.com
rem bring main repository up to date
git commit -m"changes brought over from sub-developer"
rem push the updated code to the client repository
git push
this is useful for other development practices, like versioning lab data, local developmental utilities, work logs, and other 'misc' files that seem to accumulate with the source code, but shouldn't be archived with it.
After my error on the first answer, I feel compelled to try again.
try reading in the sub developer branch and comparing/merging to main with git differencing. see 'git help config' for 'diff.external'. I use diffuse.exe and a batch file to map the parameters.
The work flow would look something like this.
rem make a remote to the sub
git remote add MYSUB https://path/to/sub/repository.com
rem see what branches are there
git remote show MYSUB
rem get the branch from the subdeveloper
git fetch MYSUB CurBranch:CurBranch
rem view/manually merge just the modified files
git diff --diff-filter=M MYSUB/CurBranch
rem cleanup
git branch -D MYSUB/CurBranch
git rm MYSUB
rem push to main site
git push
This is the method I use to transfer branches to my lab machine, via a USB memory stick. I have not tried it with a remote connection.

Git clone without including top/parent folder

We have a repo in git where the project is contained in a folder called Project. We'd like to be able to release the code to a production server, by cloning the repo, without including the "Project" folder, but with everything below it. Is this possible? The destination directory name is /var/www, which is unrelated to anything in the project. Unfortunately I can't just do a symbolic link because of the nature of our hosting provider (which we'll change soon).
My answer take the assumption that you have a git repository whose content is the following:
/.gitignore
/Project
/Project/index.php
/ProjectB
/ProjectB/pom.xml
If you don't need history at all in that copy of your repository, there is the git archive command which can do what you want except its output its data in tar or zip format:
git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
[-o <file> | --output=<file>] [--worktree-attributes]
[--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
[<path>…]
Like:
git archive --format=zip --remote=git#foobar.git master -- Project | unzip
However, the git clone command does not accept a repository path, and I think it's not really git like to export only a tree view of some branch. You would probably need a submodule making Project an independent git repository, or like the git archive example, get only what you want but without versioning (which can be questionable on a production server).
Instead, you can do that:
Clone your repository to whatever path, say /opt/foobar.
Create a symbolic link of /opt/foobar/Project in /var/www.
Or reference the /opt/foobar/Project in your apache configuration (to avoid the symlink) instead of plain /var/www.

How to conveniently sync a file between two git repositories

I have two git local repositories. Both share an identical file, under a different path and under a different name. Currently, when I make changes I have to copy the file from one directory to another.
Is there an alternative way to keep them in sync without manually overwriting the file? I don't want to create a separate repository for this file. I thought one of the following things would work, but apparently, they don't:
git submodule
git subtree
symlink soft
symlink hard
What else is there?
The only other alternative would be a post-commit hook on repoA, which would, on each commit:
check if the file is part of said commit
copy it in repoB with the right path.

Permissions Issue When Pulling from GitHub

I have cloned a GitHub repository into a new directory on my local machine using:
git clone git#github.com:*****/project.git destination-dir
Clone worked fine but whenever I try to pull or push I git this error back:
error: insufficient permission for adding an object to repository database .git/objects
fatal: git write-tree failed to write a tree
What have I done wrong? And how can I fix this. From what I read so far online, the problem is trivial if you are using your own repository (i.e. not GitHub) but what should I do if this on GitHub (there is no ssh access).
Also, I should add that I have another directory on my machine pointing to same repository on GitHub which is fine for pulls and push.
This doesn't look like related to GitHub per-se.
From what you say in the comments, it seems that the write permissions inside the hidden .git folder (which contains all of your local git repository data) were not corresponding to the permissions that your git client had when you ran it.
It can be that you cloned the repository with one user account (or using sudo), but where trying to pull/push from a different user. If that wasn't the case, something could have messed your local files permissions while cloning.
As you stated, doing chown -R login:group projectDir/ fixed the problem because it recursively reset all permissions in your project folder (including those of the .git folder and its contents).
A good experiment would be trying to clone the same repo to a different folder to see if the problem reoccurs. If it does, maybe something is wrong with your default permission usermask and/or your git client permissions.

Resources