GIT Essentials
Last updated
Last updated
This tutorial covers the definition of Git, its basic commands and concepts to help you get started with version control. As you become more familiar with Git, you can explore more advanced features and workflows.
For detailed documentation and specific Git commands, you can use the git --help
command or refer to the official Git documentation: Git Documentation.
Git is a distributed version control system that is widely used in software development to manage and track changes in code and project files. It was created by Linus Torvalds in 2005 and is known for its flexibility, speed, and robust branching and merging capabilities. Git is designed to help multiple collaborators work on the same project, enabling them to make changes independently and then integrate those changes seamlessly.
Git stores the entire project history as a series of snapshots or commits. Each commit represents a specific state of the project at a particular point in time. Developers can work on different branches, allowing for parallel development of features and bug fixes. Once work on a branch is complete, changes can be merged into the main project branch.
Let's walk through a simple example to illustrate how Git works in a real-world scenario. We'll create a Git repository, make changes, and commit those changes.
Step 1: Initialize a Git Repository
Create a new directory for your project and navigate to it in your terminal.
Initialize a Git repository in the directory:
Step 2: Create and Edit Files
Create a new file (e.g., about.html
) in your project directory:
Edit the file using a text editor or code editor to add some content. Save the file. (optional)
Step 3: Stage and Commit Changes
Check the status of your Git repository to see the untracked changes:
Stage the changes for commit:
Commit the changes with a meaningful message:
Step 4: Viewing Commit History
You can view the commit history of your project:
The git log
command displays a list of commits, including commit messages, author information, and commit timestamps.
Step 5: Create a New Branch
Suppose you want to work on a new feature. Create a new branch for this feature:
Step 6: Make Changes and Commit
Edit the about.html
file to make changes. Save the file and commit the changes to the feature branch:
Step 7: Merge Branches
After completing the feature, you can merge the feature branch into the main branch (usually named "main" or "master"):
Step 8: Push to a Remote Repository
To collaborate with others, you can push your repository to a remote Git hosting service like GitHub:
Create a repository on GitHub.
Add the remote repository to your local Git repository:
Push your local repository to the remote:
Set default upstream branch for a local branch if you cannot push your changes:
This example demonstrates how Git can be used for tracking changes, branching, committing, and collaborating with others. Git's flexibility and powerful version control capabilities make it an essential tool for software development and collaborative projects.