GIT is a Version Control System (VCS) that helps developers manage changes to their codebase. Here are the key points:
Version Control
Git tracks changes to files over time.
It allows you to revert to previous versions, collaborate with others, and manage code history.
Distributed System
Each developer has a complete copy of the repository.
Changes can be synchronised between local and remote repositories.
Basic Concepts
Repository: A project’s folder managed by Git.
Commit: A snapshot of changes (code, files) with a unique hash.
Branch: Independent line of development.
Merge: Combining changes from one branch into another.
Pull Request (PR): Proposing changes to a repository.
Branching Strategies:
Feature Branches: Isolate new features or bug fixes.
Main Branch (usually ‘master’ or ‘main’): Represents stable code.
Release Branches: Prepare for production releases.
Collaboration:
Git enables collaboration among developers.
Platforms like GitHub, GitLab, and Bitbucket host Git repositories.
Common Commands : Let’s explore some of the commonly used Git commands along with their explanations and examples. These commands are essential for managing your Git repositories effectively.
- git init
Initialises a new Git repository in the current directory.
Creates a hidden
.git
subfolder to store version control data.git init
- git clone
Creates a local copy of a remote Git repository.
Useful for starting work on an existing project.
git clone <repository_url>
- git add
Stages changes for commit.
Adds modified files to the staging area.
git add myfile.txt
- git commit
Records changes in the repository.
Creates a snapshot of the staged changes.
git commit -m "Add new feature"
- git status
Shows the current status of the working directory.
Displays untracked, modified, and staged files.
git status
- git push
Uploads local commits to a remote repository.
Updates the remote branch with your changes.
git push origin main
- git pull
Fetches changes from a remote repository.
Updates your local branch with the latest changes.
git pull origin main
- git branch
Lists existing branches or creates a new branch.
Useful for managing different features or bug fixes.
git branch feature-branch
- git merge
Combines changes from one branch into another.
Merges feature branches into the main branch.
git merge feature-branch
- git stash
Temporarily saves changes without committing.
Useful for switching branches without losing work.
git stash save "Work in progress"
Advanced Commands : Let’s delve deeper into advanced Git commands and explore additional details about Git. Understanding these commands will enhance your proficiency in version control.
- git log
Displays a chronological list of commits in the repository.
Useful for tracking changes, understanding commit history, and identifying authors.
git log
- git diff
Shows differences between working directory, staging area, and the last commit.
Helps identify changes before committing.
git diff myfile.txt
- git reset
Unstages the changes from the staging area.
Can be used to uncommit changes (with options like
--soft
,--mixed
, or--hard
).Example for unstaging the changes to a file:
git reset myfile.txt
Example for uncommit the changes:
git reset --soft HEAD^
- git rebase
Reapplies commits from one branch onto another.
Useful for maintaining a linear commit history.
Example for rebase
feature
branch ontomain
branch:git checkout feature git rebase main
- git cherry-pick
Selectively applies specific commits from one branch to another.
Useful for incorporating specific changes without merging entire branches.
git cherry-pick <commit_hash>
- git tag
Creates a named reference to a specific commit (e.g., for releases or milestones).
Useful for marking important points in history.
git tag v1.0.0
- git bisect
Helps find the commit that introduced a bug.
Binary search through commit history.
git bisect start git bisect bad <bad_commit> git bisect good <good_commit>
- git submodule
Manages external dependencies as submodules within your repository.
Useful for including external libraries or projects.
git submodule add <repository_url> path/to/submodule
- git reflog
Shows a log of all references (branches, HEAD, stashes) even after they’ve been deleted.
Useful for recovering lost commits or branches.
git reflog
- git clean
Removes untracked files and directories from the working directory.
Useful for cleaning up unwanted files.
Example (dry run):
git clean -n
Example (force removal):
git clean -f
Remember, Git is a powerful tool, and mastering these commands will make you a more efficient developer. Keep exploring and experimenting!