Does GitHub use bare repositories under the hood? - github

Recently, I discovered that git could be initialized with a --bare flag (e.g. git init --bare). It creates a local repository without a working tree (workspace). As far as I understand, such repositories can be used as a remote from which you can clone, pull and push.
Do GitHub and similar services use "bare repositories" under the hood?
I'm wondering because whenever I create a local repo and push it to a remote service (like GitHub, GitLab, Bitbucket, etc.), I don't create the bare repo myself and I'm not sure if it was created at all (implicitly by GitHub). If it wasn't created, then what is tracking the files/history?
tl;dr
Locally, I can do something like this
-- Step 1. Create a local bare repository
$ mkdir bare-repo/
$ cd bare-repo/
$ git init --bare
-- Step 2. Clone a non-bare repo and push changes
$ mkdir non-bare-repo-1
$ cd non-bare-repo-1
$ git clone ../bare-repo .
$ touch file.txt
$ git add .
$ git commit -m "Add file.txt"
$ git push (it pushes changes to the local remote a.k.a. non-bare/)
And I can create another folder and another and all of them will refer to a single (local) bare-repo/ which tracks changes of all cloned repositories.
I wonder if GitHub uses this approach under the hood, or if the bare repositories are used for something else...

Answering my own question
Do GitHub and similar services use "bare repositories" under the hood?
Yes
It looks like bare git repositories were designed to be used as remote instances and GitHub has to use them to get it to work.
P.S.
Related answer by #VonC
It seems that GitHub repository can work as working repository and bare repository at the same time, does that mean GitHub repository is developed from git bare repository
No, Git repos on GitHub are bare, like any remote repo to which you want to push.
Related answer by #duncan
Short answer
A bare repository is a git repository without a working copy, therefore the content of .git is top-level for that directory.
Use a non-bare repository to work locally and a bare repository as a central server/hub to share your changes with other people. For example, when you create a repository on github.com, it is created as a bare repository.
So, in your computer:
git init
touch README
git add README
git commit -m "initial commit"
on the server:
cd /srv/git/project
git init --bare
Then on the client, you push:
git push username#server:/srv/git/project master
...

I believe so. You can in fact create a GitHub repo and deselect all the auto created files (e.g. README.md, LICENSE). You're provided a list of instructions to init, commit and push the repo.

Related

Make the current commit the only (initial) commit in a Git repository that was created with GitHub Desktop

I created my first GitHub repository using GitHub Desktop (Windows). It is a real mess with many revisions that are quite meaningless and some versions of files that I would rather were never uploaded. This was the result of a lot of experimenting to get the feel for how things would appear on GitHub. I want to get rid of all the history versions.
I am tempted to just copy my files on my drive to another folder then delete the repository folder from my drive. Also delete it from GitHub.
Then create a new repository with GitHub Desktop, perhaps with the same name or with a different name then rename it to the original. Could it be a simple as that or will GitHub still retain the files somewhere?
I haven't tried this because in my searching I keep finding all the complex steps to be performed to remove histories or remove files.
I sort of feel that what I am proposing is too simple.
Any opinions?
All of this got too confusing.
I just did what I said in the start of the thread.
It seems GitHub Desktop has some Username/Password problem and won't let me "Publish branch".
So I went to GitHub and created a new repository and uploaded all the files from my local folder.
It looks good to me.
There may be problems in the future. I guess I'll cross that bridge when (if) I come to it.
An alternative approach is to switch to command line and:
delete the .git folder in your repository
recreate it (git init .)
reset the origin remote: git remote add origin https://github.com//
Make a first commit with your current content:
git add .
git commit -m "first commit"
overwrite everything on the remote repo
git push --force -u origin master
The end result will be the same repo but with only one commit.
You can then switch back to GitHub Desktop.
From here.
First make sure you have Git for Windows installed, you are going to need to do git commands manually sooner or later.
Go to your local repository on your computer where your project is located. It's a good idea to show hidden files so you can see that you have the .git-folder and that the .gitignore-file is in place.
Go to the folder where the .git-folder is, right-click and click git bash here.
Now enter these commands:
Create Orphan Branch – Create a new orphan branch in git repository. The newly created branch will not show in ‘git branch’ command.
git checkout --orphan temp_branch
Add Files to Branch – Now add all files to newly created branch and commit them using following commands. Don't forget .gitignore!
git add .
git commit -m "the first commit"
Delete master Branch – Now you can delete the master branch from your git repository.
git branch -D master
Rename Current Branch – After deleting the master branch, let’s rename newly created branch name to master.
git branch -m master
Push Changes – You have completed the changes to your local git repository. Finally, push your changes to the remote (Github) repository forcefully.
git push -f origin master
Git overview

Connect to github server

