DEV Community

Margai Wangara(he/him)
Margai Wangara(he/him)

Posted on

Back To Basics: Git

Personally, I believe that aspiring developers should learn git/version control before engaging into any kind of programming. Git is extremely important in programming. I remember when I was starting out, I had no idea what version control was. Infact, I only learnt about git about two years into my programming career (courtesy of Traversy Media's git course) and it was awesome.
Learning the basics of git is simple since to start out, one needs to only learn three commands.

  • Initialze a project as a git repository
  • Add files
  • Commit added files

Installation

  • Download git from here and install on your computer

Once git is installed, open the command line and type git then press enter. A long list of commands should be displayed.

Lets create a project and name it hello_world. Open the folder and git bash directly into it by right clicking and choosing Git bash here option from the dropdown list. Once the terminal is opened, type git init to initialize the folder as a git repository.

We can then add a file into the folder by typing touch index.html. This creates a html file. Open the html inside any text editor and add the following code (for testing purposes).


    <html>
        <head>
            <title>Hello World!</title>
        </head>
        <body>
            Hello World from Git!
        </body>
    </html>

Enter fullscreen mode Exit fullscreen mode

Adding files to git

To add changed files to git,

  • To add a single file

    git add index.html

Enter fullscreen mode Exit fullscreen mode
  • To add all files

    git add .

Enter fullscreen mode Exit fullscreen mode

Commiting files

Commiting files provides a point of reference incase you want to revert your code to a certain point


    git commit -m 'Initial Commit'

Enter fullscreen mode Exit fullscreen mode

The -m flag lets you add a message with your commit so as to understand what changes you made or files you added at that particular commit.

Ignoring files and folders

Git provides you with the ability to ignore files and folders that exist in your working folder which you don't want to add to your git repository. To ignore files and folder create a .gitignore file and add the name of the file or folder

In your terminal create a file and folder as follows

  • Creating a file

    touch ignorefile.txt

Enter fullscreen mode Exit fullscreen mode
  • Creating a folder

    mkdir ignorefolder

Enter fullscreen mode Exit fullscreen mode
  • Create a gitignore file to ignore the files and folders

    touch .gitignore

Enter fullscreen mode Exit fullscreen mode

You can open the gitignore file with your text editor or edit directly from the terminal. To edit directly from the terminal,


    nano .gitignore

Enter fullscreen mode Exit fullscreen mode

The command above opens the gitignore file in terminal, once the file is opened press i then write,


    ignorefile.txt
    ignorefolder/

Enter fullscreen mode Exit fullscreen mode

Then press CTRL/Command + X to exit. Then press Enter.

The file and folder will be ignored anytime the git add . command is executed.

Incase of any error, please feel free to comment or constructively criticize.

Top comments (0)