Git is a widely used version control system (VCS) that allows developers to track changes in source code during software development. Linus Torvalds developed it in 2005 to oversee the Linux kernel’s development.Linus Torvalds developed it in 2005 to oversee the Linux kernel’s development.. Here’s a breakdown of the core concepts and features:
1. Key Concepts
- Repository (Repo): A Git repository is where the project’s files, history, and changes are stored.
- Commit: A snapshot of changes in the repository. Each commit records who made the change, what changed, and when.
- Branch: A pointer to a series of commits, allowing different versions of the project to exist simultaneously.
- Merge: Combining changes from different branches into one. Merging helps integrate features and updates.
- Remote: External repositories, typically hosted on services like GitHub or GitLab, for sharing code with others.
- Clone: Making a local copy of a remote repository to your computer.
- Pull & Push:
git pull
updates your local repository with changes from the remote, whilegit push
updates the remote with your local changes.
2. Git Workflow
- Initializing a Repo: Start by initializing a repository with
git init
, creating a.git
directory that stores configuration and history. - Staging and Committing Changes:
- Staging: Add files to the staging area using
git add
. This prepares files to be committed. - Committing: Save a snapshot with
git commit -m "message"
.
- Staging: Add files to the staging area using
- Branching and Merging:
- Branching: Create a new branch with
git branch branch_name
orgit checkout -b branch_name
to develop features in isolation. - Merging: Use
git merge branch_name
to combine changes from one branch into another.
- Branching: Create a new branch with
3. Git Commands
- Setup & Configuration:bashgit config –global user.name “Your Name”
git config –global user.email “you@example.com” - Basic Workflow:bashgit init # Initialize a new repo
git clone <repo_url> # Clone an existing repo
git status # Verify your working directory’s condition
git add <file> # Stage a file
git commit -m “message” # Commit changes
git push # Push changes to remote
git pull # Pull changes from remote - Branching & Merging:bashgit branch <branch_name> # Create a new branch
git checkout <branch_name> # Switch to a branch
git merge <branch_name> # Merge a branch into the current branch
4. Git Workflow Models
- Feature Branch Workflow: Commonly used in collaborative environments; each feature is developed in a separate branch and merged when ready.
- Gitflow Workflow: Defines branches for production, development, and features, suitable for larger projects.
- Forking Workflow: Used in open-source, where contributors fork the project, make changes in their own copies, then propose changes via pull requests.
5. Popular Git Hosting Services
- GitHub: Popular for open-source projects, social features, and collaboration tools.
- GitLab: Offers integrated CI/CD and supports both public and private repositories.
- Bitbucket: Known for integrating with Jira, it’s often used by companies already using Atlassian products.
6. Git Best Practices
- Commit Often: Commit changes frequently with meaningful messages.
- Use Branches for Features: Isolate new features or changes in their own branches.
- Review Code Before Merging: Pull requests and code reviews help improve code quality.