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

Zoe picture Zoe · Feb 11, 2014 · Viewed 28.4k times · Source

I've created a new GitHub repository. I'm quite confused by working directory and local repository. When working on my own, my stuff all resides on working directory. But when work with repo, I should check my stuff there. But are they the same thing? or should they be separate. Then how to commit my stuff in working directory to my local repo. How to commit my local repo to remote repo. How to do this by Git Bash? Thanks!

Answer

Alexey Voytenko picture Alexey Voytenko · Feb 11, 2014

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.