Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding git init

Tags:

What is git init for exactly? Must I do it once per computer or once per project that uses git? I downloaded my project by git clone and got it working, but now it is storing my project also to C:/Users/myUser/git , is that certain folder or I can change it?

I don't really know much about that folder, it seems to be like a local git repo or something, but what "manages" it, or why it is using that path, can you explain it please?

This is what I understand, fix me if I am incorrect, need to get facts straight:

  1. git init is for every project
  2. The git folder under "Users" is local repo and everytime i do git commit, that folder is updated.
  3. When I do git push , it takes from that local repo, and puts to remote repository.
  4. When I want to update to "HEAD", I just do git pull
like image 495
Jaanus Avatar asked Nov 23 '12 08:11

Jaanus


People also ask

Do I have to git init every time?

git init is only for when you create your own new repository from scratch. It turns a directory into an empty git repository. When you use git clone to clone an existing repository into a new one, git init is neither necessary nor desired.

Does git init create a commit?

git init --bare also creates a repository, but it does not have the working directory. This means that you can not edit files, commit your changes, add new files in that repository.

Do I need to git init before pull?

When running git clone , what actually happens in the background is a git init , followed by git remote add origin ${URL} and then a git pull . Typically, you only use git init if you already have code and you want to put it in a new Git repository.


1 Answers

That's actually a lot of questions and misunderstandings. I'm not sure I'd be able to address them all so I'm only going to address what's directly asked.

  1. git init is for every project

    Almost correct. git init is used to start using git on a project that's not under git. For projects that are already under git you use git clone.

  2. The git folder under "Users" is local repo

    Almost correct. The folder is actually .git not git. This comes from the unix convention that all files and folders that start with a dot are considered hidden.

    Secondly, the folder is not under your Users folder. It is under your project folder. So the folder C:/Users/myUser/ is one project. If this is not your intention then you most likely have accidentally executed git init in your User folder.

    Each project has one .git folder in the project's root directory and that is the project's repository. This is one of the reasons git is so fast compared to svn or cvs - the entire repository is processed on the local hard disk without any network traffic.

  3. When I do git push , it takes from that local repo, and puts to remote

    Correct, but only for repos that have remotes (which are usually repos that you create by using git clone to copy a remote repo).

    Note that the remote repo does not need to be on another machine. You can git clone a project from a local folder into another folder and then you can push changes from the new folder back to the original folder.

    The git clone command automatically sets up the necessary config for your repo to connect back to a remote. But you can also manually configure a repo set up with git init to connect to a remote.

  4. what "manages" it

    The .git folder manages your project's repo. Git doesn't run as a server*. Instead the .git folder acts as your local 'server' that all the git commands communicate with. Basically, running a git command edits the contents of the .git folder.

    *note: Remote repos do run servers so you can connect to them. But technically they're not really git servers. They're file servers that git can download from and upload to.

like image 157
slebetman Avatar answered Oct 18 '22 18:10

slebetman