Git's local repository and remote repository -- confusing concepts

GitRepository

Git Problem Overview


If I understand correctly, Git has two sorts of repositories: one called local, another called remote. My questions are extremely naive ones about the two types of repositories.

Is that correct to say

  • Git local repository is the one on which we will make local changes, typically this local repository is on our computer.

  • Git remote repository is the one of the server, typically a machine situated at 42 miles away.

Another question: some tutorial shows me this workflow

  • mkdir myproject
  • cd myproject
  • git init
  • touch README
  • git add README
  • git commit -a -m "

I see that git init creates myproject a local repository. What I don't understand is the git commit command. If I have not yet set a remote repository, how can Git know where to commit my README file??

I hope I was clear.

[EDIT] The way I am using Git might be different from others: I use a private Git repository to backup my codes. So I think I do need a remote repository. The local repository should be nonsense in this case. Am I right? Thanks for your clarification. These are the most naive questions that I cannot find replies anywhere else...

Git Solutions


Solution 1 - Git

As I'm experiencing exactly the same questions like yours (coming from VSS and TFS mindset) and during the last 3 days started to understand. I would believe that these kind of diagrams would be more helpful to understand the whole picture for anyone else trapped there.

from: https://greenido.files.wordpress.com/2013/07/git-local-remote.png?w=696&h=570

Git Flow diagram 1

Another one, from: https://wiki.lsr.ei.tum.de/lib/exe/fetch.php?media=nst/programming/git_flow.jpg&w=500&tok=e87798

Git flow diagram 2

Solution 2 - Git

Git is a distributed version control system and it makes Git awesome. Your local repository has exactly the same features and functionality as any other Git repository. So a Git repo on a server is the same as a Git repo on GitHub (granted GitHub adds additional features, but at its core, you're dealing with Git repositories) which is the same as your coworker's local repo.

So why is that awesome? Because there is no central repo you have to have access to do your work. You can commit, branch, and party on your own repo on your local machine even without internet access. Then, when you have a connection again, you can push your changes to any other Git repo you have access to. So while most people treat a particular repo as the central repo (repository), that's a process choice, not a Git requirement.

The point of all that was to state (as others have) that you're committing your README to your local repo. Then, whenever you choose, you can push changes from your local repo to any other repo. It's pretty nifty!

Solution 3 - Git

Your quoted statement is correct.

You don't need to have a remote repository at all.
You can have the full git experience, with commits, branches, merges, rebases, etc, with only a local repository.

The purpose of a remote repository (eg, GitHub) is to publish your code to the world (or to some people) and allow them to read or write it.

The remote repository is only involved when you git push your local commits to a remote repository, or when you git pull someone else's commits from it.

Solution 4 - Git

> If I have not yet set a remote repository, how can Git know where to commit my README file??

You are committing to your local repository (which is a proper repository with its own change control; it isn't just a local checkout … since you created it locally, it isn't even a local checkout!).

If you want to send changes to a remote repository (to back them up, make them available to other members of the team, or publish them to the world), you must push them.

Solution 5 - Git

The need of a remote repository instance on github for example is only to share the code with other programmers, since they can' just access your git repo (unless they connect to your pc using your ip an still then they only have access to your changes only but not the changes of other programmers), that' why commit your changes from you local repository to the remote/central repo so that other developers can check them out and finally have end up wih a same state of the software. But at the end if you are the only developer, you don't really need a remote/central repo.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzellView Question on Stackoverflow
Solution 1 - GitBishoy HannaView Answer on Stackoverflow
Solution 2 - GitagminView Answer on Stackoverflow
Solution 3 - GitSLaksView Answer on Stackoverflow
Solution 4 - GitQuentinView Answer on Stackoverflow
Solution 5 - GitvelocityView Answer on Stackoverflow