06-27-2024, 03:18 PM
Git is a powerful version control system (VCS) that allows you to track changes in your code over time. Here's a breakdown of how to use Git for version control:
[b]1. Setting Up Git:[/b]
[b]1. Setting Up Git:[/b]
- [b]Install Git:[/b] If you haven't already, download and install Git from the official website https://git-scm.com/downloads. The installation process typically involves following a straightforward wizard.
- [b]Verify Installation:[/b] Open a terminal (command prompt on Windows) and type
. If Git is installed correctly, you'll see the installed version number.Code:git --version
- [b]Create a Project Directory:[/b] Create a directory for your project files. This could be your codebase, website files, or any collection of files you want to track with Git.
- [b]Open Terminal:[/b] Open your terminal and navigate to the project directory using the
command (e.g.,Code:cd
).Code:cd myproject
- [b]Initialize Git:[/b] Run the command
in your terminal. This creates a hidden folder calledCode:git init
within your project directory. This folder stores all the Git version control information.Code:.git
- [b]Making Changes:[/b] Edit your project files as usual.
- [b]Staging Changes:[/b] Once you're ready to track changes, use the
command. For example,Code:git add
stages the specific file for version control. You can also useCode:git add index.html
to stage all modified files in the current directory.Code:git add .
- [b]Committing Changes:[/b] After staging your changes, use the
command to create a snapshot of the staged changes. You'll need to provide a commit message briefly describing the changes you made. A good commit message should be clear and concise, explaining what was modified.Code:git commit
- [b]Example:[/b]
Code:git commit -m "Added a new home page"
- [b]Understanding the Workflow:[/b] Git works with three main areas:
- [b]Working Directory:[/b] This is your project folder where you edit your files.
- [b]Staging Area:[/b] This is a temporary holding area for changes you want to include in your next commit. You use
to stage changes.Code:git add
- [b]Repository:[/b] This is the central location where all the versions (commits) of your project are stored. The
folder in your project directory holds the repository.Code:.git
- [b]Working Directory:[/b] This is your project folder where you edit your files.
- [b]Listing Commits:[/b] Use the
command to see a list of all your commits. This displays information like commit message, author, and date for each commit.Code:git log
- [b]Viewing Specific Commit:[/b] Use
to view details of a specific commit, including the commit message and the file changes introduced in that commit. TheCode:git show <commit_hash>
is a unique identifier for each commit.Code:<commit_hash>
- [b]Unstaging Changes:[/b] If you've staged a change but decide you don't want to commit it yet, use
from the staging area (not the working directory).Code:git rm <filename>
- [b]Discarding Committed Changes:[/b] To undo a committed change, you can't directly edit past commits. However, you can create a new commit that reverts the changes.
- [b]Branching:[/b] Branches are lightweight ways to experiment with different versions of your project without affecting the main codebase. You can create a new branch using
and switch between branches usingCode:git branch <branch_name>
.Code:git checkout <branch_name>
- [b]Merging:[/b] When you're happy with the changes in a branch, you can merge them back into the main branch using
.Code:git merge <branch_name>
- [b]Remote Repositories:[/b] For collaborative projects, you can push your local Git repository to a remote hosting service like GitHub or GitLab. This allows multiple people to work on the project and share changes.