Git is a distributed version control system that allows multiple developers to collaborate on a project, tracking changes to files and managing different versions of the codebase. Here's a brief explanation of how Git works:
A Git repository is a directory or folder that contains your project's files, as well as the entire history of changes made to those files. It acts as a central hub for collaboration.
In Git, a commit represents a snapshot of your project at a specific point in time. Each commit is associated with a unique identifier (a hash) and contains information such as the author, timestamp, and a message describing the changes made in that commit.
Git allows you to create branches, which are independent lines of development. Each branch can have its own commits, allowing multiple developers to work on different features or bug fixes simultaneously. Branches make it easy to experiment, collaborate, and merge changes back into the main codebase.
Git has three main areas:
-
Working Directory: This is the directory on your local machine where you modify and create files.
-
Staging Area (Index): The staging area is an intermediate area where you select and stage the changes you want to include in the next commit. You can think of it as a holding area for changes you want to commit.
-
Repository: The repository is where Git stores the complete history of your project, including all the committed changes. It is usually located in the hidden
.git
folder within your project directory.
The typical Git workflow involves the following steps:
-
Initialize a repository: Use
git init
to create a new Git repository in your project directory, orgit clone
to clone an existing repository. -
Add and commit changes: Use
git add
to stage changes from your working directory to the staging area. Then usegit commit
to create a new commit with the staged changes, along with a descriptive commit message. -
Create and switch branches: Use
git branch
to create a new branch, andgit checkout
to switch to a different branch. -
Merge branches: Use
git merge
to merge changes from one branch into another. This combines the commits from both branches into a single branch. -
Push and pull changes: Use
git push
to upload your local commits to a remote repository, andgit pull
to fetch and integrate remote changes into your local repository.
Command:
git init
Example:
$ git init
Initialized empty Git repository in /path/to/repository/.git/
Command:
git clone <repository_url>
Example:
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 25 (delta 6), reused 22 (delta 5), pack-reused 0
Unpacking objects: 100% (25/25), done.
Command:
git add <file_name>
Example:
$ git add index.html
Command:
git commit -m "Commit message"
Example:
$ git commit -m "Add initial version of index.html"
Command:
git branch <branch_name>
Example:
$ git branch feature/add-new-feature
Command:
git checkout <branch_name>
Example:
$ git checkout feature/add-new-feature
Command:
git push <remote_name> <branch_name>
Example:
$ git push origin main
Command:
git pull <remote_name> <branch_name>
Example:
$ git pull origin main
Command:
git merge <branch_name>
Example:
$ git merge feature/add-new-feature
Command:
git log
Example:
$ git log
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6
Author: John Doe <[email protected]>
Date: Mon Jul 05 12:34:56 2023 +0300
Add initial version of index.html
commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q
Author: Jane Smith <[email protected]>
Date: Sun Jul 04 09:12:34 2023 +0300
Update README.md
These are just a few examples of commonly used Git commands. There are many more commands and options available for different use cases. You can refer to the official Git documentation for more information: Git Documentation