Working with repositories

In this article we are going to be looking at how to:

  • Turn a normal folder into a Git repository

  • Add files to the repository, add them to the Git index and commit the changes

 

We will be using the command line and Ubuntu Linux to learn these techniques, you may prefer to use a GUI and other operating system but as there are so many options, and as learning the command line is harder, and thus makes us feel smarter, we will stick with the programmer’s friend.

Let’s leap in before we change our minds.


 

Turn a normal folder into a Git repository

  • Create a new folder called git-tutorial – This is going to be your first repository

  • Open up the Terminal or Command Line

  • Navigate to your new folder – In linux, if you have created your folder in your home folder, simply type in cd git-tutorial (cd is the command to change directory)

Change directory
  • Now you are in the correct folder, type git init – short for initialise, it tells Git that you want to turn the folder into a repository

Initialise a Git repository
  •  Git will now tell you that you have initialised an empty repository, go open the folder you created and take a look, you will see that Git has created a folder called .git (it’s a hidden folder so make sure you have turned on ‘Show hidden folders’)

  • Take a moment to explore this hidden .git folder, this is where Git will hide all its magic

View empty Git directory


 

Add files to your repository

Adding a file (or multiple files) to our new repository is a three step process:

  1. Add the file/s to the folder

  2. Tell Git the files are there (this adds them to Git’s index of files)

  3. Tell Git you want to commit the changes permanently

 

Add the file/s to the folder

This is the bit you already know, just put the file, files or folder into your new repository folder. Now you have your new file, plus the .git folder as seen in the screenshot.

File added to repository folder

 Tell Git the files are in the folder (this adds them to Git’s index of files)

  • Now we are back to the command line, make sure you are in the git repository folder, use cd git-tutorial like we did before to change to the correct directory.
  • There are two ways to add files, either by 1. telling Git there are a bunch of files, so add them all, or 2. by specifying each individual file:
  1. git add . (Don’t forget the space and . After the word ‘add’)
  2. Git add filename (You can add multiple files in a row i.e. filename1 filename2 etc
File added to index

 Tell Git you want to commit the changes permanently

  • You have added the new files to the Git index, now Git needs to know if you are really committed to these changes, that’s right, Git has no faith in you, so let’s show it our commitment in the only way Git respects, with a command!

    • Enter git commit to show Git you’re no flake

Git commit command


 

  • Git wants you to make another declaration of your commitment to these changes, it now asks you to add a comment explaining what you have done to the log. You can write anything you like here, but the more descriptive it is the better.

Git commit comment

Look at the above screenshot, you can see that the first line is my comment explaining the changes I made. Git goes on to explain that the changes are saved on the master branch (more on this later) and tells us which file has been added or changed.


 

You have to do this every time you make a change

So once you have set up your repository it will chug along quite happily, but if you make a change you must add it to the index and then commit it every time!

 

What did I just read?

  • A Git repository is just a normal folder

  • The git init command turns a folder into a repository

  • To add a file to the folder, just drag it in or save it there

  • Files must be added to the index, do it individually or all at once with git add . Or git add filename1 filename2

  • To commit to these changes use the git commit command

  • Every commit must have a comment in the log, make it descriptive!

  • Every change you make to your repository must be added to the index and committed every time, or it isn't actually in the repository

Tags

git
repsoitories
folders
directories
git init
git add .
git commit
tutorial