NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
Edit on GitHub
Export as PDF
  1. Hackerschool
  2. Lightning Git

Making your first commit

PreviousGetting Started with GitNextBranching

Last updated 26 days ago

Great, now we have Git set up! You might have some questions about the Git Setup process:

Why do I need to indicate my name and email?

If you remember, each commit contains metadata about the author's name and email. When you set this up in your git config, this will ensure all your commits are properly attributed to the correct author (you!)

What is this ssh key stuff?

To put it simply, it is a good way for Github to "authenticate" you. You wouldn't want unauthorized people trying to change your repositories.

The traditional way is to use the commands in Command Glossary to add files to the staged area, then using git commit. Let's try using lazygit to speed up this workflow.

To start, let's first initialize a repository somewhere.

mkdir recipe_repo
cd recipe_repo
git init

Make a new file, recipe.txt and modifying it a little.

Now, run lazygit

lazygit
  • Hit 2 to go to the files submenu.

  • Hit a to stage all commits (this is the same as git commit -A)

  • Hit spacebar to stage commits by individual files

  • Hit Enter to enter into a file and use spacebar to select line by line which files to stage (This is known as interactive staging)

  • Once you've selected what you want to commit, press c, and enter a message, then hit Enter to commit

Adding a new file

Create a new file in the folder and add some text to it.

echo 'Hello world' >> hello.txt

The command above essentially redirects the output of the echo (Hello world) into a new file hello.txt

If you don't want to use bash commands, you can just create the file using your preferred method as well.

Making your first commit

Getting the status of a repository

Now, run the following command to view the status of your repository:

git status

You should see the following:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	hello.txt

nothing added to commit but untracked files present (use "git add" to track)

git status is to view the state of your repository in Git's eyes. Use it to view things like the current files in the snapshot.

Tracking files

You may notice that the git status message states that hello.txt is untracked. Untracked files are those that have never been registered with Git before. They are often new files that have been added to the repository and have not existed in any snapshots.

Files that have been added to a snapshot before are considered "tracked" and Git knows to look out for changes between snapshots.

Adding files to the staging area

To add hello.txt to the staging area, use the following command:

git add hello.txt

Then, use git status to view the status of your repository again:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   hello.txt

Notice that now, instead of stating that your file is untracked, Git is indicating that the changes have not committed. This is a sign that the file(s) have been tracked and added to the snapshot.

You can use . to add all files in the current folder as well.

Taking the snapshot

Now, to take the snapshot (make the commit), you can use the following:

git commit -m "First commit"

The -m flag is used to specify the commit message. Every commit has an accompanying message that you can use to indicate what the commit contains/entails.

If you do not use -m, your favorite terminal/GUI editor will be launched and you can compose the commit message in that editor, save it, and close the editor

There you have it! You have made a local repository and created a snapshot! We will now look at how we can integrate Github with your local repository!

Ignoring files

See: Ignoring Files

Recall that in , Git does not automatically add files to a snapshot as it does not know exactly what you want. So we want to tell Git that we want hello.txt in the snapshot.

As discussed in , a file from the working directory needs to be explicitly added to the staging area for a snapshot to include it. By default, an untracked file that is added to a snapshot becomes tracked for future snapshots.

#adding-files-to-a-snapshot
Introducing the commit

Adding files to a snapshot

By default, Git does not know what files it should be including in a snapshot (and this is a good thing because we don't want Git to just add every file as they may contain sensitive information).

This is where the "three areas" concept comes into play. It is often good to think of your projects with Git as three separate concepts:

  1. Working directory: where your codebase actually resides

  2. Staging area: set of files that you want to include in a snapshot

  3. Repository: local/remote repository storing metadata about the project and Git

By default, all of your files reside in the working directory and are not yet added to the staging area. If you want a file included in the staging area, then you must first add it to the staging area (we will cover how this happens later on).

There are also ways to remove files from the staging area!