Git and GitHub

Basics of Git

Understanding the basics of Git is crucial for efficient version control and collaboration. This section covers fundamental concepts and commands to help you start with Git.

What is a Git Repo

A Git repository (repo) is a directory that stores all the files and their history for a project. It includes all the changes made to the files and the details of who made those changes and when. A repository allows multiple developers to work on the same project without interfering with each other’s work.

First Command: Git Init and Git Status

Initialize a New Git Repository:

  • Navigate to your project directory in the terminal:

    cd path/to/your/project
    
  • Initialize a new Git repository:

    git init
    
  • This command creates a new subdirectory named .git, which contains all the necessary repository files.

Check the Status of Your Repository:

  • Use the following command to check the current status of your repository:

    git status
    
  • This command will show which files are in the staging area, which are not tracked, and any changes made since the last commit.

Exploring the .git Folder

The .git folder contains all the metadata and object database for your repository. Key components include:

  • HEAD: Points to the current branch reference.

  • config: Contains the configuration settings.

  • description: A short description of the project.

  • hooks/: Contains client- or server-side hook scripts.

  • info/: Contains auxiliary information.

  • objects/: Stores all the content of your files and their history.

  • refs/: Stores pointers to commit objects.

To view the contents of the .git folder, navigate to your project directory and run:

ls -a .git

Untitled (46).png

Common Early Git Mistakes

  • Not Staging Changes: Forgetting to use git add before git commit.

  • Committing Unnecessary Files: Committing files that shouldn't be versioned, like build artifacts or personal settings.

  • Poor Commit Messages: Writing non-descriptive commit messages.

  • Ignoring .gitignore: Do not use a .gitignore file to specify intentionally untracked files.

The Committing Workflow Overview

The basic Git workflow consists of three main steps:

  1. Modify files in your working directory.

  2. Stage the changes you want to include in your commit using git add.

  3. Commit your staged changes using git commit.

Staging Changes with Git Add

The git add command is used to stage changes for the next commit. This includes adding new files, modifying existing files, and removing files. For example, in a backend project:

  • Add a new file to the staging area:

    git add app/controllers/user_controller.rb
    
  • Stage all changes in the project:

    git add .
    
  • Add specific changes within a file:

    git add -p app/models/user.rb
    

The Git Commit Command

The git commit command captures a snapshot of the project's currently staged changes. For example:

  • Commit with a message:

    git commit -m "Add user authentication feature"
    
  • Use a multi-line commit message:

    git commit
    

    This opens the default text editor, where you can provide a detailed commit message.

The Git Log Command

The git log command shows the repository's commit history. It includes the commit hash, author, date, and commit message. For example:

  • View the commit history:

    git log
    
  • View a brief history with one line per commit:

    git log --oneline
    
  • View a graph of your commits:

    git log --graph --oneline
    

By mastering these basic Git commands and concepts, you'll be well-equipped to efficiently manage and collaborate on software projects. Next, we'll dive deeper into the details of commits and how to manage them effectively.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board