42

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!

Zoe
  • 997
  • 4
  • 10
  • 19

4 Answers4

40

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.

davelupt
  • 1,845
  • 4
  • 21
  • 32
Spock77
  • 3,256
  • 2
  • 30
  • 39
  • Thanks, Alex! Could you please be more specific? For example, I have a working directory, JAVA, a local repo, JavaRepo, where should .git be and how can I commit files in JAVA to JavaRepo by Git Bash? Thanks! – Zoe Feb 11 '14 at 03:33
  • 1
    Added FOR EXAMPLE paragraph. – Spock77 Feb 11 '14 at 04:53
  • Millions of thanks, Alex! I'm doing what you said. As I use eclipse, I set my working directory also in JavaRepo, then there's a folder .meta appears. And every time I tried to check my projects, it's also there... Is there anything I did wrong? – Zoe Feb 11 '14 at 17:42
  • 1
    I created two projects in JavaRepo, but when push to GitHub, there's only one. The other only exists in .meta... – Zoe Feb 11 '14 at 17:46
  • 2
    I suppose .meta is the directory where Eclipse saves its 'workspace'? Try to set up [.gitignore](http://git-scm.com/docs/gitignore) list. – Spock77 Feb 12 '14 at 14:07
  • *Git is tracking the difference between your working directory and local repository*: No. No difference in between working directory and local repository except that working directory could be subdirectory of local repository. – haccks Jan 12 '15 at 00:04
13

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:

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

Kostia Mololkin
  • 868
  • 9
  • 25
Anna Skoulikari
  • 1,281
  • 10
  • 9
  • external resource should compliment text answer so it should go after text- otherwise great answer – Kostia Mololkin Apr 05 '20 at 09:24
  • 1
    @KostiaMololkin thanks so much for your edit (I approved) and for letting me know! (-: I'm a bit of a stack overflow newbie so it's nice to learn from more knowledgeable folk. – Anna Skoulikari Apr 05 '20 at 10:14
5

When you create a project-folder in your computer and start working on it, it is a working directory. It is synonymous to work bench or work space. At this point, your project-folder is not a repository.

To make your project-folder into a repository, you need to use the git init command. This creates a hidden folder called .git inside the main folder, which is called the repository.

The .git represents the local repository, which comprises of staging area and commit history.

The relationship between working directory, staging area, local repository (containing commit history), and remote repository and how they interact by means of different commands in git workflow is depicted very well in this image by Nikki Siappno.

enter image description here

hbstha123
  • 1,260
  • 11
  • 23
4

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.