Deep understanding of git essentials
Deep understanding of git essentials
What is Git?
Git is a distributed version control system designed for tracking changes in source code during software development. Unlike centralized version control systems, Git operates as a distributed system, allowing every developer to have a complete copy of the repository on their local machine. This decentralization enables offline work and collaboration without constant reliance on a central server.
Getting Started with Git
Installation Guide
-
Linux: Use the package manager (apt, yum, etc.) to install Git.
-
Mac OS X: Git can be installed through Homebrew, MacPorts, or by downloading the installer from the official Git website.
-
Windows: Download the installer from the Git website and follow the installation wizard.
Configuration
Once Git is installed, configure user credentials:
# Open a terminal or command prompt
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
To list all configurations set in Git:
git config --list
Git Commands
Repository Initialization and Cloning
Initializing a Repository
Initialize a new Git repository:
git init
This creates a hidden .git
folder containing version control information.
Cloning a Repository
Clone an existing repository:
git clone <repository_url>
This copies the entire repository history and files to the local machine.
Staging Changes
Adding a particular file
Add specific file changes to the staging area:
git add <file_name>
Adding all the files
git add .
Stages all modifications and additions within the current directory.
git add --all
Stages all modifications, additions, and deletions across the entire project.
Reviewing
Checking Status
Use git status
to check the current state of the repository.
Viewing Differences
git diff
Showcases disparities between the current state and the last commit.
git diff --staged
Spotlights alterations currently staged for the next commit.
Committing Changes
git commit -m "Commit message"
Records changes staged in the commit with a descriptive message.
git commit -am "Commit message"
Combines “add” and “commit” actions, staging all modified files and immediately committing them with a message.
Inspecting Commits
Viewing Commit History
git log
Presents a chronological history of commits with essential details.
git log --oneline
Provides a condensed log with abbreviated commit hashes alongside commit messages.
Inspecting Specific Commits
git show <commit_hash>
Displays detailed information about a specific commit.
Undoing Changes
Unstaging changes
git reset <file_name>
Removes specific files from the staging area while retaining changes in the working directory.
Resetting changes
git reset --hard HEAD
Resets both the staging area and the working directory to match the state of the last commit (HEAD).
Reverting commits
git revert <commit_hash>
Creates a new commit that effectively undoes the changes introduced by a particular commit.
Branching and Merging in Git
Creating and Switching Branches
git branch
Lists all branches with an asterisk denoting the current active branch.
git branch <branch_name>
Creates a new branch starting from the current commit.
git checkout <branch_name>
Switches the working directory to the specified branch.
Merging Changes
git merge <branch_name>
Integrates changes from a specified branch into the current branch.
Remote Repositories
A remote repository is a version of your project hosted on a different location or server.
git remote add origin <remote_url>
Links the local repository to a remote repository identified by the name “origin” and the specified URL.
git remote -v
Lists all remote repositories associated with the local repository.
git pull <remote> <branch>
Fetches changes from a remote repository and merges them into the current branch.
git push <remote> <branch>
Pushes committed changes from the local repository to the specified remote repository and branch.
Miscellaneous
.gitignore
file specifies files and directories to be excluded by Git from tracking or being committed.
git rm <file_name>
Removes a file from the repository and stages its deletion for the subsequent commit.
git clean -df
Clears out untracked files and directories from the working directory.
This comprehensive guide provides developers with a strong foundation in Git, facilitating efficient collaboration, precise version control, and streamlined project management. Mastering these essentials unlocks Git’s potential, propelling software development toward greater efficiency and collaboration.