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
Related
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.
How to import a specific project from github ? I dont want to clone the entire repository
I just want to clone only a portion of repository .
For eg this url https://github.com/eugenp/tutorials
has many projects but I just want to clone only spring-boot-crud project .
Here is the url for spring-boot-crud project.
https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-crud
Thank you .
You can look into a git sparse-checkout: that command is made to checkout only part of a Git repository. This assume the latest Git 2.26 though.
And I mean Git-core, as in Git SCM, not Egit (which does not support the new sparse-checkout command)
Even though the command is new and still experimental, it should be useful in your case.
git clone --no-checkout /url/a/repo
cd repo
git sparse-checkout init --cone
git sparse-checkout set spring-boot-modules/spring-boot-crud
Then open the relevant project in your Eclipse.
Create a project in the Git repo (.project, in the root folder of your repo)
That will give you:
git clone -n https://github/git/git git2
cd git2
git sparse-checkout init
git sparse-checkout set Documentation
At this point, you have the repository git/git with only the folder Documentation checked out (everything else is not in the working tree)
# create an empty project in C:\path\to\git2 in Eclipse
As you can see, all the other files not checked out are not displayed in the Git staging view. Only the ones currently checked out and modified are listed.
The first step must be done in command-line because JGit does not support the sparse-checkout directive (see but 383772 and change 33)
I cloned a git repo into my pc using this command in my git bash
git clone https://github.com/xxx/yyy.git
which created a folder called yyy in my pc.
How can I update the folder yyy in my pc with new contents of the https://github.com/xxx/yyy.git (is it called remote repo)?
I followed the instructions in Updating a local repository with changes from a Github repository, in particular git pull origin master but none of them worked and returned the error $ git pull origin master
fatal: Not a git repository (or any of the parent directories): .git.
I also tried git pull https://github.com/xxx/yyy.git as I reasoned that if I successfully did git clone https://github.com/xxx/yyy.git, the git pull https://github.com/xxx/yyy.git must work otherwise git syntax is not great.
Should I do the "clone" again to overwrite the existing folder in my pc? Why can't I do "pull"?
You need to issue git commands from within the cloned repository:
cd yyy
git pull
You are probably executing this command from outside the yyy folder. You have to go into it first:
cd yyy
git pull
i have some apps and all the apps is in git repo with submodule.i added those repo by this command
git submodule add https://github.com/rahmanshaber/about
i am getting some problems.
1.when i click on the submodule it shows the old commit when it added it. i want the latest commit of the submodule.
2.when i clone the supper reop(where all the apps as submodule).it only downloads the folders .then i put command to download the submodules, i got this error
git submodule update --init --recursive
fatal: not a git repository (or any of the parent directories): .git
here is the gihub page
i found the both solution.
it will show the commit when it is added .i have to manually update all to latest commit by this command
git submodule foreach git pull origin master
2.i am getting this error because i am not putting the command on the downloaded repo folder.i am putting command in out of it.
thanks.
I made the mistake of creating a workspace before a repository on C9.io.
As a result I do not have version control. Naturally, I want to use Git for my project before make any more changes. Everything I have tried has failed. I would rather not have to copy all the code I've made into a new work space with an already set up repository. So if anyone has any suggestions or answers to this problem, via Command line, GUI or anything else that would be wonderful.
cloud9 doesn't do any special magic when cloning from a git repository, and you can add a remote the standard way you would do locally:
git init initialize a git repository in the current folder
git add -a add everything (or what you want)
git commit -m "initial commit" commit
git remote add origin git#github.com:<me>/<repo>.git add your repo
git push origin HEAD:master push HEAD to master branch in the remote you just added