How to make first git commit

GitGitBeginner
Practice Now

Introduction

This comprehensive tutorial guides developers through the process of making their first Git commit, providing essential knowledge for effective version control. Whether you're a beginner or looking to refresh your Git skills, this guide will help you understand the fundamental steps of initializing a repository, staging changes, and creating your initial commit.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/SetupandConfigGroup(["`Setup and Config`"]) git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git(("`Git`")) -.-> git/CollaborationandSharingGroup(["`Collaboration and Sharing`"]) git/SetupandConfigGroup -.-> git/config("`Set Configurations`") git/SetupandConfigGroup -.-> git/git("`Show Version`") git/SetupandConfigGroup -.-> git/init("`Initialize Repo`") git/SetupandConfigGroup -.-> git/clone("`Clone Repo`") git/BasicOperationsGroup -.-> git/add("`Stage Files`") git/BasicOperationsGroup -.-> git/status("`Check Status`") git/BasicOperationsGroup -.-> git/commit("`Create Commit`") git/CollaborationandSharingGroup -.-> git/remote("`Manage Remotes`") subgraph Lab Skills git/config -.-> lab-461419{{"`How to make first git commit`"}} git/git -.-> lab-461419{{"`How to make first git commit`"}} git/init -.-> lab-461419{{"`How to make first git commit`"}} git/clone -.-> lab-461419{{"`How to make first git commit`"}} git/add -.-> lab-461419{{"`How to make first git commit`"}} git/status -.-> lab-461419{{"`How to make first git commit`"}} git/commit -.-> lab-461419{{"`How to make first git commit`"}} git/remote -.-> lab-461419{{"`How to make first git commit`"}} end

Git Fundamentals

What is Git?

Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to work together efficiently, manage project versions, and collaborate seamlessly.

Key Git Concepts

Version Control

Version control helps developers track and manage changes to their codebase. Git provides a robust mechanism for:

  • Tracking file modifications
  • Maintaining project history
  • Enabling collaborative development

Core Git Components

graph TD A[Working Directory] --> B[Staging Area] B --> C[Local Repository] C --> D[Remote Repository]
Component Description
Working Directory The directory where you modify files
Staging Area Prepares changes before committing
Local Repository Stores commit history on your machine
Remote Repository Shared repository for team collaboration

Git Workflow

  1. Modify files in working directory
  2. Stage changes using git add
  3. Commit changes using git commit
  4. Push changes to remote repository using git push

Why Use Git?

  • Distributed development
  • Branching and merging capabilities
  • Complete project history tracking
  • Easy collaboration
  • Open-source and free

Getting Started with LabEx

LabEx provides interactive Git learning environments to help developers master version control skills effectively.

Git Installation Setup

Prerequisites

Before installing Git, ensure your Ubuntu 22.04 system is updated:

sudo apt update
sudo apt upgrade

Installation Methods

Method 1: Using APT Package Manager

Install Git directly from Ubuntu's default repositories:

sudo apt install git

Method 2: Adding Git PPA (Personal Package Archive)

For the latest Git version:

sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git

Verification

Check Git installation and version:

git --version

Initial Configuration

Setting User Name and Email

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Configuration Levels

graph TD A[System Level] --> B[Global Level] B --> C[Local Level]
Configuration Level Scope File Location
System All users /etc/gitconfig
Global Current user ~/.gitconfig
Local Current repository .git/config

Optional: SSH Key Setup

Generate SSH key for secure repository access:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

LabEx Recommendation

LabEx provides comprehensive Git environment setups for seamless learning and development experiences.

Creating First Commit

Initialize a Git Repository

Create a new project directory and initialize Git:

mkdir my-first-project
cd my-first-project
git init

Git Commit Workflow

graph LR A[Working Directory] -->|git add| B[Staging Area] B -->|git commit| C[Local Repository]

Creating Project Files

Create a sample README file:

echo "## My First Project" > README.md

Checking Repository Status

git status

Staging Changes

Add file to staging area:

git add README.md

Committing Changes

Create your first commit:

git commit -m "Initial commit: Add README.md"

Commit Best Practices

Practice Description
Clear Messages Write descriptive commit messages
Atomic Commits Commit small, focused changes
Consistent Style Use a standard commit message format

Viewing Commit History

git log

Advanced Commit Options

Combining Staging and Committing

git commit -am "Quick commit message"

LabEx Learning Tip

LabEx provides interactive tutorials to master Git commit workflows effectively.

Summary

By following this tutorial, you've learned the core principles of Git version control, including repository initialization, file staging, and commit creation. These foundational skills are crucial for collaborative software development, enabling you to track changes, manage project versions, and work effectively with development teams using Git.

Other Git Tutorials you may like