Day 4: Mastering Git: Essential Commands for Beginners
Git is one of the most powerful version control systems used by developers worldwide. Whether you're working solo or in a team, Git helps you track changes, collaborate efficiently, and maintain a clean codebase.
In this guide, we’ll walk through the most essential Git commands, from installation to repository management, branching, merging, and beyond. If you're new to Git, this will be your go-to reference for getting started. Let's dive in! 🚀
1️⃣ Installing Git
Before anything else, ensure Git is installed on your system. You can check if it's already installed using:
git --version
If Git is not installed, use the following command:
sudo apt install git # For Debian-based Linux (Ubuntu, etc.)
For Windows and macOS, download and install Git from git-scm.com.
2️⃣ Configuring Git
After installation, configure your Git user details to ensure commits are associated with your identity:
git config --global user.email "thecodexmentis@gmail.com"
git config --global user.name "The-Codex-Mentis"
To verify your Git configurations, use:
git config --list
3️⃣ Setting Up SSH Authentication
Using SSH authentication allows you to interact with GitHub without repeatedly entering your password. Follow these steps:
🔹 Generate an SSH key:
ssh-keygen -t rsa -C "thecodexmentis@gmail.com" -f "The-Codex-Mentis"
🔹 Start the SSH agent and add your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/The-Codex-Mentis
🔹 Retrieve your public key to add it to GitHub:
vim ~/.ssh/The-Codex-Mentis.pub
🔹 Test the SSH connection:
ssh -T git@github.com
If successful, you should see a message: "Hi The-Codex-Mentis! You've successfully authenticated..."
4️⃣ Creating and Cloning a Repository
🔹 Create a new local repository:
mkdir Codex-Mentis
cd Codex-Mentis/
git init
This initializes a new Git repository in the current directory.
🔹 Add a remote repository (GitHub link):
git remote add origin https://github.com/The-Codex-Mentis/The-Codex-Mentis.github.io.git
🔹 Clone an existing repository:
git clone git@github.com:The-Codex-Mentis/The-Codex-Mentis.github.io.git
5️⃣ Staging, Committing, and Pushing Changes
Once you have created or modified files, use these commands to track and save changes.
🔹 Add all files to staging:
git add .
🔹 Check the status of changes:
git status
🔹 Commit changes with a message:
git commit -m "It works!"
🔹 Push changes to the remote repository:
git push origin main # Replace 'main' with your branch name if different
6️⃣ Working with Branches
Branching allows you to work on features without affecting the main codebase.
🔹 Create a new branch:
git branch feature-branch
🔹 Switch to a branch:
git checkout feature-branch # Traditional way
# OR
git switch feature-branch # Modern alternative
🔹 Merge a branch into main:
git checkout main
git merge feature-branch
🔹 Delete a branch (locally and remotely):
git branch -d feature-branch # Delete locally
git push origin --delete feature-branch # Delete remotely
7️⃣ Pulling and Fetching Updates
🔹 Pull latest changes from remote repository:
git pull origin main
🔹 Fetch updates without merging automatically:
git fetch origin
8️⃣ Undoing and Resetting Changes
🔹 Undo the last commit but keep changes staged:
git reset --soft HEAD~1
🔹 Undo the last commit and discard changes:
git reset --hard HEAD~1
🔹 Discard uncommitted changes in a file:
git checkout -- filename
9️⃣ Git Log and History
🔹 View commit history:
git log --oneline --graph --all --decorate
🔹 Show detailed commit history:
git log
🔟 Stashing Changes
Stashing allows you to save work temporarily without committing it.
🔹 Save changes temporarily:
git stash
🔹 Retrieve last stashed changes:
git stash pop
Bonus: Tagging and Releasing
Tags help in versioning and tracking releases.
🔹 Create an annotated tag:
git tag -a v1.0 -m "Version 1.0"
🔹 Push tags to remote repository:
git push origin v1.0
Final Thoughts 🎯
Git is an indispensable tool for developers, and mastering these commands will make your workflow more efficient and professional. Whether you're pushing changes, branching out, or undoing mistakes, these commands will help you navigate Git like a pro.
Comments
Post a Comment
Share your thoughts...