Hands-on demo – Working with Git Locally
Git is a distributed version control system that allows developers to track changes in code, collaborate, and manage projects efficiently. Working with Git locally means managing code changes, commits, branches, and repositories directly on your machine before pushing them to a remote repository (e.g., GitHub, GitLab, Bitbucket).
Key Concepts
Repository: A collection of your project files and the entire Git history.
Commit: A snapshot of the code at a particular point in time.
Branch: A separate line of development that diverges from the main project (e.g.,
mainormaster).Staging Area: A place where changes are prepared before committing them.
Basic Git Workflow with Examples
Initialize a Git Repository
To start working with Git, you need to create a Git repository (also known as a repo).
x1# Navigate to your project directory2cd path/to/your/project3
4# Initialize a new Git repository5git initThis will create a .git folder in the project directory to manage version control.
Making Changes
After initializing the repository, you can make changes to files.
xxxxxxxxxx21# Create or edit a file2echo "Hello, World!" > index.htmlTracking Changes
To track changes, use git status. This command shows which files are modified, staged, or untracked.
xxxxxxxxxx11git statusExample output:
xxxxxxxxxx41On branch main2Changes to be committed:3 (use "git reset HEAD <file>" to unstage)4 modified: index.htmlStaging Changes
To stage changes for commit, use git add.
xxxxxxxxxx11git add index.htmlCommitting Changes
Once changes are staged, commit them using git commit.
xxxxxxxxxx11git commit -m "Added a simple HTML page"Viewing Commit History
To view the commit history, use git log.
xxxxxxxxxx11git logExample output:
xxxxxxxxxx41commit 1234567abcde2Author: Your Name <your.email@example.com>3Date: Wed Dec 21 12:00:00 2024 +00004 Added a simple HTML pageBranching
To create a new branch, use git branch.
xxxxxxxxxx11git branch feature-branchSwitch to the new branch with:
xxxxxxxxxx11git checkout feature-branchMerging Branches
After making changes in a branch, merge them back into the main branch.
xxxxxxxxxx21git checkout main2git merge feature-branchPushing to Remote Repository
Once changes are locally committed, push them to a remote repository.
xxxxxxxxxx11git push origin mainThis uploads changes to the remote repository.
Summary
Working with Git locally involves creating a repository, making changes, staging them, committing them, and managing branches. Once everything is ready, you push your changes to a remote repository for collaboration.






















Leave a Reply