I have the ip, useename and password of the github server, which uses gitlab, and I need to connect to it and manage files. What should I do?
I'm assuming you're asking how to manage/change files within a git repository. If you're asking something else, I've missed the point, apologies.
Firstly you will need 'git' on your local machine - install it with your package manager or download and install it from the git website.
Once git is installed you can clone a repository (which contains a set of files, and ends in .git) using the git clone command. Run git clone --help for more information on this, but if you already know the address of your git server and the path to the repository, it will look something like as follows:
git clone https://git-server.url/path/to/repo.git
Note that the URL you should use will depend on the configured transport protocol (git, ssh, http[s]) of your git server.
Next, you should makes some local configurations, for example, from a terminal:
git config user.name "your_username"
git config user.email "someone#example.com"
You can add --global to both of these command if you want these changes to persist outside of the current git repo.
Having a cloned repository automatically checks out an initial 'branch' on which you can make changes to files within your repository. See the git docs for a detailed explanation of a branch.
Files can be added, removed or renamed/moved using git add, git rm or git mv respectively.
Finally, you must commit your changes and push them
git commit -m "A message describing your changes"
git remote will confirm the name of the default remote (repo location). In this case, it is assumed to be origin. You can then push your changes to an existing branch (viewed using git branch -r), assumed to be master below:
git push origin master
Here you would be merging your master branch with the remote one. Clearly your permissions/project conventions will dictate what you can/should do to this repo.

Why must I run `git init` after `git clone`?

We have a GitHub repository. After I clone that repository I have to do a git init from command line before any Git commands will work. git pull, git checkout, etc. don't work.
As far as I know, git init initializes a new repository. Is it like after cloning I am creating a new repository again?
Here are the commands I'm trying to run:
git clone <url>
git checkout master
You don't have to do git init in this situation¹. As pointed out in the comments, git clone will give you a local repository that you can start using right away.
The problem is that you're not in your repository when you try to run Git commands. By default, git clone puts the repository in a subdirectory with the same name as the repository that was cloned. You must change into that directory to use it:
git clone git#github.com/foo/bar.git
cd bar
Now that you're in bar/ you can interact with your repository.
¹If you do run git init in the parent directory you're in for a confusing time. You'll end up with a repository in the wrong place and an inner repository containing your code. Git's behaviour with nested repositories often doesn't match users' expectations.
After the clone, get into the projects root directory by `cd your-project" then run git commands
In your case what happened was, you cloned the remote repository then didn't change your present working directory to the cloned project, you were in the parent of the cloned repo, since parent repo itself is not a git repository, git complained, you can't run any git commands

best way to export Eclipse project to GitHub

I need to export an Eclipse project to GitHub. I am using Eclipse-Mars with the eGit plugin. I have spent hours reading documentation, tutorials, and posts and see that there are basically two ways
Create an Eclipse project and a local Git repository. Commit to local repository. Create a repository on GitHub. Push the Eclipse project to the repository on GitHub. When I try this, I get the error "rejected non-fast-forward". I have no idea why. I did create a .gitignore on GitHub when I created the repository - is that causing a problem?
Create a repository on GitHub. In Eclipse, clone this repository and then add files. Commit to local repository, then push. This works, but I end up with a weird configuration on GitHub:
reponame/myprojname/src
when I would prefer the more normal
reponame/src
Which method is the correct way to proceed? Why do I get the push error in the first method, and why the strange folder layout in the second?
You first need to pull the github code into you local machine.
=> git remote add origin
=> git pull origin master
will automatically fetch and merge the github repo with your local copy. You may need to resolve some conflicts that could be generated due to 3-way merge.
After all this, you can use -
=> git push -u origin master
For the second Point,
If you want src/ directory directly under reponame/ on github, then you should execute git clone after creating the project in eclipse and git clone should be executed inside the myprojectname/ directory.
When you created the .gitignore file on GitHub, you created a commit in the repository that isn't in your local copy. You'll need to add the GitHub repo as a remote, pull the change, and then push your local changes:
git remote add origin <path to your repository>
git pull origin master
git push -u origin master

How to import a Bitbucket repo into my Github account?

I have a large repo on Bitbucket that I need to import into Github. The repo contains a ton of historical information that I do not want to lose. I've googled around and can't find a definitive resource which explains the process. Am I missing something obvious?
Thanks in advance!
Am I missing something obvious?
In many cases simply adding a new remote and pushing to it will do what you want:
git remote add github git#github.com:user/repo.git
git push github master
This will push your master branch to GitHub. You can push other branches in a similar manner, and you can push your tags with git push github --tags.
A more comprehensive option is to use the --mirror option, e.g.
# Add the github remote as above, then
git push --mirror github
From the documentation:
--mirror
Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not limited to refs/heads/, refs/remotes/, and refs/tags/) be mirrored to the remote repository. Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and deleted refs will be removed from the remote end. This is the default if the configuration option remote.<remote>.mirror is set.
Note that this implies --force, so be careful with it. Some users like to do this from a new bare clone of the remote (i.e. first do git clone --bare git#bitbucket.com:user/repo.git, then do the rest of the steps from the newly created bare repository).