What's the difference between working directory and local repository?

Working directory is the directory with your source files under git control (in the root of all dirs under control .git file is present). Git is tracking the difference between your working directory and local repository, and between your local repository and (one of) remote repositories.

To see what was changed, use $ git status.

To commit your changes (edits and/or new files) to the local repository, use $ git add and then $ git commit.

To see what was committed use $ git log.

Then, if you want to commit your changed to the remote repository, use $ git push.

Pay attention to what git commands are printing, it often contain advice what to do.

If you are working with GitHub, their help is also useful: pushing to a remote.

Good basic information is also in atlassian site.


FOR EXAMPLE:

Suppose, you initiated your git repository with $ git init in JavaRepo dir and checkouted there the project with $ git pull or $ git clone. Then JavaRepo should contain .git file (among others git dotted files) - you can check it with $ ls -a. These files itself are the local git repository, and there is no need to distinguish 'working directory' and 'local repository' directory.

So, start working with files in JavaRepo: say you changed file example.java and created new file example2.java. Execute $ git status in JavaRepo (or in any its subdirs). It should show: 'modified: example.java', 'Untracked files: example2.java'.

Add your files to the staging area with $ git add example.java and $ git add example2.java commands from the directory with these files. (Notice that $ git add is both for changed and new files.)

And finally commit your files by $ git commit -m "example changes" (-m stays for message - comments to the commit).

$ git log should show this commit.


The contents of your project folder (the folders and files you find within it) are represented by the working directory.

The working directory is sort of like a workbench, it's where you work on your files (you edit them, you add new files, you delete files etc.).

On the other hand, the .git folder (which is a hidden folder) represents the repository.

Within the .git folder there are two "places" that should be mentioned, the staging area (represented by the index file) and the commit history (represented by the objects folder).

The staging area is sort of like a rough draft space. Whenever you are done working on a file (or files) in your working directory, you want to copy them to the staging area (using the git add command).

Once you have all the files that you want to update in the next version of your project in the staging area, you are ready to save them in the next version of your project which is called a commit. You do this using the git commit command.

A commit is basically a version of your project and each commit has a 40 character hash (40 letters and numbers) and this hash acts like a name for the commit, it's a way to refer to it.

The commits are in the commit history (objects folder).

So in order to add a file to our repository:

  • First we create it in our project folder so it goes in our working directory
  • Then we add it to the staging area (using the git add command)
  • And then it becomes part of a commit in our commit history (using the git commit command)

This video explains things very simply in a visual manner (also the video right before and right after it help!)


There is a layer between working directory and local repository. It is called staging area. When you add your changed files using 'git add', the changes first gets stored in staging area for checking. Then you can use 'git commit' to further push your changes from staging area to local repo.