Git Overview

Git is a distributed source control management system. Git is a relatively fast source control program and has built-in data integrity check. Using git, it is easy to create an experimental branch and then either bring merge it back to the main branch or discard the experimental branch.

Getting started

  1. Get the Git scm tools from the https://git-scm.com/downloads. MacOS already comes with it. You can check if your computer already has Git by running this command in a console.

    You can run the Git commands in the regular OS console (Terminal in MacOS and Command Prompt on Windows) or through Git Bash.

    
    git --version
    
    

    The git version

  2. Setup your username and email address by running the following commands:

    
    git config --global user.name "John Doe"
    git config --global user.email "john@doe.com"
    
    
    setting up git's global user configuration
  3. You can confirm the setting took effect by running the following commands. These are the same commands as above for setting the username and email address, but without providing values in quotation:

    
    git config --global user.name
    git config --global user.email
    
    
    getting the git's global user configuration to display

    Git will tag this username and email address as the author for the source code change commit. This is a global setting, and it is possible to override global setting on per repository level.

Initializing a new git repository

Initializing a new git repository to an existing project or to an empty folder, it is the same command. Go to the root of the project's directory and run the following command

  1. 
    git init
    
    
    initializing a new git repository locally
  2. The git status command will show the basic information about the repository, such as which branch repository is currently on, commits, any tracked files that was stagged to be committed, and any untracked files.

    Once git initialized done on the directory, git will automatically monitor all the files from the that directory and subdirectories.

    
    git status
    
    
    git status showing that there is nothing commit

    This is an example of a git status output on a git initialized project directory that is empty.

    git status showing untracked files

    This is the git status output on a repository with files. It shows that the existing files are untracked.

Adding files to git for tracking

  1. We can add file to git for tracking. Run the command git add [filename].

    
    git add index.html
    
    
    git add command to add just one file to track
  2. Since this is a new git repository to an existing project with files, we can also add all existing files to be tracked. Run the command git add .

    
    git add .
    
    
    git add command to add all the files and directories to track

Committing the changes

Once the files has been stagged for tracking, we can commit the changes to git.

Git log

Git Ignore

Usually, a project has files that are only used for local development and these files that should not be committed to source code to be tracked. Git provides a way to ignore tracking these files. Create a file called .gitignore at the root of the source repository directory. List files and directory paths that should not be tracked.

For example, in a node.js project, we have node_modules directory that are only for local development and threfore should not be committed to a remote repository.

In this case, add /node_modules in the .gitignore file.

We can also add wild card pattern to gitignore. For example, if we want to ignore all files with .log extension, add *.log in the .gitignore file.


        /node_modules
        *.log
    

 

 

Published on Last updated on