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
Common Early Git Mistakes
Not Staging Changes: Forgetting to use
git add
beforegit 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:
Modify files in your working directory.
Stage the changes you want to include in your commit using
git add
.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.