-
Notifications
You must be signed in to change notification settings - Fork 0
Git
Git is a distributed version control system that allows developers to track changes in their code, collaborate with others, and manage projects efficiently. It was created by Linus Torvalds in 2005 and has become the industry standard for version control. Unlike centralized version control systems, Git allows every developer to have a full copy of the repository, making it highly reliable and robust.
Git operates on a distributed version control model, meaning each developer has a complete copy of the repository, including the entire history. This makes Git highly efficient and reliable. Here’s a simplified workflow of how Git works:
- Working Directory: The local folder where files are created, edited, and deleted.
- Staging Area: Files that are marked to be included in the next commit.
-
Local Repository: Commits are stored locally in the
.git
directory, maintaining a history of changes. - Remote Repository: The central repository (e.g., on GitHub, GitLab) where changes can be pushed and shared with others.
- Modify files in the Working Directory.
- Add files to the Staging Area using
git add
. - Commit changes to the Local Repository using
git commit
. - Push changes to the Remote Repository using
git push
. - Pull updates from the Remote Repository using
git pull
to keep the local copy up to date.
This decentralized approach allows for collaboration while maintaining version history and avoiding conflicts.
Git offers several advantages, including:
- Version Control: Track changes and revert to previous versions if needed. Every change in the codebase is recorded, allowing developers to go back in time and review modifications.
- Collaboration: Work on projects with multiple developers without conflicts. Git enables multiple contributors to work on the same codebase simultaneously while efficiently managing changes.
- Branching & Merging: Create isolated branches for features, bug fixes, or experiments. This ensures that new features can be developed independently without affecting the main codebase until they are ready to be merged.
- Distributed System: Every developer has a full copy of the repository, reducing dependency on a central server and ensuring data redundancy.
- Efficiency: Optimized for speed and performance. Git performs operations such as commits, branching, and merging quickly, even with large projects.
Here are some fundamental Git commands to get started:
Before using Git, set up your identity:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
This ensures that all commits you make are associated with your identity.
To start tracking a project with Git:
git init
This creates a new Git repository in the current directory, allowing version control to begin.
To copy an existing repository from GitHub:
git clone https://github.com/user/repository.git
This command downloads the repository and all its history to your local machine.
To check changes in your working directory:
git status
This command shows which files have been modified, staged, or remain untracked.
To add specific files:
git add filename
To add all changes:
git add .
Adding files to the staging area prepares them to be included in the next commit.
Save changes with a message:
git commit -m "Your commit message"
Commits are like snapshots of the repository at a specific point in time, allowing easy rollback if needed.
To send your local commits to GitHub:
git push origin main
This uploads your changes to the remote repository, making them available to other contributors.
To fetch and merge changes from the remote repository:
git pull origin main
This command updates your local repository with the latest changes from the remote repository.
To create and switch to a new branch:
git checkout -b new-branch
Branches allow you to work on new features or fixes without affecting the main branch.
To merge a branch into the main branch:
git checkout main
git merge new-branch
Merging incorporates changes from a separate branch into the main branch.
To see the commit history of your repository:
git log
This displays a list of all commits, including their authors, timestamps, and messages.
To undo modifications to a file before staging:
git checkout -- filename
To remove a file from the staging area without deleting it:
git reset filename
To revert the last commit while keeping changes in the working directory:
git reset --soft HEAD~1
To completely undo the last commit and remove changes:
git reset --hard HEAD~1
- Git Documentation - Official documentation for Git commands and workflows.
- GitHub Learning Lab - Interactive tutorials for learning Git and GitHub.
- Pro Git Book - A comprehensive guide to mastering Git.
- Git Cheatsheet - A quick reference guide for commonly used Git commands.